@mrhitman/validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Tiny wrapper around superstruct for using in decorator way

import "reflect-metadata";
import { bind, validate, string, number, size } from '@mrhitman/validator';

class Test {
  @bind(size(string(), 4, 20)) // should be only one decorator that combine 
  foo: string;

  @bind(number()) // should be only one decorator that combine 
  foo: number;
}

const test = new Test();
test.bar = 3;
console.log(validate(test));
[
  StructError: At path: foo -- Expected a string, but received: undefined
      at Module._compile (node:internal/modules/cjs/loader:1101:14)
      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
    value: undefined,
    key: 'foo',
    type: 'string',
    refinement: undefined,
    path: [ 'foo' ],
    branch: [ Test {}, undefined ],
    failures: [Function (anonymous)]
  },
  undefined
]

with nestjs > 10 version

import { bind, validate, string, number, size, ValidationPipe } from '@mrhitman/validator';

class Test {
  @bind(size(string(), 4, 20)) // should be only one decorator that combine 
  foo: string;

  @bind(number()) // should be only one decorator that combine 
  foo: number;
}

@Controller('example')
export class ExampleController {
  @Post()
  @UsePipes(new ValidationPipe())
  create(@Body() dto: Test) {
    // Your controller logic here
  }
}

github example

Package Sidebar

Install

npm i @mrhitman/validator

Weekly Downloads

1

Version

1.0.6

License

ISC

Unpacked Size

8.62 kB

Total Files

10

Last publish

Collaborators

  • mrhitman