monadidator
v0.3.1
Published
Because validation can be expressed as a monad.
Downloads
4
Readme
Monadidator
Because validation can be expressed as a monad.
...but more likely because you want to have a validator that also allows you to modify the input as you are validating it.
Usage
const {is} = require('monadidator');
const pointValidator = is.object({
x: is.number().finite(),
y: is.number().finite(),
}).or(is.array([
is.number().finite(),
is.number().finite(),
]).map(([x, y]) => ({x, y})));
const lengthBetweenPoints = function (point1, point2) {
const {x: x1, y: y2} = pointValidator.run(point1, 'point1');
const {x: x2, y: y2} = pointValidator.run(point2, 'point2');
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
};
Docs
The validator API may be summed up as follows:
is.<type>(...)
is.<type>(...).<restriction>(...)
is.<type>(...).where(<sub-property>, validator)
is.<type>(...).where.<frequency>.<entry-type>(validator)
is.<type>(...).not.<restriction>(...)
is.<type>(...).not.where(<sub-property>, validator)
is.<type>(...).not.where.<frequency>.<entry-type>(validator)
Types
A <type>
validator is a primitive validator that validates that a value is of a given type, typically through using the JavaScript typeof
or instanceof
operators. All types are static functions that return an instance of a validator class specific to the type.
Restrictions
Restrictions are validators that allow one to restrict the range of valid inputs to a subset of the base type. Restrictions are type specific, i.e. which restrictions are available depends on the base type. Every restriction is a method on the class instance returned by the base type, and returns a new modified instance of the same class so that you may chain multiple restrictions together, e.g.:
const v = is.number().finite().greaterThan(0);
// v.run(NaN); // throws
// v.run(0); // throws
v.run(1); // ok
Restrictions may also be negated using the not
property. This property is a special Proxy
object that has all the same restrictions as the base type, but where every validator is negated, e.g.:
const v = is.number().finite().not.greaterThan(0);
// v.run(NaN); // throws
// v.run(1); // throws
v.run(0); // ok
Note that the not
property only negates the next validator in the chain, i.e. the validator to its immediate right. The not property will also negate itself. Note that the not
property is not a function, but rather an object. And perhaps confusingly, there also exist a not
method that functions as a single restriction. So for maximum silliness, behold this monstrosity:
const v = is.number().not.not.not(is.number().not.finite());
// v.run(NaN); // throws
v.run(1); // ok
Map
The map
method allow one to perform transformations on the value being validated. It functions similarly to the map
method on arrays, but instead of applying the transformation function on each element in the array, it applies the function to the value being validated, if the validator is successful.
For example:
const v = is.string().match(/^\d+$/).map(x => parseInt(x, 10));
// v.run(1); // throws
// v.run('lol'); // throws
v.run('123'); // ok => returns 123 (as a number)
Where
The where
method allows one to specify a validator for a given sub-property of a value. The given validator will then be run against the values sub-property. E.g.:
const v = is.string().where('length', is.number().greaterThan(1));
// v.run(''); // throws
v.run('lol'); // ok
Note that if you apply a transformation to the sub-property using the map
method, then the where
method will return a new object with the modified property. However, if the property is non-configurable then this is not valid and will instead throw an error. Furthermore, if the property is an object, this may subtly change the value, as the new value will be created using a plain object, i.e.: {...originalValue, [property]: resultOfWhere}
. Notably the object will loose its prototype, and any non-enumerable properties will be lost.
const v = is.string().where('length', is.number().map(x => x + 1));
// v.run(null); // throws, expects a string
// v.run(''); // throws, cannot assign to read only property 'length'
const v = is.object().where('x', is.number().map(x => x + 1));
// v.run(null); // throws, expects an object
// v.run({}); // expects, input.x to be a number
v.run({x: 1}); // ok, returns {x: 2}
v.run({x: 1, y: 2}); // ok, returns {x: 2, y: 2}
class SomeClass {}
const someInstance = new SomeClass();
someInstance.x = 1;
const v = is.object().where('x', is.number().map(x => x + 1));
v.run(someInstance); // ok, but returns {x: 2} (not instance of SomeClass)
Where iterators
There also exist some sub methods under where
, i.e.: the where.<frequency>.<entry-type>
pattern. These methods, however, are type specific and are only available for objects and arrays, and subclasses thereof. These methods function much like the where
method itself, but instead of declaring a validator for a single sub-property it declares a validator for a collection of sub-properties. E.g.:
const v = is.array().where.every.element(is.number());
v.run([1, 2, 3]); // ok
// v.run([1, 2, 3, 'lol']); // throws
Note that if the inner validator uses map
then these methods will return new objects similar to the where
method itself. But for each <entry-type>
only the properties of the given type will be preserved.
| <entry-type>
| Properties |
|:-------------------|:----------------------------|
| key
| enumerable + symbol |
| value
| enumerable + symbol |
| enumerable.key
| enumerable |
| enumerable.value
| enumerable |
| enum
| alias for enumerable
|
| symbol.key
| symbols |
| symbol.value
| symbols |
| element
| alias for element.value
|
| element.key
| Array elements |
| element.value
| Array elements |
| elem
| alias for element
|
const v = is.object().where.every.enumerable.value(is.number());
v.run({x: 1, y: 2, [Symbol('test')]: 3}) // ok, but returns {x: 1, y: 2} and
// the symbol property is neither
// validated nor returned.
API
is : object
Main object containing all type validators.
- is : object
- .{any,anything}() ⇒
AnyValidator
- .array([tmpl]) ⇒
ArrayValidator
- .boolean() ⇒
BooleanValidator
- .date() ⇒
DateValidator
- .function() ⇒
FunctionValidator
- .null() ⇒
NullValidator
- .number() ⇒
NumberValidator
- .object([tmpl]) ⇒
ObjectValidator
- .string() ⇒
StringValidator
- .undefined() ⇒
UndefinedValidator
- .url([baseUrl]) ⇒
UrlValidator
- .{any,anything}() ⇒
is.{any,anything}() ⇒ AnyValidator
A function that returns a new validator that always succeeds.
is.array([tmpl]) ⇒ ArrayValidator
A function that returns a new validator that succeeds if the given input is
an array, and fails otherwise, i.e. if the input satisfies the condition
Array.isArray(input)
.
You may also pass a template, i.e.: an array of validators that will run each validator in the template, against their corresponding value in the array that is being validated according to their index. Note that when using a template array a new array is yielded as the result containing only those indexed elements present in the template array.
[tmpl]
(Array)
Example
const v is.array([is.number().finite(), is.number().finite()]);
// v.run([1, 'lol']); // throws
v.run([1, 2]); // ok
is.boolean() ⇒ BooleanValidator
A function that returns a new validator that succeeds if the given input is
a boolean, and fails otherwise, i.e. if the input satisfies the condition
typeof input === 'boolean'
.
is.date() ⇒ DateValidator
A function that returns a new validator that succeeds if the given input is
a date, and fails otherwise, i.e. if the input satisfies the condition
input instanceof Date
.
is.function() ⇒ FunctionValidator
A function that returns a new validator that succeeds if the given input is
a function, and fails otherwise, i.e. if the input satisfies the condition
typeof input === 'function'
.
is.null() ⇒ NullValidator
A function that returns a new validator that succeeds if the given input is
null, and fails otherwise, i.e. if the input satisfies the condition
input === null
.
is.number() ⇒ NumberValidator
A function that returns a new validator that succeeds if the given input is
a number, and fails otherwise, i.e. if the input satisfies the condition
typeof input === 'number'
.
is.object([tmpl]) ⇒ ObjectValidator
A function that returns a new validator that succeeds if the given input is
an object, and fails otherwise, i.e. if the input satisfies the condition
input !== null && typeof input === 'object'
. Note that unlike the normal
JavaScript typeof
operator Monadidator does not interpret null
as an
object.
You may also pass a template, i.e.: an object of validators that will run each validator in the template, against their corresponding value in the input that is being validated according to their key. Note that when using a template object a new object is yielded as the result containing only those properties present in the template object.
[tmpl]
(Object)
Example
const v = is.object({x: is.number().finite(), y: is.number().finite()});
// v.run({x: 1, y: 'lol'}); // throws
v.run({x: 1, y: 2}); // ok
is.string() ⇒ StringValidator
A function that returns a new validator that succeeds if the given input is
a string, and fails otherwise, i.e. if the input satisfies the condition
typeof input === 'string'
.
is.undefined() ⇒ UndefinedValidator
A function that returns a new validator that succeeds if the given input is
undefined, and fails otherwise, i.e. if the input satisfies the condition
typeof input === 'undefined'
.
is.url([baseUrl]) ⇒ UrlValidator
A function that returns a new validator that succeeds if the given input is
a URL, and fails otherwise, i.e. if the input satisfies the condition
input instanceof URL
. Alternatively it may be a string that is coercible to
a URL trough new URL
, although this is most useful when combined with the
baseUrl parameter.
[baseUrl]
(string | URL)
Monadidator
- Monadidator
- instance
- .map(f) ⇒
Monadidator
- .{or,concat}(validator) ⇒
Monadidator
- .{and,then}(validator) ⇒
Monadidator
- .not(validator) ⇒
Monadidator
- .where(property, validator) ⇒
Monadidator
- .{satisfy,satisfies,satisfying}(predicate) ⇒
Monadidator
- .label(msg) ⇒
Monadidator
- .{run,validate}(input, [name], [options]) ⇒ *
- .{asyncRun,asyncValidate}(input, [name], [options]) ⇒ Promise.<*>
- .map(f) ⇒
- static
- .mkType(Cls, options) ⇒ function
- instance
Monadidator.map(f) ⇒ Monadidator
A method that returns a new validator that behaves exactly like this
validator, but if this
validator succeeds it applies the given function
to the result and yields the transformed result.
Note that the map
function will always return a validator of the same
type as before the transformation, such that you may continue to chain
methods as normal. However, this does not work if the transformation
function turns the value into a different type, then you need to explicitly
and
(or then
) the result with the new type in order to continue
chaining methods.
f
(function)
Example
is.string()
.map(x => x.trim()) // transform from string to string
.matching(/^\d+$/)
.map(x => parseInt(x, 10)) // transform from string to number
.then(is
.number()
.between(0, 100));
Monadidator.{or,concat}(validator) ⇒ Monadidator
A method that returns a new validator that first tries to validate using
this
validator, but if this
fails tries to run the given validator
.
Succeeds if either validator succeeds and fails otherwise.
validator
(Monadidator
)
Monadidator.{and,then}(validator) ⇒ Monadidator
A method that returns a new validator that first applies this
validator
and then applies the given validator
. Succeeds only if both validators
succeeds and fails otherwise.
validator
(Monadidator
)
Monadidator.not(validator) ⇒ Monadidator
A type-restriction method that succeeds if the given validator
fails, and
fails otherwise. NOTE that this is not actually a function, but rather a
special Proxy
object that behaves like both a function and an object.
When used as an object it operates like a special negated version of
this
. I.e. when used as an object it will have all the same
type-restrictions as are available on this
, but where every
type-restriction is negated.
When used as a function it will simply negate the given validator
. NOTE
that this may not behave exactly like you might initially expect, for
example: the validator is.any().not(is.number().lt(0))
is not equal to
the validator is.number().not.lt(0)
! For example, when run on an input
value of type string, this value will be ok when run against the first
validator, but not the second. As the first will negate the whole validator
whereas the second will only negate the type-restriction lt
. Thus for the
first the input is strictly not (a number less than 0)
and will therefore
be ok, and for the second the number must be a number && not less then 0
.
validator
(Monadidator
)
Example
const v1 = is.any().not(is.number().lt(0));
const v2 = is.number().not.lt(0);
v1.run('lol'); // ok
// v1.run(-1); // throws
v1.run(1); // ok
// v2.run('lol'); // throws
// v2.run(-1); // throws
v2.run(1); // ok
Monadidator.where(property, validator) ⇒ Monadidator
A type-restriction method that succeeds if the current input has the
sub-property given by property
, and the given validator
succeeds when
run against the sub-property, and fails otherwise.
property
(*)validator
(Monadidator
)
Monadidator.{satisfy,satisfies,satisfying}(predicate) ⇒ Monadidator
A type-restriction method that succeeds if the given predicate function returns true for the current input, and fails otherwise.
predicate
(function)
Monadidator.label(msg) ⇒ Monadidator
A method that returns a new validator that behaves exactly like this
validator, but if this
validator fails it replaces the expected error
message with the expected error msg
.
msg
(string)
Monadidator.{run,validate}(input, [name], [options]) ⇒ *
Execute a validator, i.e. run the validator against the given input
.
Returns a transformed version of the input, if the validator contains any
transformations, i.e. if the validator uses the map
method, when the
validator is successful and throws an error otherwise.
input
(*)[name]
(string) = "'input'"[options]
(Object)[.format]
(string) = "'text'"[.ErrorClass]
(function) = TypeError
Example
const v = is.string().match(/^\d+$/).map(x => parseInt(x, 10));
// v.run(123); // => throws, expects a string
// v.run('lol'); // => throws, expects a string matching /^\d+$/
v.run('123'); // => ok, returns 123 (number)
Monadidator.{asyncRun,asyncValidate}(input, [name], [options]) ⇒ Promise.<*>
Async version of run
that automatically awaits any asynchronous
transformations, ie. uses of the map
method that return promises.
input
(*)[name]
(string) = "'input'"[options]
(Object)[.format]
(string) = "'text'"[.ErrorClass]
(function) = TypeError
Monadidator.mkType(Cls, options) ⇒ function
Helper function for creating type validators. Each type validator returns an instance of some sub-class of Monadidator that contains all the type-restriction methods that should be available for the type.
Most notably this function will setup all the prerequisites for the not
proxy object to function correctly, as well as the where
methods where
applicable.
Cls
(function) - The class you want to turn into a type validator.options
(Object) - Options object..name
(string) - The name of the type to be used in the error output..predicate
(function) - A predicate function that determines if the given value is of this type.[.clone]
(function) - A function that determines how to clone a value of the given type. This is required for thewhere
method to function correctly.[.empty]
(function) - A function that returns an empty value of the given type. This is required for thewhere.every
andwhere.some
methods to function correctly.
- returns (function) - A function that returns a validator.
AnyValidator ⇐ Monadidator
Extends: Monadidator
AnyValidator.nullish() ⇒ AnyValidator
A type-restriction method that succeeds if the given input is "nullish",
and fails otherwise, i.e. if the input satisfies the condition
input == null
.
AnyValidator.truthy() ⇒ AnyValidator
A type-restriction method that succeeds if the given input is "truthy",
and fails otherwise, i.e. if the input satisfies the condition
Boolean(input)
.
AnyValidator.falsy() ⇒ AnyValidator
A type-restriction method that succeeds if the given input is "falsy",
and fails otherwise, i.e. if the input satisfies the condition
!input
.
ArrayValidator ⇐ ObjectValidator
Extends: ObjectValidator
ArrayValidator.where(property, validator) ⇒ ArrayValidator
Inherited ObjectValidator.where
.
property
(*)validator
(ArrayValidator
)
where.every : object
Inherited ObjectValidator.where.every
.
- .every : object
every.{elem,element}(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all values in the input array, and fails otherwise. Note
that if the input has no values then this will always succeed.
validator
(ArrayValidator
)
{elem,element}.key(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all keys in the input array, and fails otherwise. Note
that if the input has no keys then this will always succeed.
validator
(ArrayValidator
)
{elem,element}.value(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all values in the input array, and fails otherwise. Note
that if the input has no values then this will always succeed.
validator
(ArrayValidator
)
where.some : object
Inherited ObjectValidator.where.some
.
- .some : object
some.{elem,element}(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one value in the input array, and fails otherwise. Note that
if the input has no values then this will always fail.
validator
(ArrayValidator
)
{elem,element}.key(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one key in the input array, and fails otherwise. Note that
if the input has no keys then this will always fail.
validator
(ArrayValidator
)
{elem,element}.element(validator) ⇒ ArrayValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one value in the input array, and fails otherwise. Note that
if the input has no values then this will always fail.
validator
(ArrayValidator
)
BooleanValidator ⇐ Monadidator
Extends: Monadidator
BooleanValidator.true() ⇒ BooleanValidator
A type-restriction method that succeeds if the given input is true
, and
fails otherwise, i.e. if the input satisfies the condition
input === true
.
BooleanValidator.false() ⇒ BooleanValidator
A type-restriction method that succeeds if the given input is false
, and
fails otherwise, i.e. if the input satisfies the condition
input === false
.
DateValidator ⇐ ObjectValidator
Extends: ObjectValidator
- DateValidator ⇐
ObjectValidator
- .valid() ⇒
DateValidator
- .invalid() ⇒
DateValidator
- .{gt,greater,greaterThan}() ⇒
DateValidator
- .{gte,greaterOrEqual,greaterOrEquals,greaterOrEqualTo,greaterThanOrEqual,greaterThanOrEquals,greaterThanOrEqualTo}() ⇒
DateValidator
- .{eq,equal,equals,equalTo}() ⇒
DateValidator
- .{ne,notEqual,notEquals,notEqualTo}() ⇒
DateValidator
- .{lte,lessOrEqual,lessOrEquals,lessThanOrEqual,lessThanOrEquals,lessThanOrEqualTo}() ⇒
DateValidator
- .{lt,less,lessThan}() ⇒
DateValidator
- .between(min, max, [inclusivity]) ⇒
DateValidator
- .valid() ⇒
DateValidator.valid() ⇒ DateValidator
A type-restriction method that succeeds if the given input is a valid date,
and fails otherwise, i.e. if the input satisfies the condition
Number.isFinite(input.valueOf())
.
DateValidator.invalid() ⇒ DateValidator
A type-restriction method that succeeds if the given input is a an invalid
date, and fails otherwise, i.e. if the input satisfies the condition
!Number.isFinite(input.valueOf())
.
DateValidator.{gt,greater,greaterThan}() ⇒ DateValidator
A type-restriction method that succeeds if the input is greater than the
given date
, and fails otherwise, i.e. if the input satisfies the
condition input.valueOf() > date.valueOf()
.
DateValidator.{gte,greaterOrEqual,greaterOrEquals,greaterOrEqualTo,greaterThanOrEqual,greaterThanOrEquals,greaterThanOrEqualTo}() ⇒ DateValidator
A type-restriction method that succeeds if the input is greater than or
equal to the given date
, and fails otherwise, i.e. if the input satisfies
the condition input.valueOf() >= date.valueOf()
.
DateValidator.{eq,equal,equals,equalTo}() ⇒ DateValidator
A type-restriction method that succeeds if the input is equal to the given
date
, and fails otherwise, i.e. if the input satisfies the condition
input.valueOf() === date.valueOf()
.
DateValidator.{ne,notEqual,notEquals,notEqualTo}() ⇒ DateValidator
A type-restriction method that succeeds if the input is not equal to the
given date
, and fails otherwise, i.e. if the input satisfies the
condition input.valueOf() !== date.valueOf()
.
DateValidator.{lte,lessOrEqual,lessOrEquals,lessThanOrEqual,lessThanOrEquals,lessThanOrEqualTo}() ⇒ DateValidator
A type-restriction method that succeeds if the input is less than or equal
to the given date
, and fails otherwise, i.e. if the input satisfies the
condition input.valueOf() <= date.valueOf()
.
DateValidator.{lt,less,lessThan}() ⇒ DateValidator
A type-restriction method that succeeds if the input is less than the given
date
, and fails otherwise, i.e. if the input satisfies the condition
input.valueOf() < date.valueOf()
.
DateValidator.between(min, max, [inclusivity]) ⇒ DateValidator
A type-restriction method that succeeds if the given input is between the
given dates min
and max
, and fails otherwise.
The min
and max
parameters are not strictly ordered, so if min
are
larger than max
then the inputs are swapped. Furthermore the
inclusivity
controls if the range includes the min
and max
values,
i.e.:
| Inclusivity| **Condition** |
|:-----------------|:--------------------------------|
|
'()' |
min < input && input < max|
|
'(]' |
min < input && input <= max|
|
'[)' |
min <= input && input < max|
|
'[]' |
min <= input && input <= max` |
min
(Date)max
(Date)[inclusivity]
(String) = '[]'
FunctionValidator ⇐ ObjectValidator
Extends: ObjectValidator
NullValidator ⇐ Monadidator
Extends: Monadidator
NumberValidator ⇐ Monadidator
Extends: Monadidator
- NumberValidator ⇐
Monadidator
- .{nan,NaN}() ⇒
NumberValidator
- .finite() ⇒
NumberValidator
- .{gt,greater,greaterThan}(n) ⇒
NumberValidator
- .{gte,greaterOrEqual,greaterOrEquals,greaterOrEqualTo,greaterThanOrEqual,greaterThanOrEquals,greaterThanOrEqualTo}(n) ⇒
NumberValidator
- .{eq,equal,equals,equalTo}(n) ⇒
NumberValidator
- .{ne,notEqual,notEquals,notEqualTo}(n) ⇒
NumberValidator
- .{lte,lessOrEqual,lessOrEquals,lessThanOrEqual,lessThanOrEquals,lessThanOrEqualTo}(n) ⇒
NumberValidator
- .{lt,less,lessThan}(n) ⇒
NumberValidator
- .between(min, max, [inclusivity]) ⇒
NumberValidator
- .{in,elem,elemOf,element,elementOf}(ns) ⇒
NumberValidator
- .{nan,NaN}() ⇒
NumberValidator.{nan,NaN}() ⇒ NumberValidator
A type-restriction method that succeeds if the given input is NaN
, and
fails otherwise, i.e. if the input satisfies the condition
Number.isNaN(input)
.
NumberValidator.finite() ⇒ NumberValidator
A type-restriction method that succeeds if the given input is finite, and
fails otherwise, i.e. if the input satisfies the condition
Number.isFinite(input)
.
NumberValidator.{gt,greater,greaterThan}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is greater than
the given number n
, and fails otherwise, i.e. if the input satisfies the
condition input > n
.
n
(Number)
NumberValidator.{gte,greaterOrEqual,greaterOrEquals,greaterOrEqualTo,greaterThanOrEqual,greaterThanOrEquals,greaterThanOrEqualTo}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is greater than
or equal to the given number n
, and fails otherwise, i.e. if the input
satisfies the condition input >= n
.
n
(Number)
NumberValidator.{eq,equal,equals,equalTo}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is equal to the
given number n
, and fails otherwise, i.e. if the input satisfies the
condition input === n
.
n
(Number)
NumberValidator.{ne,notEqual,notEquals,notEqualTo}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is not equal to
the given number n
, and fails otherwise, i.e. if the input satisfies the
condition input !== n
.
n
(Number)
NumberValidator.{lte,lessOrEqual,lessOrEquals,lessThanOrEqual,lessThanOrEquals,lessThanOrEqualTo}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is less then or
equal to the given number n
, and fails otherwise, i.e. if the input
satisfies the condition input <= n
.
n
(Number)
NumberValidator.{lt,less,lessThan}(n) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is less then the
given number n
, and fails otherwise, i.e. if the input satisfies the
condition input < n
.
n
(Number)
NumberValidator.between(min, max, [inclusivity]) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is between the
given numbers min
and max
, and fails otherwise.
The min
and max
parameters are not strictly ordered, so if min
are
larger than max
then the inputs are swapped. Furthermore the
inclusivity
controls if the range includes the min
and max
values,
i.e.:
| Inclusivity| **Condition** |
|:-----------------|:--------------------------------|
|
'()' |
min < input && input < max|
|
'(]' |
min < input && input <= max|
|
'[)' |
min <= input && input < max|
|
'[]' |
min <= input && input <= max` |
min
(Number)max
(Number)[inclusivity]
(String) = '[]'
NumberValidator.{in,elem,elemOf,element,elementOf}(ns) ⇒ NumberValidator
A type-restriction method that succeeds if the given input is in the
given list of numbers ns
, and fails otherwise, i.e. if the input
satisfies the condition ns.includes(input)
.
ns
(Array.<Number>)
ObjectValidator ⇐ Monadidator
Extends: Monadidator
- ObjectValidator ⇐
Monadidator
ObjectValidator.{instance,instanceof,instanceOf}() ⇒ ObjectValidator
A type-restriction method that succeeds if the input is an instance of the
given class Cls
, and fails otherwise, i.e. if the input satisfies the
condition input instanceof Cls
.
ObjectValidator.where(property, validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the current input has the
sub-property given by property
, and the given validator
succeeds when
run against the sub-property, and fails otherwise. Note that if the given
validator
uses map
, then a new object is yielded as the result.
property
(*)validator
(ObjectValidator
)
- .where(property, validator) ⇒
ObjectValidator
where.every : object
ObjectValidator.where.every
- .every : object
every.{enum,enumerable} : object
"{enum,enumerable}"
{enum,enumerable}.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all enumerable keys in the input object, and fails
otherwise. Note that if the input has no enumerable keys then this will
always succeed.
validator
(ObjectValidator
)
{enum,enumerable}.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all enumerable values in the input object, and fails
otherwise. Note that if the input has no enumerable values then this will
always succeed.
validator
(ObjectValidator
)
every.symbol : object
ObjectValidator.where.every.symbol
symbol.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all symbol keys in the input object, and fails
otherwise. Note that if the input has no symbol keys then this will
always succeed.
validator
(ObjectValidator
)
symbol.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all symbol values in the input object, and fails
otherwise. Note that if the input has no symbol values then this will
always succeed.
validator
(ObjectValidator
)
every.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all keys (both enumerable and symbols) in the input
object, and fails otherwise. Note that if the input has no keys then this
will always succeed.
validator
(ObjectValidator
)
every.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
when run against all values (both enumerable and symbols) in the input
object, and fails otherwise. Note that if the input has no values then
this will always succeed.
validator
(ObjectValidator
)
where.some : object
ObjectValidator.where.some
- .some : object
some.{enum,enumerable} : object
"{enum,enumerable}"
{enum,enumerable}.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one enumerable key, in the input object, and fails
otherwise. Note that if the input has no enumerable keys then this will
always fail.
validator
(ObjectValidator
)
{enum,enumerable}.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one enumerable value, in the input object, and fails
otherwise. Note that if the input has no enumerable values then this will
always fail.
validator
(ObjectValidator
)
some.symbol : object
ObjectValidator.where.some.symbol
symbol.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one symbol key, in the input object, and fails otherwise.
Note that if the input has no symbol keys then this will always fail.
validator
(ObjectValidator
)
symbol.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one symbol value, in the input object, and fails otherwise.
Note that if the input has no symbol values then this will always fail.
validator
(ObjectValidator
)
some.key(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one key (either enumerable or symbol), in the input object,
and fails otherwise. Note that if the input has no keys then this will
always fail.
validator
(ObjectValidator
)
some.value(validator) ⇒ ObjectValidator
A type-restriction method that succeeds if the given validator
succeeds
for at least one value (either enumerable or symbol), in the input
object, and fails otherwise. Note that if the input has no values then
this will always fail.
validator
(ObjectValidator
)
StringValidator ⇐ Monadidator
Extends: Monadidator
StringValidator.empty() ⇒ StringValidator
A type-restriction method that succeeds if the given input is empty, and
fails otherwise, i.e. if the input satisfies the condition
input.length === 0
.
StringValidator.{match,matches,matching}() ⇒ StringValidator
A type-restriction method that succeeds if the given input is matches the
given regular expression r
, and fails otherwise, i.e. if the input
satisfies the condition r.test(input)
.
StringValidator.{contain,contains,containing}() ⇒ StringValidator
A type-restriction method that succeeds if the given input contains the
given substring str
, and fails otherwise, i.e. if the input satisfies the
condition input.indexOf(str) !== -1
.
StringValidator.{eq,equal,equals,equalTo}() ⇒ StringValidator
A type-restriction method that succeeds if the given input is equal to the
given string str
, and fails otherwise, i.e. if the input satisfies the
condition input === str
.
StringValidator.{ne,notEqual,notEquals,notEqualTo}() ⇒ StringValidator
A type-restriction method that succeeds if the given input is not equal to
the given string str
, and fails otherwise, i.e. if the input satisfies
the condition input !== str
.
StringValidator.{in,elem,elemOf,element,elementOf}() ⇒ StringValidator
A type-restriction method that succeeds if the given input is in the
supplied list of strings xs
, and fails otherwise, i.e. if the input
satisfies the condition xs.includes(input)
.
UndefinedValidator ⇐ Monadidator
Extends: Monadidator
UrlValidator ⇐ Monadidator
Extends: Monadidator