Skip to content

Commit

Permalink
feat(api): check.schema is curried
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 17, 2015
1 parent 8e629e5 commit 1b5444c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
25 changes: 25 additions & 0 deletions README.md
Expand Up @@ -425,6 +425,13 @@ check.badItems(check.unemptyString, ['foo', '', 'bar']); // ['']
This makes it very convenient to create new validator functions using partial
argument application

The method is curried, thus you can easily create predicate function

```js
var hasName = check.schema({ name: check.unemptyString });
hasName({ name: 'joe' }); // true
```

#### check.schema bind

var personSchema = {
Expand All @@ -443,6 +450,24 @@ argument application
isValidPerson(h1); // true
isValidPerson(h2); // false

If you want you can manually bind `check.schema` to first argument

var personSchema = {
name: check.unemptyString,
age: check.positiveNumber
};
var isValidPerson = check.schema.bind(null, personSchema);
var h1 = {
name: 'joe',
age: 10
};
var h2 = {
name: 'ann'
// missing age property
};
isValidPerson(h1); // true
isValidPerson(h2); // false

You can use `Function.prototype.bind` or any partial application method, for example
`_.partial(check.schema, personSchema);`.
Because bound schema parameter generates a valid function, you can nest checks using
Expand Down
8 changes: 4 additions & 4 deletions check-more-types.js
Expand Up @@ -15,9 +15,9 @@
}

// utility method
function curry2(fn) {
function curry2(fn, strict2) {
return function curried(a) {
if (arguments.length > 2) {
if (strict2 && arguments.length > 2) {
throw new Error('Curry2 function ' + fn.name +
' called with too many arguments ' + arguments.length);
}
Expand Down Expand Up @@ -747,7 +747,7 @@
arrayOfStrings: arrayOfStrings,
arrayOfArraysOfStrings: arrayOfArraysOfStrings,
all: all,
schema: schema,
schema: curry2(schema),
raises: raises,
empty: empty,
found: found,
Expand All @@ -762,7 +762,7 @@
git: git,
arrayOf: arrayOf,
badItems: badItems,
oneOf: curry2(oneOf),
oneOf: curry2(oneOf, true),
promise: isPromise,
validDate: validDate,
equal: curry2(equal),
Expand Down
2 changes: 1 addition & 1 deletion check-more-types.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions docs/use.md
Expand Up @@ -318,6 +318,13 @@ check.badItems(check.unemptyString, ['foo', '', 'bar']); // ['']
This makes it very convenient to create new validator functions using partial
argument application

The method is curried, thus you can easily create predicate function

```js
var hasName = check.schema({ name: check.unemptyString });
hasName({ name: 'joe' }); // true
```

### check.schema bind

var personSchema = {
Expand All @@ -336,6 +343,24 @@ argument application
isValidPerson(h1); // true
isValidPerson(h2); // false

If you want you can manually bind `check.schema` to first argument

var personSchema = {
name: check.unemptyString,
age: check.positiveNumber
};
var isValidPerson = check.schema.bind(null, personSchema);
var h1 = {
name: 'joe',
age: 10
};
var h2 = {
name: 'ann'
// missing age property
};
isValidPerson(h1); // true
isValidPerson(h2); // false

You can use `Function.prototype.bind` or any partial application method, for example
`_.partial(check.schema, personSchema);`.
Because bound schema parameter generates a valid function, you can nest checks using
Expand Down
7 changes: 7 additions & 0 deletions test/unit-tests.js
Expand Up @@ -744,6 +744,13 @@ describe('check-more-types', function () {
describe('check.schema', function () {
la(check.fn(check.schema));

it('is curried', function () {
var hasName = check.schema({ name: check.unemptyString });
la(check.fn(hasName), 'returned a function');
la(hasName({ name: 'my name' }));
la(!hasName({ age: 42 }));
});

it('check.schema', function () {
var obj = {
foo: 'foo',
Expand Down

0 comments on commit 1b5444c

Please sign in to comment.