fastest-validator-typesciprt v0.0.16
Fastest Validator Decorators
Decorators for fastest-validator
Example usage
import {
Schema,
Array,
Nested,
UUID,
Enum,
Email,
Number,
getSchema,
validate,
validateOrReject
} from "fastest-validator-decorators";
@Schema(true)
class Entity1 {
@Array({ items: "string"})
prop1: string[];
}
@Schema()
class Entity2 {
@UUID()
prop1: string;
@Enum({ values : ["one", "two"] })
prop2: "one" | "two";
@Email()
prop3: string;
@Number({ positive: true })
prop4: number;
@Nested()
prop5: Entity1;
}
const schema = getSchema(Entity2); // get the fastest-validator schema
{
$$strict: false,
prop1: { type: "uuid" },
prop2: { type: "enum", values: ["one", "two"] },
prop3: { type: "email" },
prop4: { type: "number", positive: true, convert: true },
prop5: { type: "object", strict: true, props: {
prop1: { type: "array", items: "string" }
}}
}
const entity = new Entity2();
entity.prop1 = "thiswillfail";
entity.prop2 = "one";
entity.prop3 = "some@email.com"
entity.prop4 = -10;
entity.prop5 = new Entity1();
const result = validate(entity); // returns true or fastest-validator errors
const result = await validateOrReject(entity); // returns true or throws fastest-validator errors
Setup
Install the package
npm install --save fastest-validator-decorators
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)
Available methods
getSchema() - Returns the fastest-validator schema for a given class
validate() - Returns true or fastest-validator errors for a given instance
validateOrReject() - Returns true or throws fastest-validator errors for a given instance
License
Licensed under the MIT license.