Define rules by which a single parameter can be validated and revised to contain the desired value. Has built-in checks to remove common undesirable values, check value type, etc and hooks where additional methods can easily be added to further clean up values.
Options is an object which defines properties that will define how an expectation will behave. These properties may also be set on Expectation to define your own default values.
Defines what it is known by. Useful when assigning the resulting values to objects, maps, etc.
Should empty collections (strings, arrays, maps, sets, buffers) be allowed. In addition to these well known collection types any iterator object with a length or size property will be considered a colection.
Should error values be allowed. If undefined and either the defaultValue or requiredType is an error then will be true.
Should function values be allowed. If undefined and either the defaultValue or requiredType is a function then will be true.
Should arrays of values be allowed.
Should NaN be allowed.
Should null be allowed.
Should symbol values be allowed. If undefined and either the defaultValue or requiredType is a symbol then will be true.
Value that will be returned if given value fails to meet any of the criteria.
Should the return value be an array whenever possible? Only considered if acceptMultiples is true.
If true, will push any indidivual values into an array. If acceptEmpty is true will return an empty array instead of the default value.
If false, empty arrays will instead return the default value and arrays with a single member will return just that indidivual.
If undefined, doesn't do anything.
If true and given value fails to meet any of the criteria then will throw an error instead of returning a default value.
A class/prototype function or a string to compare the values type against.
If a function then will check if value is an instanceof that function.
If a string then will check if typeof value equals string.
If typeof doesn't match then will attemp to convert the value using a matching implicit conversion function.
If undefined then any value is fine.
A function ( value, expectation ) => value that gets a copy of the working value after the build-in methods have had their turn evaluating and revising it but before the default value is applied. Whatever value returned from this function will then be checked and the default value applied if needed or error thrown.
An object with implicit conversion functions ( value ) => value as members. If an implicit conversion operation is being done then the string name of the required type will be used to lookup a matching function. If that function exists then it will be passed the value and whatever value it returns will be the new working value.
If a matching function isn't found on the instance then it will also be looked for in Expectation.implicitConverters.
Expectation.create(options) => expectation
Create a new expectation instance with the given options object to set the criteria. Returns the new expectation.
Alternatively, options can be either: a two element array with default name and value, or a string to use as the default name.
Expectation.createList([options1,options2,..optionsN]) => [expectation1,expectation2,..expectationN]
Create an array of expectations from option objects passed as criteria.
The expectation's name. Where appropiate this value should be used in naming revised values.
revise(value) => value
Validates and revises a value against an expectation's criteria. Returns the revised value.
initialize() => value
Produces an initial value based on the expectation's criteria. Ignores the required rule that otherwise might throw an error.
included implicit convertors
If it's null, NaN, or undefined return undefined. Otherwise return a string.
Same as string but returns in locale uppercase.
Same as string but returns in locale lowercase.
If it's undefined return undefined. Otherwise return as JSON.
If it's a Date or can be parsed into a Date from a string or number then return as a Date. Otherwise return undefined.
Same as date but returns in locale date format.
Same as date but returns in locale time format.
Same as date but returns in iso8601 format.
If it's a number or can be parsed into a number return as a number. Otherwise return NaN.
If it's an integer or can be parsed into an integer return as an integer. Otherwise return NaN.
If it's a boolean or can be parsed into a boolean return as a boolean. Otherwise return undefined.
Try to parse into a byte value. If it's a number 0-255 then return that value as an integer. If it's a single char string and the char code is 0-255 then return that value. Otherwise return undefined.
If it parses to a finite number then return that value. Otherwise return undefined.
If it's a Url object or can be parsed to a Url object then return a Url. Otherwise return undefined.
If it's a emailaddress object or can be parsed into a emailaddress object then will return a string representation of the email addresses. If optional emailaddress dependency isn't installed then just treats it as string.
If it's a number it is treated as epochs. If it is a date or can be parsed as a date then will be output as epochs.
Requires Node 4 with harmony features or Traceur.