Wondering what’s next for npm?Check out our public roadmap! »

fastest-validator-typescriptTypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

npm npm GitHub issues

Fastest Validator Typescript

Decorators for fastest-validator

Setup

Install the package

npm install --save fastest-validator-typescript

Example usage

    @Schema(true)
    class AnotherNestedObject {
      @Number({positive: true, integer: true})
      prop: number;
 
      get total (): number{return 1;}
 
    }
 
    @Schema(true)
    class NestedObject {
      @Nested()
      anotherNested: AnotherNestedObject;
      get total (): number{return 1;}
    }
 
    @Schema()
    class SomeModel {
      @NestedArray(NestedObject)
      prop: NestedObject[];
      get total (): number{return 1;}
    }
    const t: SomeModel = Object.assign(new SomeModel(), {prop: [{anotherNested: {prop: 1}}]});
    expect(transformAndValidate(t)).toEqual(true);
    expect(t.total).toEqual(1);
    expect(t.prop[0].total).toEqual(1);
    expect(t.prop[0].anotherNested.total).toEqual(1);

Add the following to your tsconfig.json

"experimentalDecorators": true
"emitDecoratorMetadata": true

Available decorators

All decorators accept an object of options that apply to the type being used, for a full list of options please refer to the fastest-validator documentation.

@Schema(strict=false, messages={}) - Schema decorator.

@Field({}) - Generic decorator, no default properties set. Will apply all options to the schema.

@String({}) - Applies { type: "string", empty: false }

@Boolean({}) - Applies { type: "boolean" }

@Number({}) - Applies { type: "number", convert: true }

@UUID({}) - Applies { type: "uuid" }

@ObjectId({}) - Applies { type: "string", pattern: /^[a-f\d]{24}$/i }

@Email({}) - Applies { type: "email" }

@Date({}) - Applies { type: "date" }

@Enum({}) - Applies { type: "enum" }

@Array({}) - Applies { type: "array" }

@Nested({}) - Applies { type: "object", props: {} } (The props are gathered from the nested schema)

@NestedArray(type, {}) - Applies { type: "object", props: {} } (The props are gathered from the nested schema)

Available methods

getSchema() - Returns the fastest-validator schema for a given class

validate() - Returns true or fastest-validator errors for a given instance

transformAndValidate() - validate and tranform object.

transform() - nested transform object to the ES6 classes

validateOrReject() - Returns true or throws fastest-validator errors for a given instance

ESLint

To force using validation decorators add rule to eslint file (within domain folder).

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "fastest-validator"],
  "extends": ["../.eslintrc.json"],
  "rules": {
    "fastest-validator/fastest-validator": "error"
  }
}

License

Licensed under the MIT license.

Install

npm i fastest-validator-typescript

DownloadsWeekly Downloads

26

Version

1.0.7

License

MIT

Unpacked Size

35.8 kB

Total Files

8

Last publish

Collaborators

  • avatar