diff --git a/README.md b/README.md index ae84dd4..0b1b92a 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,9 @@ const schema = object({ - **`InferType`**: get the output type of a schema - **`Schema`**: dictates that a value is a schema that has an output type of `T` +### Validation options +- `abortEarly`: stop validation when the first invalid field is found. Default is `true` when running in *NodeJS*, to avoid performance issues. + ### Creating a schema of my own: Just extend the class of the closest schema there is for your type of value, and call the `transform()` and `test()` methods in your new schema to setup the validation logic that will be run. Can be either in it's _constructor_, or you can add new methods to your schema. diff --git a/packages/not-me/package-lock.json b/packages/not-me/package-lock.json index 791fb65..a6d4f04 100644 --- a/packages/not-me/package-lock.json +++ b/packages/not-me/package-lock.json @@ -1,6 +1,6 @@ { "name": "not-me", - "version": "2.0.2", + "version": "2.0.3", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/packages/not-me/package.json b/packages/not-me/package.json index 71e24e4..43f22a8 100644 --- a/packages/not-me/package.json +++ b/packages/not-me/package.json @@ -1,6 +1,6 @@ { "name": "not-me", - "version": "2.0.2", + "version": "2.0.3", "description": "Easy and type-safe validation", "main": "lib/index.js", "types": "lib/types.d.ts", diff --git a/packages/not-me/src/schemas/base/base-schema.ts b/packages/not-me/src/schemas/base/base-schema.ts index 55343c5..7373668 100644 --- a/packages/not-me/src/schemas/base/base-schema.ts +++ b/packages/not-me/src/schemas/base/base-schema.ts @@ -77,8 +77,14 @@ export abstract class BaseSchema< validate( input: unknown, - options: ValidationOptions = undefined + options?: ValidationOptions ): ValidationResult> { + const _options: ValidationOptions = { + // Use typeof to avoid 'process is not defined' error + abortEarly: typeof process === "undefined" ? false : true, + ...options, + }; + let _currentValue = input; if (this.wrapValueBeforeValidation) { @@ -116,7 +122,7 @@ export abstract class BaseSchema< const typeFilterResponse = this.baseTypeFilter.filterFn( _currentValue, - options + _options ); if (typeFilterResponse.errors) { @@ -140,7 +146,7 @@ export abstract class BaseSchema< const filterRes = shapeFilter.filterFn( shapedValueWithUnknownProperties, - options + _options ); if (filterRes.errors) { @@ -159,7 +165,7 @@ export abstract class BaseSchema< for (let i = 0; i < this.shapeFilters.length; i++) { const shapeFilter = this.shapeFilters[i] || throwError(); - const filterRes = shapeFilter.filterFn(shapedValue, options); + const filterRes = shapeFilter.filterFn(shapedValue, _options); if (filterRes.errors) { return filterRes; @@ -184,7 +190,7 @@ export abstract class BaseSchema< if (!valid) { const messages = [filter.getMessage()]; - if (options?.abortEarly) { + if (_options.abortEarly) { return { errors: true, messagesTree: messages,