@@ -185,6 +185,25 @@ Thrown on failed validations, with the following properties
185
185
validation chain. When the ` abortEarly ` option is ` false ` this is where you can inspect each error thrown,
186
186
alternatively ` errors ` will have all the of the messages from each inner error.
187
187
188
+ #### ` ref(String path, Object options) `
189
+
190
+ Creates a reference to another sibling or sibling descendant field. Ref's are resolved
191
+ at _ run time_ and supported where specified. Ref's are evaluated in in the proper order so that
192
+ the ref value is resolved before the field using the ref (be careful of circular dependencies!).
193
+
194
+ ``` js
195
+ var schema = object ({
196
+ baz: ref (' foo.bar' ),
197
+ foo: object ({
198
+ bar: string ()
199
+ })
200
+ x: ref (' $x' )
201
+ })
202
+
203
+ inst .cast ({ foo: { bar: ' boom' } }, { context: { x: 5 } })
204
+ // { baz: 'boom', x: 5, { foo: { bar: 'boom' } }, }
205
+ ```
206
+
188
207
189
208
### mixed
190
209
@@ -365,7 +384,7 @@ schema.isValid(42) //=> false
365
384
schema .isValid (new Date ) // => true
366
385
```
367
386
368
- #### ` mixed.when(String key , Object options | Function func) `
387
+ #### ` mixed.when(String|Array<String> keys , Object options | Function func) `
369
388
370
389
Adjust the schema based on a sibling or sibling children fields. You can provide an object
371
390
literal where the key ` is ` is value or a matcher function, ` then ` provides the true schema and/or
@@ -374,11 +393,8 @@ literal where the key `is` is value or a matcher function, `then` provides the t
374
393
` is ` conditions are strictly compared (` === ` ) if you want to use a different form of equality you
375
394
can provide a function like: ` is: (value) => value == true ` .
376
395
377
- Alternatively you can provide a function the returns a schema (called with the value of the key
378
- and the current schema). ` when ` conditions are additive.
379
-
380
396
Like joi you can also prefix properties with ` $ ` to specify a property that is dependent
381
- on ` context ` passed in by ` validate() ` or ` isValid ` .
397
+ on ` context ` passed in by ` validate() ` or ` isValid ` . ` when ` conditions are additive.
382
398
383
399
``` javascript
384
400
var inst = yup .object ({
@@ -397,6 +413,42 @@ var inst = yup.object({
397
413
inst .validate (value, { context: { other: 4 }})
398
414
```
399
415
416
+ You can also specify more than one dependent key, in which case each value will be spread as an argument.
417
+
418
+ ``` javascript
419
+ var inst = yup .object ({
420
+ isSpecial: yup .bool ()
421
+ isBig: yup .bool (),
422
+ count: yup .number ()
423
+ .when ([' isBig' , ' isSpecial' ], {
424
+ is: true , // alternatively: (isBig, isSpecial) => isBig && isSpecial
425
+ then: yup .number ().min (5 ),
426
+ otherwise: yup .number ().min (0 )
427
+ })
428
+ })
429
+
430
+ inst .validate ({
431
+ isBig: true ,
432
+ isSpecial: true ,
433
+ count: 10
434
+ })
435
+ ```
436
+
437
+ Alternatively you can provide a function the returns a schema
438
+ (called with the value of the key and the current schema).
439
+
440
+ ``` js
441
+ var inst = yup .object ({
442
+ isBig: yup .boolean (),
443
+ count: yup .number ()
444
+ .when (' isBig' , (isBig , schema ) => {
445
+ return isBig ? schema .min (5 ) : schema .min (0 )
446
+ })
447
+ })
448
+
449
+ inst .validate ({ isBig: false , count: 4 })
450
+ ```
451
+
400
452
401
453
#### ` mixed.test(String name, String message, Function fn, [Bool callbackStyleAsync]) `
402
454
@@ -539,11 +591,11 @@ schema.isValid('hello') //=> true
539
591
The same as the ` mixed() ` schema required, except that empty strings are also considered 'missing' values.
540
592
To allow empty strings but fail on ` undefined ` values use: ` string().required().min(0) `
541
593
542
- #### ` string.min(Number limit, [String message]) `
594
+ #### ` string.min(Number|Ref limit, [String message]) `
543
595
544
596
Set an minimum length limit for the string value. The ` ${min} ` interpolation can be used in the ` message ` argument
545
597
546
- #### ` string.max(Number limit, [String message]) `
598
+ #### ` string.max(Number|Ref limit, [String message]) `
547
599
548
600
Set an maximum length limit for the string value. The ` ${max} ` interpolation can be used in the ` message ` argument
549
601
@@ -588,12 +640,12 @@ var schema = yup.number();
588
640
schema .isValid (10 ) // => true
589
641
```
590
642
591
- #### ` number.min(Number limit, [String message]) `
643
+ #### ` number.min(Number|Ref limit, [String message]) `
592
644
593
645
Set the minimum value allowed. The ` ${min} ` interpolation can be used in the
594
646
` message ` argument.
595
647
596
- #### ` number.max(Number limit, [String message]) `
648
+ #### ` number.max(Number|Ref limit, [String message]) `
597
649
598
650
Set the maximum value allowed. The ` ${max} ` interpolation can be used in the
599
651
` message ` argument.
@@ -636,11 +688,11 @@ var schema = yup.date();
636
688
schema .isValid (new Date ) // => true
637
689
```
638
690
639
- #### ` date.min(Date|String limit, [String message]) `
691
+ #### ` date.min(Date|String|Ref limit, [String message]) `
640
692
641
693
Set the minimum date allowed.
642
694
643
- #### ` date.max(Date|String limit, [String message]) `
695
+ #### ` date.max(Date|String|Ref limit, [String message]) `
644
696
645
697
Set the maximum date allowed.
646
698
@@ -668,11 +720,11 @@ not validate its contents.
668
720
The same as the ` mixed() ` schema required, except that empty arrays are also considered 'missing' values.
669
721
To allow empty arrays but fail on ` undefined ` values use: ` array().required().min(0) `
670
722
671
- #### ` array.min(Number limit, [String message]) `
723
+ #### ` array.min(Number|Ref limit, [String message]) `
672
724
673
725
Set an minimum length limit for the array. The ` ${min} ` interpolation can be used in the ` message ` argument.
674
726
675
- #### ` array.max(Number limit, [String message]) `
727
+ #### ` array.max(Number|Ref limit, [String message]) `
676
728
677
729
Set an maximum length limit for the array. The ` ${max} ` interpolation can be used in the ` message ` argument.
678
730
0 commit comments