2017-02-02 12:17:30 +00:00
|
|
|
import {validator} from 'vendor';
|
|
|
|
|
|
|
|
export const validators = {
|
|
|
|
presence: function(value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (validator.isEmpty(value))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value can't be empty`);
|
|
|
|
},
|
2017-03-07 11:37:59 +00:00
|
|
|
absence: function(value, conf) {
|
|
|
|
if (!validator.isEmpty(value))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value should be empty`);
|
|
|
|
},
|
|
|
|
length: function(value, conf) {
|
|
|
|
let options = {
|
|
|
|
min: conf.min || conf.is,
|
|
|
|
max: conf.max || conf.is
|
|
|
|
};
|
2017-03-07 11:37:59 +00:00
|
|
|
|
|
|
|
if (!validator.isLength(value, options)) {
|
|
|
|
if (conf.is)
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value should be ${conf.is} characters long`);
|
|
|
|
else
|
|
|
|
throw new Error(`Value should have a length between ${conf.min} and ${conf.max}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
numericality: function(value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (conf.int) {
|
|
|
|
if (!validator.isInt(value))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value should be integer`);
|
2017-03-07 11:37:59 +00:00
|
|
|
} else if (!validator.isNumeric(value))
|
|
|
|
throw new Error(`Value should be a number`);
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
|
|
|
inclusion: function(value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.isIn(value, conf.in))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
exclusion: function(value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (validator.isIn(value, conf.in))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
format: function(value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.matches(value, conf.with))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
custom: function(value, conf) {
|
|
|
|
let valid = true;
|
|
|
|
function err(kind) {
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inst = {attr: value};
|
2017-03-07 11:37:59 +00:00
|
|
|
conf.customValidator.call(inst, err);
|
2017-02-02 12:17:30 +00:00
|
|
|
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!valid)
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Invalid value`);
|
|
|
|
},
|
|
|
|
uniqueness: function() {
|
|
|
|
// TODO: Implement me
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|
2017-02-02 12:17:30 +00:00
|
|
|
};
|
2017-03-07 11:37:59 +00:00
|
|
|
|
|
|
|
export function validateAll(value, validations) {
|
|
|
|
for (let conf of validations)
|
|
|
|
validate(value, conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validate(value, conf) {
|
|
|
|
let validator = validators[conf.validation];
|
|
|
|
try {
|
|
|
|
checkNull(value, conf);
|
|
|
|
if (validator) validator(value, conf);
|
|
|
|
} catch (e) {
|
|
|
|
let message = conf.message ? conf.message : e.message;
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if value is null.
|
|
|
|
*
|
|
|
|
* @param {value} value The value
|
|
|
|
* @param {Object} conf The configuration for the field
|
|
|
|
*/
|
|
|
|
export function checkNull(value, conf) {
|
|
|
|
if (typeof value === 'undefined') {
|
|
|
|
if (!conf.allowBlank)
|
|
|
|
throw new Error(`Value can't be blank`);
|
|
|
|
} else if (value === null && !conf.allowNull)
|
|
|
|
throw new Error(`Value can't be null`);
|
|
|
|
}
|