Validation and conversion library
The cast.js library was written to provide convenient methods for validating and converting object properties for model based frameworks like backbone.js. The core provides several features to make converting, validating, and error handling easier:
- Convert object properties in place (by passing an object and property name)
- Validate object properties without converting (by passing a value only)
- Handle various data states at any point (blank, valid, invalid, out of range)
The syntax was designed to be human readable. See the Module API for complete documentation of built in modules.
castobj 'propName'asadataType // converts value in placecastvalueisadataTypevalid // returns true if value is valid for that data type// Note: is/as/a/an are optional.
Here are some real-world examples:
castattrs 'start_date'datebeginningOfDaycastattrs 'email'emailif castageintegeratLeast13valid// do something
if method allows you run callback functions based on the current state (blank, valid, out of range, or invalid). Usually this is done at the end of a method chain, but it can be done any time after the initial data type is cast.
castattrs 'foo'asanintegeratLeast1noMoreThanbarifshowError'foo' 'foo is required'console.log'foo is valid'showError'foo' 'foo must be at least 1 and no more than ' + bar + '.'showError'foo' 'foo is not a whole number.'
Note: States are evaulated in the order shown here. If a given state is true and a handler was provided, only the handler for that state will be called. If a state is true, but no handler was provided, the next state will be evaluated.
You can add you own custom modules using the
cast.register method. All modules must provide an
initialize method at the very least.
cast.register can be called in two ways:
castregister'myModule' // For simple modules with no chained methodscastregister // For multiple modules or modules with chainingmyModule:// initialize is called by cast(value).myModule()
Note: an unregister method is also provided for testing purposes:
cast.unregister('moduleOne', 'ModuleTwo', ...)
You can add aliases for you modules or module methods by including the alias/method mapping in your module definition:
castregistermoduleName:methodAlias: 'methodName'moduleAlias: 'moduleName'
All of the methods in you module will change the state of the cast in some way or another. There are three built in helpers:
rangeError. Here is an example of how they could be used:
castregister'positiveInteger'x = parseIntx 10if isNaNx return thisfailif x <= 0 return thisrangeErrorthissetx
Custom modules have access to a handfull of useful helpers for parsing data. You can use these from within you module methods by calling
xto a the same data type as the module by running it's initialize function and returning the value.
xto a string if it can, or falls back to an empty string.
xas a number, optionally to
ddecimal places or returns
realTypeof(x)returns the actual type of
x. Includes support for arrays and date objects.
trimString(x)removes the leading and trailing whitespace from single line strings. Multi-line strings are not changed.
isBlank(x)returns true if
or an empty string.
isValid(x)returns false if