Rekord is a javascript REST ORM that is offline and real-time capable.
rekord-validation adds rules, expressions, transforms, and custom validation functionality.
Installation
The easiest way to install rekord-validation is through bower via bower install rekord-validation
.
- rekord-validation.js is
48KB
(8.32KB
gzipped) - rekord-validation.min.js is
19KB
(5.69KB
gzipped)
// Simplest rule definition
var Task = Rekord({
name: 'task',
fields: ['name', 'details', 'done', 'done_at'],
validation: {
rules: {
name: 'required|alpha_dash',
details: '$custom', // custom function
done: 'yesno',
done_at: 'if:done:accepted|required|date_like'
},
required: true // the rules must pass to $save model instances
},
methods: {
$custom: function(value, getAlias, specifiedMessage, chain) {
if ( value.length > 140 ) {
return 'Details can be no larger than 140 characters.'
}
}
}
});
var t0 = new Task({name: '^', done: true, details: '0...141'});
t0.$save();
t0.$valid; // false
t0.$validationMessages; // array of below messages
t0.$validations; /* {
name: 'name should only contain alpha-numeric characters, dashes, and underscores.',
details: 'Details can be no larger than 140 characters.',
done_at: 'done_at is required.'
} */
// You can use aliases to make messages friendlier
var Task = Rekord({
name: 'task',
fields: ['name', 'done', 'done_at'],
validation: {
rules: {
name: 'required|alpha_dash',
done: 'yesno',
done_at: 'if:done:accepted|required|date_like'
},
aliases: {
name: 'Task name',
done: 'Task completed',
done_at: 'Task completed at'
}
}
});
var t1 = new Task({name: '', done: false});
t1.$validate(); /* {
name: 'Task name is required.'
} */
var t2 = new Task({name: 'task2', done: true, done_at: 'not date'});
t2.$validate(); /* {
done_at: 'Task completed at must be a valid date.'
} */
// You can specify field level custom messages
var Task = Rekord({
name: 'task',
fields: ['name', 'done', 'done_at'],
validation: {
rules: {
name: 'required|alpha_dash',
done: 'yesno',
done_at: 'if:done:accepted|required|date_like'
},
messages: {
name: 'Task name is required and must be alpha-numeric and can contain dashes and hyphens.',
done_at: 'When a task is completed, the completed date is required.'
}
}
});
var t3 = new Task({name: '?', done: true, done_at: 'not date'});
t3.$validate(); /* {
name: 'Task name is required and must be alpha-numeric and can contain dashes and hyphens.',
done_at: 'When a task is completed, the completed date is required.'
} */
// You can specify rule level custom messages
var Task = Rekord({
name: 'task',
fields: ['name', 'done', 'done_at'],
validation: {
rules: {
name: {
'required': false, // defaults to field level - then default
'alpha_dash': 'Task name must be alpha-numeric and can contain dashes and hyphens.'
},
done: 'yesno',
done_at: {
'if:done:accepted': false, // defaults to field level - then default
'required': 'Task completed date is required when the task is complete.',
'date_like': 'Task completed date must be a valid date when the task is complete.'
}
},
aliases: {
name: 'Task name'
}
}
});
var t4 = new Task({name: '?', done: true, done_at: 'not date'});
t4.$validate(); /* {
name: 'Task name must be alpha-numeric and can contain dashes and hyphens.',
done_at: 'Task completed date must be a valid date when the task is complete.'
} */
var t5 = new Task({done: true});
t5.$validate(); /* {
name: 'Task name is required.',
done_at: 'Task completed date is required when the task is complete.'
} */
// There are even more specific ways to define rules - depending on the rule.
// Check out the tests!
- "date like": number (millis since Unix Epoch), Date object, or string that can be parsed with
Date.parse
- "number like": number or a string that begins with a number
accepted
: The field must be an accepted value (1, yes, on, y, or true)after:date
: If the field is like a date, it must be after the givendate
expressionafter_on:date
: If the field is like a date, it must be on or after the givendate
expressionbefore:date
: If the field is like a date, it must be before the givendate
expressionbefore_on:date
: If the field is like a date, it must be on or before the givendate
expressiondate_like
: The field must look like a date (Date, number, or valid date string)required_if:field,value0,valueN
: The field is required is anotherfield
has any of the given valuesrequired_unless:field,value0,valueN
: The field is required if anotherfield
does not have any of the given valuesconfirmed:field
: The field must match anotherfield
different:field
: The field must NOT match anotherfield
if_valid:field0,fieldN
: The rules following this one for this field will not be executed if any of thefield
s are invalid at this point in time in validationrequired_with:field0,fieldN
: The field is required if any of thefield
s have a non-empty valuerequired_with_all:field0,fieldN
: The field is required if all of thefield
s have a non-empty valuerequired_without:field0,fieldN
: The field is required if any of thefield
s have an empty valuerequired_without_all:field0,fieldN
: The field is required if all of thefield
s have an empty valueexists
exists:models
exists:models,field
: The field value must exist in the given database (models
- or this if none specified) in the givenfield
(this field if not given)unique
unique:models
unique:models,field
: The field value must NOT exist in the given database (models
- or this if none specified) in the givenfield
(this field if not given)if:rule0\|ruleN
: The rules following this one for this field will not be executed unless allrule
s passif_any:rule0\|ruleN
: The rules following this one for this field will not be executed unless at lease onerule
passesif_not:rule0\|ruleN
: The rules following this one for this field will only be executed if allrule
s failin:value0,valueN
: This field must have a value in the givenvalue
snot_in:value0,valueN
: This field must NOT have a value in the givenvalue
sbetween:start,end
: This field must have a size between (inclusive) thestart
andend
(string, number, array, or object)not_between:start,end
: This field must NOT have a size between (inclusive) thestart
andend
(works with strings, numbers, arrays, object keys)alpha
: The field should only contain alphabetic charactersalpha_dash
: The field should only contain alpha-numeric characters, dashes, and underscoresalpha_num
: The field should only contain alpha-numeric charactersemail
: The field should look like an emailurl
: The field should look like a URLuri
: The field should look like a URIphone
: The field should look like a phone numberregex:/rgx/
: The field should pass the regular expressionrequired
: The field must be a non-empty valuemin:number
: The field must be at leastnumber
in size (string, number, array, or object)greater_than:number
: The field must be greater thannumber
in size (string, number, array, or object)max:number
: The field must be no more thannumber
in size (string, number, array, or object)less_than:number
: The field must be less thannumber
in size (string, number, array, or object)equal:number
: The field must be equal tonumber
in size (string, number, array, or object)not_equal:number
: The field must not be equal tonumber
in size (string, number, array, or object)array
: The field must be an instance of Arrayobject
: The field must be an object (arrays count as objects)string
: The field must be a stringnumber
: The field must be a numberboolean
: The field must be a booleanmodel
: The field must be a model instancewhole
: The field must look like a whole numbernumeric
: The field must look like a numberyesno
: The field must look like a boolean (true, t, yes, y, 1, false, f, no, n, 0)
contains:field,value
: The relation field must contain a model with at least onefield
matching the givenvalue
not_contains:field,value
: The relation field must NOT contain a model with at least onefield
matching the givenvalue
validate
: The relation field must contain all valid models
Expressions can be passed to rules (like date rules) and are generated on validation
MM/dd/yyyy
: Parses to a date timefield
: Takes a field value from the model being validated-1 day
: Relative amount of time from current time (+-)N (ms,s,min,hr,day,month,wk,yr)today
: Todays date (start of day)tomorrow
: Tomorrows date (start of day)yesterday
: Yesterdays date (start of day)
Transforms modify the value being validated so all subsequent rules use the modified values. Some transforms can apply the modified value back to the model after validation.
-
trim
: If the field value is a string, return the trimmed value -
abs
: If the field value looks like a number, parse it and return the absolute value -
ceil
: If the field value looks like a number, parse it and return the ceiling value -
floor
: If the field value looks like a number, parse it and return the floored value -
round
: If the field value looks like a number, parse it and return the rounded value -
endOfDay
: If the field value looks like a date, return its end of day -
startOfDay
: If the field value looks like a date, return its start of day -
base64
: Base64 encode the current value -
unbase64
: Un-base64 the current value -
filter
: If the field value is an array or object, remove the null and undefined properties/elements -
mod:number
: If the field value looks like a number, parse it and return the remainder to the division between the field value andnumber
-
null
: Apply null back to the model (can be used in conjuction withif
) -
apply
: Apply the currently transformed value back to the model