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
-[range and exclusiveRange](#range-and-exclusiverange)
18
+
-[propertyNames](#propertynames)
19
+
-[regexp](#regexp)
20
+
-[dynamicDefaults](#dynamicdefaults)
21
+
-[License](#license)
22
+
23
+
10
24
## Install
11
25
12
26
```
@@ -168,6 +182,117 @@ var invalidData = {
168
182
```
169
183
170
184
185
+
### `dynamicDefaults`
186
+
187
+
This keyword allows to assign dynamic defaults to properties, such as timestamps, unique IDs etc.
188
+
189
+
This keyword only works if `useDefaults` options is used and not inside `anyOf` keywrods etc., in the same way as [default keyword treated by Ajv](https://github.com/epoberezkin/ajv#assigning-defaults).
190
+
191
+
The keyword should be added on the object level. Its value should be an object with each property corresponding to a property name, in the same way as in standard `properties` keyword. The value of each property can be:
192
+
193
+
- an identifier of default function (a string)
194
+
- an object with properties `func` (an identifier) and `args` (an object with parameters that will be passed to this function during schema compilation - see examples).
195
+
196
+
The properties used in `dynamicDefaults` should not be added to `required` keyword (or validation will fail), because unlike `default` this keyword is processed after validation.
197
+
198
+
There are several predefined dynamic default functions:
199
+
200
+
-`"timestamp"` - current timestamp in milliseconds
201
+
-`"datetime"' - current date and time as string (ISO, valid according to `date-time` format)
202
+
-`"date"' - current date as string (ISO, valid according to `date` format)
203
+
-`"time"` - current time as string (ISO, valid according to `time` format)
204
+
-`"random"` - pseudo-random number in [0, 1) interval
205
+
-`"randomint"` - pseudo-random integer number. If string is used as a property value, the function will randomly return 0 or 1. If object `{func: 'randomint', max: N}` is used then the default will be an integer number in [0, N) interval.
206
+
-`"seq"` - sequential integer number starting from 0. If string is used as a property value, the default sequence will be used. If object `{func: 'seq', name: 'foo'}` is used then the sequence with name `"foo"` will be used. Sequences are global, even if different ajv instances are used.
0 commit comments