salix/client/core/src/lib/validator.js

96 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-02-02 12:17:30 +00:00
import {validator} from 'vendor';
export const validators = {
presence: function(value, conf) {
if (validator.isEmpty(value))
2017-02-02 12:17:30 +00:00
throw new Error(`Value can't be empty`);
},
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
};
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 if (conf.min && conf.max) {
2017-02-02 12:17:30 +00:00
throw new Error(`Value should have a length between ${conf.min} and ${conf.max}`);
} else if (conf.min) {
2017-05-26 11:14:47 +00:00
throw new Error(`Value should have at least ${conf.min} characters`);
} else {
throw new Error(`Value should have at most ${conf.max} characters`);
}
2017-02-02 12:17:30 +00:00
}
},
numericality: function(value, conf) {
if (conf.int) {
if (!validator.isInt(value))
2017-02-02 12:17:30 +00:00
throw new Error(`Value should be integer`);
} 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) {
if (!validator.isIn(value, conf.in))
2017-02-02 12:17:30 +00:00
throw new Error(`Invalid value`);
},
exclusion: function(value, conf) {
if (validator.isIn(value, conf.in))
2017-02-02 12:17:30 +00:00
throw new Error(`Invalid value`);
},
format: function(value, conf) {
if (!validator.matches(value, conf.with))
2017-02-02 12:17:30 +00:00
throw new Error(`Invalid value`);
},
custom: function(value, conf) {
if (!conf.bindedFunction(value))
2017-02-02 12:17:30 +00:00
throw new Error(`Invalid value`);
}
2017-02-02 12:17:30 +00:00
};
2017-03-07 12:01:24 +00:00
/**
* Checks if value satisfies a set of validations.
*
* @param {*} value The value
* @param {Array} validations Array with validations
*/
export function validateAll(value, validations) {
for (let conf of validations)
validate(value, conf);
}
2017-03-07 12:01:24 +00:00
/**
* Checks if value satisfies a validation.
*
* @param {*} value The value
* @param {Object} conf The validation configuration
*/
export function validate(value, conf) {
let validator = validators[conf.validation];
try {
checkNull(value, conf);
2017-05-26 11:14:47 +00:00
if (validator && value != null)
validator(value, conf);
} catch (e) {
let message = conf.message ? conf.message : e.message;
throw new Error(message);
}
}
/**
2017-05-26 15:05:33 +00:00
* Checks if value satisfies a blank or not null validation.
*
2017-03-07 12:01:24 +00:00
* @param {*} value The value
* @param {Object} conf The validation configuration
*/
export function checkNull(value, conf) {
2017-05-26 15:05:33 +00:00
if (conf.allowBlank === false && value === '')
throw new Error(`Value can't be blank`);
2017-05-26 15:05:33 +00:00
else if (conf.allowNull === false && value == null)
throw new Error(`Value can't be null`);
}