Installation
Straints can be installed through npm.
To add straints to your project via npm do
$ npm install straints --save
To use in the browser link in one of the js files.
<script src=".../straints.min.js"></script>
Alternatively, you can use the unpkg CDN.
<script src="https://unpkg.com/straints/dist/straints.min.js"></script>
Once you've got the script in your page, you can do
<script>
var instance = straints({ /* config options */ });
</script>
Configuration Options
Here's a list of the available configuration options.
-
validator
(object)
Specify the test method implementation(s) to use here. (As of v0.7.0 straints no longer comes with a default validator method implementation) -
schema
(object)
Validation schema definition for the straints instance. -
contexts
(object)
Default validation context(s) to use if none passed to the straints instance. levels
(string|array)
Additional validation levels can be specified here with an array or comma-delimited string.
Validators
As far as straints is concerned, a validator is simply an object with validation methods (or test methods).
var instance = straints({ validator: myTestMethods });
// OR, if using multiple validator libraries
var instance = straints({ validator: { alyze: alyze.create(), myTestMethods } });
In the validation schema a reference to a test method is its full dot-notated path.
Validation Methods
Straints expects test methods to be defined in the following way:
function testMethod(value: any, ...args: any[]): boolean|Function|Promise
The test method must accept the test value as the first argument and may also accept or require additional arguments.
Straints handles the test method return value as follows:
- a Promise its
.then()
is called with success and error callbacks - a function it is called with success and error callbacks
- a boolean then it is handled as a successful validation
If the value returned is none of the above or if the error callback is used then the validation session will be terminated.