This repository has been archived by the owner on Apr 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from DavidAdrielSolutions/master
Add support for schema validation
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
<!-- Screenshot --> | ||
<p align="center"> | ||
<img src="resources/example.png" alt="Code example"> | ||
|
@@ -85,6 +86,21 @@ Iodine.isValid(item_1, ['required', 'integer']); // true | |
Iodine.isValid(item_2, ['required', 'integer']); // false | ||
``` | ||
|
||
### Schema validation | ||
|
||
If you want to compare an object against a schema. In other words, you want to run a list of checks against a list of values, then you can use the `isValidSchema` helper method. **Note** : This method uses `isValid`under the hood, hence it returns a `boolean`. | ||
|
||
```js | ||
Iodine.isValidSchema( | ||
{ email: '[email protected]', password: 'abcdefgh', fullname: 'John Doe' }, | ||
{ | ||
email: [ 'required' , 'email' ], | ||
password: [ 'required' , 'minLength:6' ], | ||
fullname: [ 'required' , 'minLength:3' ] | ||
} | ||
); // true | ||
``` | ||
|
||
## Additional parameters | ||
|
||
Some rules require extra parameters e.g. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,6 +397,53 @@ test('return true/false', () => { | |
expect(Iodine.isValid(undefined, ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); | ||
}); | ||
|
||
/** | ||
* Confirm that the 'isValidSchema' method returns the right value against multiple schemas. | ||
* | ||
*/ | ||
test('schema validation', () => { | ||
|
||
expect( | ||
Iodine.isValidSchema( | ||
{ }, | ||
{ | ||
website: [ 'required' , 'url' ], | ||
ping: [ 'required' , 'integer' ], | ||
} | ||
) | ||
).toBe(false); | ||
|
||
expect( | ||
Iodine.isValidSchema( | ||
{ email: '[email protected]', password: 'abcdefgh', fullname: 'John Doe' }, | ||
{ | ||
email: [ 'required' , 'email' ], | ||
password: [ 'required' , 'minLength:6' ], | ||
fullname: [ 'required' , 'minLength:3' ] | ||
} | ||
) | ||
).toBe(true); | ||
|
||
expect( | ||
Iodine.isValidSchema( | ||
{ website: 'https://iodine.is', ping: 'ninety' }, | ||
{ | ||
website: [ 'required' , 'url' ], | ||
} | ||
) | ||
).toBe(true); | ||
|
||
expect( | ||
Iodine.isValidSchema( | ||
{ website: 'https://iodine.io', ping: 'ninety' }, | ||
{ | ||
website: [ 'required' , 'url' ], | ||
ping: [ 'required' , 'integer' ], | ||
} | ||
) | ||
).toBe(false); | ||
}); | ||
|
||
/** | ||
* Confirm that the 'is' method can handle rules that contain semicolons. | ||
* | ||
|
@@ -541,4 +588,4 @@ test('it can add async custom rules', async () => { | |
expect(await Iodine.asyncIs(1, ['required', 'timeoutEquals:2'])).toBe('timeoutEquals:2'); | ||
expect(await Iodine.asyncIsValid(1, ['required', 'integer', 'timeoutEquals:1'])).toBe(true); | ||
expect(await Iodine.asyncIsValid(1, ['required', 'integer', 'timeoutEquals:2'])).toBe(false); | ||
}); | ||
}); |