Skip to content

Commit

Permalink
Abort validation early on NodeJS by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartmr committed Jun 27, 2021
1 parent d78b85d commit 6a612cd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ const schema = object({
- **`InferType<typeof schema>`**: get the output type of a schema
- **`Schema<T>`**: 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.
Expand Down
2 changes: 1 addition & 1 deletion packages/not-me/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/not-me/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 11 additions & 5 deletions packages/not-me/src/schemas/base/base-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ export abstract class BaseSchema<

validate(
input: unknown,
options: ValidationOptions = undefined
options?: ValidationOptions
): ValidationResult<InferType<this>> {
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) {
Expand Down Expand Up @@ -116,7 +122,7 @@ export abstract class BaseSchema<

const typeFilterResponse = this.baseTypeFilter.filterFn(
_currentValue,
options
_options
);

if (typeFilterResponse.errors) {
Expand All @@ -140,7 +146,7 @@ export abstract class BaseSchema<

const filterRes = shapeFilter.filterFn(
shapedValueWithUnknownProperties,
options
_options
);

if (filterRes.errors) {
Expand All @@ -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;
Expand All @@ -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,
Expand Down

0 comments on commit 6a612cd

Please sign in to comment.