Mithril Validator
Easily validate Mithril.js models, and objects.
Install
- Download the latest package
- NPM:
npm install mithril-validator
Setup
var m = m
Documentation
new m.validator(validators) -> Validator
Validates mithril models and objects through validation functions mapped to specific model properties.
Example
// Our mithril modelvar { thisname = m thisdone = m} // Initialize a new validatorvar validator = // Check model name property { if !name return "Name is required." } // Results in "Name is required."validator
validator.hasErrors() -> Boolean
Returns length of error mapping
if validator // do something
validator.hasError(key) -> Mixed
Returns the element associated with the specified key from the error mapping
validator.clearErrors() -> void
Removes all of the elements from the error list.
// Results in "Name is required."validator validator // Results in undefinedvalidator
validator.validate(model) -> Validator
Validates the specified model against the validations mapping in this instance.
Each (shallow) property is iterated over and cross-checked against the given model for value, then the validation function is invoked passing the model as context and value as the first argument.
On a truthy result from a validation function the result is placed on the error object with the property name as the identifier.
validator
Note Missing properties are treated as undefined
and do not throw errors, you should do this yourself
within the property validator.
License
Licensed under The MIT License.