-
Notifications
You must be signed in to change notification settings - Fork 4
Add AnyOf assert
#276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
josecarlosbrandao
wants to merge
1
commit into
feature/add-one-of-assert
Choose a base branch
from
feature/add-any-of-assert
base: feature/add-one-of-assert
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+206
−1
Open
Add AnyOf assert
#276
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Module dependencies. | ||
| */ | ||
|
|
||
| const { Constraint, Validator, Violation } = require('validator.js'); | ||
|
|
||
| /** | ||
| * Export `AnyOfAssert`. | ||
| */ | ||
|
|
||
| module.exports = function anyOfAssert(...constraintSets) { | ||
| if (constraintSets.length < 2) { | ||
| throw new Error('AnyOf constraint requires at least two constraint sets'); | ||
| } | ||
|
|
||
| /** | ||
| * Class name. | ||
| */ | ||
|
|
||
| this.__class__ = 'AnyOf'; | ||
|
|
||
| /** | ||
| * Validator instance. | ||
| */ | ||
|
|
||
| this.validator = new Validator(); | ||
|
|
||
| /** | ||
| * Validation algorithm. | ||
| */ | ||
|
|
||
| this.validate = value => { | ||
| const violations = []; | ||
|
|
||
| for (const constraintSet of constraintSets) { | ||
| const result = this.validator.validate(value, new Constraint(constraintSet, { deepRequired: true })); | ||
|
|
||
| if (result === true) { | ||
| return true; | ||
| } | ||
|
|
||
| violations.push(result); | ||
| } | ||
|
|
||
| throw new Violation(this, value, violations); | ||
| }; | ||
|
|
||
| return this; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Module dependencies. | ||
| */ | ||
|
|
||
| const { Assert: BaseAssert, Violation } = require('validator.js'); | ||
| const { describe, it } = require('node:test'); | ||
| const AnyOfAssert = require('../../src/asserts/any-of-assert.js'); | ||
|
|
||
| /** | ||
| * Extend `Assert` with `AnyOfAssert`. | ||
| */ | ||
|
|
||
| const Assert = BaseAssert.extend({ | ||
| AnyOf: AnyOfAssert | ||
| }); | ||
|
|
||
| /** | ||
| * Test `AnyOfAssert`. | ||
| */ | ||
|
|
||
| describe('AnyOfAssert', () => { | ||
| it('should throw an error if no constraint sets are provided', ({ assert }) => { | ||
| assert.throws(() => Assert.anyOf(), { message: 'AnyOf constraint requires at least two constraint sets' }); | ||
| }); | ||
|
|
||
| it('should throw an error if only one constraint set is provided', ({ assert }) => { | ||
| assert.throws(() => Assert.anyOf({ bar: [Assert.equalTo('foo')] }), { | ||
| message: 'AnyOf constraint requires at least two constraint sets' | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw an error if value does not match any constraint set', ({ assert }) => { | ||
| try { | ||
| Assert.anyOf({ bar: [Assert.equalTo('foo')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' }); | ||
|
|
||
| assert.fail(); | ||
| } catch (e) { | ||
| assert.ok(e instanceof Violation); | ||
| assert.equal(e.show().assert, 'AnyOf'); | ||
| } | ||
| }); | ||
|
|
||
| it('should include all violations in the error when no constraint set matches', ({ assert }) => { | ||
| try { | ||
| Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'qux' }); | ||
|
|
||
| assert.fail(); | ||
| } catch (e) { | ||
| const { violation } = e.show(); | ||
|
|
||
| assert.equal(violation.length, 2); | ||
| assert.ok(violation[0].bar[0] instanceof Violation); | ||
| assert.equal(violation[0].bar[0].show().assert, 'EqualTo'); | ||
| assert.equal(violation[0].bar[0].show().violation.value, 'biz'); | ||
| assert.ok(violation[1].bar[0] instanceof Violation); | ||
| assert.equal(violation[1].bar[0].show().assert, 'EqualTo'); | ||
| assert.equal(violation[1].bar[0].show().violation.value, 'baz'); | ||
| } | ||
| }); | ||
|
|
||
| it('should validate required fields using `deepRequired`', ({ assert }) => { | ||
| try { | ||
| Assert.anyOf( | ||
| { bar: [Assert.required(), Assert.notBlank()] }, | ||
| { baz: [Assert.required(), Assert.notBlank()] } | ||
| ).validate({}); | ||
|
|
||
| assert.fail(); | ||
| } catch (e) { | ||
| assert.ok(e instanceof Violation); | ||
| assert.equal(e.show().assert, 'AnyOf'); | ||
| } | ||
| }); | ||
|
|
||
| it('should throw an error if a constraint set with an extra assert does not match', ({ assert }) => { | ||
| try { | ||
| Assert.anyOf( | ||
| { | ||
| bar: [Assert.equalTo('biz')], | ||
| baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })] | ||
| }, | ||
| { bar: [Assert.equalTo('baz')] } | ||
| ).validate({ bar: 'biz', baz: { qux: 'wrong' } }); | ||
|
|
||
| assert.fail(); | ||
| } catch (e) { | ||
| assert.ok(e instanceof Violation); | ||
| assert.equal(e.show().assert, 'AnyOf'); | ||
| } | ||
| }); | ||
|
|
||
| it('should pass if value matches more than one constraint set', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass if value matches more than one constraint set with different constraints', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf({ bar: [Assert.notBlank()] }, { bar: [Assert.equalTo('biz')] }).validate({ bar: 'biz' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass if value matches the first constraint set', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'biz' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass if value matches the second constraint set', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf({ bar: [Assert.equalTo('biz')] }, { bar: [Assert.equalTo('baz')] }).validate({ bar: 'baz' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should support more than two constraint sets', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf( | ||
| { bar: [Assert.equalTo('biz')] }, | ||
| { bar: [Assert.equalTo('baz')] }, | ||
| { bar: [Assert.equalTo('qux')] } | ||
| ).validate({ bar: 'qux' }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should pass if a constraint set contains an extra assert', ({ assert }) => { | ||
| assert.doesNotThrow(() => { | ||
| Assert.anyOf( | ||
| { | ||
| bar: [Assert.equalTo('biz')], | ||
| baz: [Assert.anyOf({ qux: [Assert.equalTo('corge')] }, { qux: [Assert.equalTo('grault')] })] | ||
| }, | ||
| { bar: [Assert.equalTo('baz')] } | ||
| ).validate({ bar: 'biz', baz: { qux: 'corge' } }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view, I’d use it like this, just a dummy example:
So if I’m seeding an instance of
is.ofLength({ max: 254 })into the constraints set, this method returns an object that has avalidatemethod, so no need to redeclarenew Validator();everytime we want to do any of.I should be able to call it directly like this, right?
is.ofLength({ max: 254 }).validate(value);