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
import { enforce } from 'passable'; | |
enforce(); | |
enforce(1); | |
enforce(2).isNumber(); | |
enforce([]).allOf({ | |
isArray: true, | |
isEmpty: true | |
}); |
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
// Le'ts start with this object | |
const orig = { | |
a: 'get', | |
b: 'moving' | |
}; | |
// Now let's write a handler that serves as a getter for our object. | |
const handler = { | |
// by setting the `get` function we say that we intend to | |
// intercept `get` interactions with the object |
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
const enforce = new Enforce({ | |
hasDog: (value) => /dog/.test(value) | |
}); | |
enforce('dog').hasDog(); | |
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ, hasDog: ƒ} | |
enforce('cat').hasDog(); | |
// Uncaught Error: hasDog: YOU SHALL NOT PASS! |
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
const enforce = new Enforce(); | |
enforce(55); | |
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ} | |
enforce(55).largerThan(20); | |
// Proxy {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ} | |
enforce(55).largerThan(200); | |
// Uncaught Error: largerThan: YOU SHALL NOT PASS! |
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
const rules = { | |
isArray: (value) => Array.isArray(value), | |
largerThan: (value, compare) => value > compare, | |
smallerThan: (value, compare) => value < compare, | |
isEmpty: (value) => !value || Object.keys(value).length === 0 | |
}; | |
// export default rules |
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
enforce() | |
// {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ} | |
enforce(55).smallerThan(100); | |
// {isArray: ƒ, largerThan: ƒ, smallerThan: ƒ, isEmpty: ƒ} | |
enforce(55).smallerThan(100).largerThan(56); | |
// Uncaught Error: validation failed for largerThan | |
enforce('passable').isArray(); |
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
// 1: accepts a value | |
enforce(1) | |
// 2: returns an object | |
{ | |
isNumber: // func, | |
largerThan: // func, | |
... | |
} | |
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
// import rules from './rules.js'; | |
// This is a `rule runner` It runs the rule it gets | |
// * throws an error on failure | |
// * returns the wrapped rules object on success so you can keep on chaining | |
function ruleRunner(rule, wrapped, value, ...args) { | |
const result = rule(value, ...args); | |
// if the result is not explicitly true, throw | |
if (result !== true) { |
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
import passable, { enforce } from passable; | |
passable('new_user_form', (test) => { | |
test('username', 'Must be between 5 and 20 chars', () => { | |
enforce(username).allOf({ | |
largerThan: 4, | |
smallerThan: 20 | |
}); | |
}); |
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
// import rules from './rules'; | |
class Enforce { | |
constructor(custom = {}) { | |
this.rules = Object.assign({}, rules, custom); | |
return this.enforce; | |
} | |
enforce = (value) => { | |
this.value = value; |
NewerOlder