2018-12-27 11:54:16 +00:00
|
|
|
import {validator} from '../vendor';
|
2017-02-02 12:17:30 +00:00
|
|
|
|
|
|
|
export const validators = {
|
2018-08-02 07:49:00 +00:00
|
|
|
presence: ($translate, value) => {
|
2018-01-11 14:25:03 +00:00
|
|
|
if (validator.isEmpty(value ? String(value) : ''))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value can't be empty`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2023-06-12 10:42:30 +00:00
|
|
|
negative: $translate => {
|
|
|
|
if (validator < 0)
|
|
|
|
throw new Error(_($translate, `Negative numbers are not allowed. Please enter a valid number.`));
|
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
absence: ($translate, value) => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.isEmpty(value))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value should be empty`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2018-08-02 07:49:00 +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
|
|
|
|
};
|
2018-01-10 13:03:52 +00:00
|
|
|
let val = value ? String(value) : '';
|
2018-01-10 07:07:40 +00:00
|
|
|
if (!validator.isLength(val, options)) {
|
2017-05-26 11:26:12 +00:00
|
|
|
if (conf.is) {
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate,
|
|
|
|
`Value should be %s characters long`, [conf.is]));
|
2017-05-26 11:26:12 +00:00
|
|
|
} else if (conf.min && conf.max) {
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate,
|
|
|
|
`Value should have a length between %s and %s`, [conf.min, conf.max]));
|
2017-05-26 11:26:12 +00:00
|
|
|
} else if (conf.min) {
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate,
|
|
|
|
`Value should have at least %s characters`, [conf.min]));
|
2017-05-26 11:26:12 +00:00
|
|
|
} else {
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate,
|
|
|
|
`Value should have at most %s characters`, [conf.max]));
|
2017-05-26 11:26:12 +00:00
|
|
|
}
|
2017-02-02 12:17:30 +00:00
|
|
|
}
|
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
numericality: ($translate, value, conf) => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (conf.int) {
|
|
|
|
if (!validator.isInt(value))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value should be integer`));
|
2017-03-07 11:37:59 +00:00
|
|
|
} else if (!validator.isNumeric(value))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value should be a number`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
inclusion: ($translate, value, conf) => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.isIn(value, conf.in))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Invalid value`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
exclusion: ($translate, value, conf) => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (validator.isIn(value, conf.in))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Invalid value`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
format: ($translate, value, conf) => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.matches(value, conf.with))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Invalid value`));
|
2017-02-02 12:17:30 +00:00
|
|
|
},
|
2018-08-02 07:49:00 +00:00
|
|
|
custom: ($translate, value, conf) => {
|
2017-09-25 15:39:51 +00:00
|
|
|
if (!conf.bindedFunction(value))
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Invalid value`));
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|
2017-02-02 12:17:30 +00:00
|
|
|
};
|
2017-03-07 11:37:59 +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
|
|
|
|
*/
|
2018-08-02 07:49:00 +00:00
|
|
|
export function validateAll($translate, value, validations) {
|
2017-03-07 11:37:59 +00:00
|
|
|
for (let conf of validations)
|
2018-08-02 07:49:00 +00:00
|
|
|
validate($translate, value, conf);
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 12:01:24 +00:00
|
|
|
/**
|
|
|
|
* Checks if value satisfies a validation.
|
|
|
|
*
|
|
|
|
* @param {*} value The value
|
|
|
|
* @param {Object} conf The validation configuration
|
|
|
|
*/
|
2018-08-02 07:49:00 +00:00
|
|
|
export function validate($translate, value, conf) {
|
2017-03-07 11:37:59 +00:00
|
|
|
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)
|
2018-08-02 07:49:00 +00:00
|
|
|
checkNull($translate, value, conf);
|
2018-02-27 21:50:14 +00:00
|
|
|
if (validator && (!isEmpty || conf.validation == 'presence'))
|
2018-08-02 07:49:00 +00:00
|
|
|
validator($translate, value, conf);
|
2017-03-07 11:37:59 +00:00
|
|
|
} catch (e) {
|
|
|
|
let message = conf.message ? conf.message : e.message;
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, message));
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-26 15:05:33 +00:00
|
|
|
* Checks if value satisfies a blank or not null validation.
|
2017-03-07 11:37:59 +00:00
|
|
|
*
|
2017-03-07 12:01:24 +00:00
|
|
|
* @param {*} value The value
|
|
|
|
* @param {Object} conf The validation configuration
|
2017-03-07 11:37:59 +00:00
|
|
|
*/
|
2018-08-02 07:49:00 +00:00
|
|
|
export function checkNull($translate, value, conf) {
|
2017-05-26 15:05:33 +00:00
|
|
|
if (conf.allowBlank === false && value === '')
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value can't be blank`));
|
2017-05-26 15:05:33 +00:00
|
|
|
else if (conf.allowNull === false && value == null)
|
2018-08-02 07:49:00 +00:00
|
|
|
throw new Error(_($translate, `Value can't be null`));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function _($translate, text, params = []) {
|
|
|
|
text = $translate.instant(text);
|
|
|
|
|
2023-06-12 10:42:30 +00:00
|
|
|
for (let i = 0; i < params.length; i++)
|
2018-08-02 07:49:00 +00:00
|
|
|
text = text.replace('%s', params[i]);
|
|
|
|
|
|
|
|
return text;
|
2017-03-07 11:37:59 +00:00
|
|
|
}
|