Skip to content

Commit 3018aa9

Browse files
committed
readme
1 parent 4617264 commit 3018aa9

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,49 @@ schema.validate({ name: 'jimmy', age: 'hi' })
374374
})
375375
```
376376
377-
#### `mixed.isValid(value: any, options: ?object: Promise<boolean>`
377+
#### `mixed.validateSync(value: any, options: ?object): any`
378+
379+
Runs validatations synchronously _if possible_ and returns the resulting value,
380+
or throws a ValidationError. Accepts all the same options as `validate`.
381+
382+
Synchronous validation only works if there are no configured async tests, e.g tests that return a Promise.
383+
For instance this will work:
384+
385+
```js
386+
schema = number.test(
387+
'is-42',
388+
'this isn\'t the number i want',
389+
value => value != 42
390+
)
391+
392+
schema.validateSync(23) // throws ValidationError
393+
```
394+
395+
however this will not:
396+
397+
```js
398+
schema = number.test(
399+
'is-42',
400+
'this isn\'t the number i want',
401+
value => Promise.resolve(value != 42)
402+
)
403+
404+
schema.validateSync(42) // throws Error
405+
```
406+
407+
#### `mixed.isValid(value: any, options: ?object): Promise<boolean>`
378408
379409
Returns `true` when the passed in value matches the schema. `isValid`
380410
is __asynchronous__ and returns a Promise object.
381411
382412
Takes the same options as `validate()`.
383413
414+
#### `mixed.isValidSync(value: any, options: ?object): boolean`
415+
416+
Synchronously returns `true` when the passed in value matches the schema.
417+
418+
Takes the same options as `validateSync()` and has the same caveats around async tests.
419+
384420
#### `mixed.cast(value: any): any`
385421
386422
Attempts to coerce the passed in value to a value that matches the schema. For example: `'5'` will
@@ -566,7 +602,7 @@ inst.validate({ isBig: false, count: 4 })
566602

567603
Adds a test function to the validation chain. Tests are run after any object is cast.
568604
Many types have some tests built in, but you can create custom ones easily.
569-
In order to allow asynchronous custom validations _all_ tests are run asynchronously.
605+
In order to allow asynchronous custom validations _all_ (or no) tests are run asynchronously.
570606
A consequence of this is that test execution order cannot be guaranteed.
571607

572608
All tests must provide a `name`, an error `message` and a validation function that must return

0 commit comments

Comments
 (0)