salix/front/core/lib/validator.js

113 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2018-12-27 11:54:16 +00:00
import {validator} from '../vendor';
2017-02-02 12:17:30 +00:00
export const validators = {
presence: ($translate, value) => {
if (validator.isEmpty(value ? String(value) : ''))
throw new Error(_($translate, `Value can't be empty`));
2017-02-02 12:17:30 +00:00
},
absence: ($translate, value) => {
if (!validator.isEmpty(value))
throw new Error(_($translate, `Value should be empty`));
2017-02-02 12:17:30 +00:00
},
length: ($translate, value, conf) => {
2017-02-02 12:17:30 +00:00
let options = {
min: conf.min || conf.is,
max: conf.max || conf.is
};
let val = value ? String(value) : '';
2018-01-10 07:07:40 +00:00
if (!validator.isLength(val, options)) {
if (conf.is) {
throw new Error(_($translate,
`Value should be %s characters long`, [conf.is]));
} else if (conf.min && conf.max) {
throw new Error(_($translate,
`Value should have a length between %s and %s`, [conf.min, conf.max]));
} else if (conf.min) {
throw new Error(_($translate,
`Value should have at least %s characters`, [conf.min]));
} else {
throw new Error(_($translate,
`Value should have at most %s characters`, [conf.max]));
}
2017-02-02 12:17:30 +00:00
}
},
numericality: ($translate, value, conf) => {
if (conf.int) {
if (!validator.isInt(value))
throw new Error(_($translate, `Value should be integer`));
} else if (!validator.isNumeric(value))
throw new Error(_($translate, `Value should be a number`));
2017-02-02 12:17:30 +00:00
},
inclusion: ($translate, value, conf) => {
if (!validator.isIn(value, conf.in))
throw new Error(_($translate, `Invalid value`));
2017-02-02 12:17:30 +00:00
},
exclusion: ($translate, value, conf) => {
if (validator.isIn(value, conf.in))
throw new Error(_($translate, `Invalid value`));
2017-02-02 12:17:30 +00:00
},
format: ($translate, value, conf) => {
if (!validator.matches(value, conf.with))
throw new Error(_($translate, `Invalid value`));
2017-02-02 12:17:30 +00:00
},
custom: ($translate, value, conf) => {
if (!conf.bindedFunction(value))
throw new Error(_($translate, `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($translate, value, validations) {
for (let conf of validations)
validate($translate, 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($translate, value, conf) {
let validator = validators[conf.validation];
try {
2018-02-27 21:50:14 +00:00
let isEmpty = value == null || value === '';
2017-05-26 11:14:47 +00:00
2018-02-27 21:50:14 +00:00
if (isEmpty)
checkNull($translate, value, conf);
2018-02-27 21:50:14 +00:00
if (validator && (!isEmpty || conf.validation == 'presence'))
validator($translate, value, conf);
} catch (e) {
let message = conf.message ? conf.message : e.message;
throw new Error(_($translate, 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($translate, value, conf) {
2017-05-26 15:05:33 +00:00
if (conf.allowBlank === false && value === '')
throw new Error(_($translate, `Value can't be blank`));
2017-05-26 15:05:33 +00:00
else if (conf.allowNull === false && value == null)
throw new Error(_($translate, `Value can't be null`));
}
export function _($translate, text, params = []) {
text = $translate.instant(text);
for (let i = 0; i < params.length; i++) {
text = text.replace('%s', params[i]);
}
return text;
}