diff --git a/README.md b/README.md index 807f7a7..9991357 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ Add the following to your tsconfig.json 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](https://www.npmjs.com/package/fastest-validator). **@Schema(strict=false, messages={})** - Schema decorator. +**@Schema({ strict = false, async = false }, messages={})** - Schema decorator. **@Field({})** - Generic decorator, no default properties set. Will apply all options to the schema. @@ -143,7 +144,7 @@ All decorators accept an object of options that apply to the type being used, fo ```ts @Custom({ - check (value: number, errors: {type: string}[]){ + check (value: number, errors: {type: string, actual: number}[]){ if (value % 2 !== 0) { errors.push({ type: "even", actual : value }); } diff --git a/src/index.spec.ts b/src/index.spec.ts index 2c44952..d3e93af 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -841,11 +841,7 @@ describe("Custom async", () => { }) prop!: unknown; } - - /** - * $$async key is removed for unknown reason from schema object at compile() - * https://github.com/icebob/fastest-validator/blob/a746f9311d3ebeda986e4896d39619bfc925ce65/lib/validator.js#L176 - */ + expect(getSchema(Test)).toEqual({ $$strict: false, $$async: true, diff --git a/src/index.ts b/src/index.ts index 6649ee0..5028721 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,6 +92,8 @@ export function Schema (schemaOptions?: StrictMode | SchemaOptions, messages = { /** * Make a copy of the schema, in order to keep the original from being overriden or deleted by fastest-validator + * $$async key is removed from schema object at compile() + * https://github.com/icebob/fastest-validator/blob/a746f9311d3ebeda986e4896d39619bfc925ce65/lib/validator.js#L176 */ Reflect.defineMetadata(COMPILE_KEY, v.compile({...s}), target); return target; @@ -132,6 +134,7 @@ export function Nested (options: any | any[] = {}): any { const props = Object.assign({}, getSchema(t)); const strict = props.$$strict || false; delete props.$$strict; + // never $$async in nested delete props.$$async; updateSchema(target, key, { ...options, props, strict, type: "object" }); };