You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: API.md
+80-1
Original file line number
Diff line number
Diff line change
@@ -784,14 +784,92 @@ Adds an external validation rule where:
784
784
-`value` - a clone of the object containing the value being validated.
785
785
-`helpers` - an object with the following helpers:
786
786
-`prefs` - the current preferences.
787
+
-`path` - ordered array where each element is the accessor to the value where the error happened.
788
+
-`label` - label of the value. If you are validating an object's property, it will contain the name of that property.
789
+
-`root` - the root object or primitive value under validation.
790
+
-`context` - same as `root`, but contains only the closest parent object in case of nested objects validation.
791
+
-`error` - a function with signature `function(message)`. You can use it in a return statement (`return error('Oops!')`) or you can call it multiple times if you want to push more than one error message in a single external validator.
787
792
-`description` - optional string used to document the purpose of the method.
788
793
789
794
Note that external validation rules are only called after the all other validation rules for the
790
795
entire schema (from the value root) are checked. This means that any changes made to the value by
791
796
the external rules are not available to any other validation rules during the non-external
792
797
validation phase.
793
798
794
-
If schema validation failed, no external validation rules are called.
799
+
By default, if schema validation fails, no external validation rules are called. You can change this
800
+
behavior by using `abortEarly: false` and `alwaysExecuteExternals: true` settings together.
801
+
802
+
Chains of external validation rules abort early regardless of any settings.
803
+
804
+
If your validator returns a replacement value after it added an error (using `error` helper), the replacement value will be ignored.
// you can add more than one error in a single validator
862
+
error('error 1');
863
+
error('error 2');
864
+
865
+
// you can return at any moment
866
+
if (value ==='hi!') {
867
+
return;
868
+
}
869
+
870
+
error('error 3');
871
+
})
872
+
```
795
873
796
874
#### `any.extract(path)`
797
875
@@ -1131,6 +1209,7 @@ Validates a value using the current schema and options where:
1131
1209
- `string` - the characters used around each array string values. Defaults to `false`.
1132
1210
- `wrapArrays` - if `true`, array values in error messages are wrapped in `[]`. Defaults to `true`.
1133
1211
- `externals` - if `false`, the external rules set with [`any.external()`](#anyexternalmethod-description) are ignored, which is required to ignore any external validations in synchronous mode (or an exception is thrown). Defaults to `true`.
1212
+
- `alwaysExecuteExternals` - if `true`, and `abortEarly` is `false`, the external rules set with [`any.external()`](#anyexternalmethod-description) will be executed even after synchronous validators have failed. This setting has no effect if `abortEarly` is `true` since external rules get executed after all other validators. Default: `false`.
1134
1213
- `messages` - overrides individual error messages. Defaults to no override (`{}`). Use the `'*'` error code as a catch-all for all error codes that do not have a message provided in the override. Messages use the same rules as [templates](#template-syntax). Variables in double braces `{{var}}` are HTML escaped if the option `errors.escapeHtml` is set to `true`.
1135
1214
- `noDefaults` - when `true`, do not apply default values. Defaults to `false`.
1136
1215
- `nonEnumerables` - when `true`, inputs are shallow cloned to include non-enumerable properties. Defaults to `false`.
0 commit comments