Skip to main content
ThunderLang™Programming Language

Gallery · TypeScript · NestJS

How NestJS checks a number in a URL

NestJS is a widely used TypeScript server framework. ParseIntPipe is what turns the "42" in /users/42 into the number 42, or rejects the request before your handler sees it.

What to look for

An optional, missing value passes straight through; anything that is not a numeric string stops with "Validation failed".

How much Thunder could read

4 of 5 steps put into words; the other 1 is shown as code.

Read by the current Thunder compiler when this page was built. Hand-checked against the code: accurate, with some wording that is thin or close to the code.

Flow: How does it go? One action or rule as a path: questions with Yes and No, repeats, the Stops, what happens if something goes wrong, and what always happens at the end.

Parse int pipe can

Transform

From codelines 51–61

Author's noteMethod that accesses and performs optional transformation on argument for in-flight requests.

You give itvalue, metadata
You get backa Promise<number>
  1. Value is nil and the options's optional is set?

    Yes

    1. Gives back value

    No continue

  2. Is numeric?

    No

    1. Stop: the error made by exception factory: "Validation failed (numeric string is expected)"

    Yes continue

  3. return parseInt(value, 10);
The same reading as Thunder Text, built with this page
Transform (Parse int pipe)
  Author's note: Method that accesses and performs optional transformation on argument for in-flight requests.
  You give it: value, metadata
  You get back: a Promise<number>
  - Ask: Value is nil and the options's optional is set?  (line 52)
    Yes:
      - Gives back value  (line 53)
    No: continue
  - Ask: Is numeric?  (line 55)
    Yes: continue
    No:
      - Stop: the error made by exception factory: "Validation failed (numeric string is expected)"  (line 56)
  - `return parseInt(value, 10);` (code, not explained)  (line 60)

Source and license

From NestJS (nestjs/nest), packages/common/pipes/parse-int.pipe.ts, lines 51–61, at commit 5ae7e0f (v10.4.4).

Copyright (c) 2017-2024 Kamil Mysliwiec. Licensed under the MIT. The code is shown unmodified; the Thunder reading is ThunderLang's.

Full license text (LICENSE)
(The MIT License)

Copyright (c) 2017-2024 Kamil Mysliwiec <https://kamilmysliwiec.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.