Or
Path:
/src/type/or.coffee
compiled to/lib/type/or.js
This is used to give some alternatives from which at least one check have to succeed. The first one succeeding will work.
Option:
or
- (array) with different check alternatives
Example:
You may allow numeric and special format input:
CoffeeScript Code validator.check
name: 'test' # name to be displayed in errors (optional)
value: input # value to check
schema: # definition of checks
type: 'or'
or: [
type: 'float'
,
type: 'string'
match: ///
^\s* # start with possible spaces
[+-]? # sign possible
\s*\d+(\.\d*)? # float number
\s*%? # percent sign with spaces
\s*$ # end of text with spaces
///
]
, (err, result) ->
# do something
With this type you can also use different option alternatives:
CoffeeScript Code validator.check
name: 'test' # name to be displayed in errors (optional)
value: input # value to check
schema: # definition of checks
type: 'or'
or: [
type: 'object'
allowedKeys: true
keys:
type:
type: 'string'
lowerCase: true
values: ['mysql']
port:
type: 'integer'
default: 3306
# ...
,
type: 'object'
allowedKeys: true
keys:
type:
type: 'string'
lowerCase: true
values: ['postgres']
port:
type: 'integer'
default: 5432
# ...
]
, (err, result) ->
# do something
In the example above only the default port is changed, but you may also add different options.
Schema Specification
Or is alternative schema definitions.
An object with the following keys allowed: or
, title
, description
, key
, type
, optional
, default
. The following entries have a specific format:
or
-
Alternatives are the list of alternatives for the value.
A list. The following entries have a specific format:
title
-
Title is the title used to describe the element.
A text entry which is optional. All control characters will be removed.
description
-
Description is the free description of the element.
A text entry which is optional. All control characters will be removed.
key
-
Binding to Keyname is the mapping to which key names in an object this element belongs.
A valid regular expression which is optional. It has to be one of the following types:
- An object which has to be an instance of class
RegExp
. - A text entry in which all control characters will be removed. The text should match: /^/.?/[gim]$/.
- An object which has to be an instance of class
type
-
Type is the type of element.
A text entry in which all control characters will be removed.
optional
-
Optional is a flag defining if this element is optional.
A boolean value, which will be true for ‘true’, ‘1’, ‘on’, ‘yes’, ‘+’, 1, true and will be considered as false for ‘false’, ‘0’, ‘off’, ‘no’, ‘-’, 0, false. It’s optional.
default
-
Default Value is the default value to use if nothing given.
A value of any type which is optional.