skemer v0.8.6-r1
skemer 0.8.6
A Javascript variable validation and merge tool.
This library can be used to ensure a variable and any additions to that variable adhere to a certain schema. The schema can be as simple as allowing a string value, to as complex as a nested Object.
The library contains the validateNew function for validating
and merging all new data, the validateAdd function for doing
validating and merging new data into existing data, a Skemer
prototype for doing multiple validations / merges against the same schema,
and a buildJsDocs function for creating
a JSDoc comment string from the schema and its doc parameters.
Uses
- Validating static data during testing
- Validating dynamic data during runtime
Example
var skemer = require('skemer');
var schema = {
doc: 'A basic schema',
type: {
value: {
doc: 'Some string value',
type: 'string'
},
figure: {
doc: 'A number value',
type: 'number',
min: 20,
max: 50
}
}
};
var valid = {
value: 'a string',
figure: 30
};
var valid1 = {
figure: 35
};
var valid2 = {
value: 'a different string'
};
var invalid = false;
var stringSchema = {
type: 'string'
};
var aString = 'string';
skemer.validateNew({ schema: stringSchema }, aString);
var Schema, data;
console.log(data = skemer.buildJsDocs(schema, {
wrap: 80,
preLine: ' * ',
lineup: true
}));
Schema = new skemer.Skemer({ schema: schema });
console.log(data = Schema.validateNew(valid));
console.log(data = Schema.validateAdd(data, valid1));
Schema.validateAdd(data, valid2, invalid);Skemer API
buildJsDocs
Build a JSDoc for a variable using the given schema.
Parameters
schemaObject An Object containing a validschemaoptionsObject An Object containing build optionsoptions.namestring Name of the object documenting (will be prepended to any parameter namesoptions.typestring Specify what block tag should be used for the variables (optional, default'prop')options.tabWidthnumber The width (number of characters) of a tab (optional, default8)options.preLinestring String (normally indentation) to include before each lineoptions.lineupboolean Whether to line up text in a JSDoc block (eg@param) with the end of the block command (optional, defaulttrue)options.wrapnumber Number of characters to wrap the JSDoc lines at
Returns string A string containing the JSDoc for the given
schema
Skemer
Skemer prototype to enable simple reuse of a schema
Parameters
validateAdd
Add new data to existing validated data based on the stored schema. NOTE: Existing data WILL NOT be validated
Parameters
dataAny Existing data to merge new data into.newData...Any Data to validate and merge into the existing data.
Returns Any Validated and merged data
validateNew
Validate and merge new data based on the stored schema.
Parameters
newData...Any Data to validate, merge and return. If no data is given, a variable containing any default values, if configured, will be returned.
Returns Any Validated and merged data
validateAdd
Validata and add new data to existing validated data based on the given schema. NOTE: Existing data WILL NOT be validated
Parameters
optionsObject An object containing the validationoptions, including theschemadataAny Data to validate and return. If no data is given, data containing any default values will be returned. If newData is given, newData will be validated and merged into data.newData...Any Data to validate and merge intodata
Returns Any Validated and merged data
validateNew
Validate and merge new data based on the given schema.
Parameters
optionsObject An object containing the validationoptions, including theschemanewData...Any Data to validate, merge and return
Returns Any Validated and merged data
Schema and Validate Options
options
Options Object that must be passed to the one-off
validate functions and
on creating an instance of a Skemer
Parameters
schemaschemaschemato use for the validationbaseSchemaschemaschemato be used for recursive schemas. If none given, the given, the full schema given inschemawill be usedreplaceboolean or Array<boolean> A boolean to specify whether to globally replace all existing values for arrays and objects, or an object of string/boolean key/value pairs used to specify what variables(their name given as the key) should have their value replaced by default (a boolean value of true)
schema
Schema Object detailing the schema to be used for validating and merging data.
Parameters
docstring A String giving information on the value expecteddocTypestring A string containing the type of the value expected that will be used instead of calculating the type of value expectednoDocDigboolean If set and the value expected is an object, buildJsDoc will not document the parameters of the objecttypestring or or Array<schema> The value type of the parameter expectedtypesArray<schema> An Array or Object ofschemacontaining different schemas of the values expectedvaluesArray<Any> Specifies the possible values for strings, numbers and datesmultipleboolean Whether or not multiple values (stored in an Array, or ifobject is set totrue` an Object) are allowed. Can be a boolean, a number (the number of values that the value expected must have), or an array containing the minimum number of values and, optionally, the maximum number of values.objectboolean Ifmultipleis true andobjectis true, the multiple values will be stored in an object. If multiple is true and object is false, any keys will be ignored and the values will be stored in an arrayregexRegExp A regular expression to validate a String valueminnumber or date The minimum number, string length or number of Array elements requiredmaxnumber or date The maximum number, string length or number of Array elements allowedreplaceboolean Whether a new value should completely replace an old value when the value expected is either an array or an objectrequiredboolean or Function or number or Array<number> Either true/false or a function returning true/false whether the parameter is required, or if the variable is a multiple stored in an array an number given the number of required elements, or an array of numbers, the first being the minimum number of elements and the second being the maximum number of elements (a maximum is not required)defaultAny Default value to use if no value is given
Skemer Errors
The Skemer module will throw the following Errors if any errors in the Schema or the variables are found
DataInvalidError
Thrown if the parameter value is not valid
Parameters
messagestring Error messageextraAny Extra information
DataItemsError
Thrown if the parameter value is out of the given range
Parameters
messagestring Error messageextraAny Extra information
DataRangeError
Thrown if the parameter value is out of the given range
Parameters
messagestring Error messageextraAny Extra information
DataRequiredError
Thrown if a parameter is required, but was not given
Parameters
messagestring Error messageextraAny Extra information
DataTypeError
Thrown if the type of value for a parameter in the schema is incorrect,
Parameters
messagestring Error messageextraAny Extra information
OptionsError
Thrown if the parameter value is out of the given range
Parameters
messagestring Error messageextraAny Extra information
SchemaError
Thrown if the parameter value is out of the given range
Parameters
messagestring Error messageextraAny Extra information