Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from DavidAdrielSolutions/master
Browse files Browse the repository at this point in the history
Add support for schema validation
  • Loading branch information
mattkingshott authored Jun 20, 2022
2 parents 4d9a76a + 3cc4426 commit 536639f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
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">
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions src/iodine.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,29 @@ export class Iodine {
return this.is(value, rules) === true;
}

/**
* Determine whether the given dictionary of values meets the given schema.
*
*/
isValidSchema(values = {}, schema = {}) {
const keys = Object.keys(schema);

if(keys.length === 0) return true;

for(let i = 0; i < keys.length; i++) {
const key = keys[i];

if(!values.hasOwnProperty(key)) return false;

const value = values[key];
const rules = schema[key];

if (!this.isValid(value, rules)) return false;
}

return true;
}

/**
* Replace the default error messages with a new set.
*
Expand Down
49 changes: 48 additions & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 536639f

Please sign in to comment.