2017-02-02 12:17:30 +00:00
|
|
|
import {validator} from 'vendor';
|
|
|
|
|
|
|
|
export const validators = {
|
2018-01-02 07:05:50 +00:00
|
|
|
presence: value => {
|
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`);
|
|
|
|
},
|
2018-01-02 07:26:46 +00:00
|
|
|
absence: value => {
|
2017-03-07 11:37:59 +00:00
|
|
|
if (!validator.isEmpty(value))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value should be empty`);
|
|
|
|
},
|
2018-01-02 13:10:17 +00:00
|
|
|
length: (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 07:07:40 +00:00
|
|
|
let val = String(value);
|
|
|
|
if (!validator.isLength(val, options)) {
|
2017-05-26 11:26:12 +00:00
|
|
|
if (conf.is) {
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`Value should be ${conf.is} characters long`);
|
2017-05-26 11:26:12 +00:00
|
|
|
} 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}`);
|
2017-05-26 11:26:12 +00:00
|
|
|
} else if (conf.min) {
|
2017-05-26 11:14:47 +00:00
|
|
|
throw new Error(`Value should have at least ${conf.min} characters`);
|
2017-05-26 11:26:12 +00:00
|
|
|
} else {
|
2017-05-26 12:39:36 +00:00
|
|
|
throw new Error(`Value should have at most ${conf.max} characters`);
|
2017-05-26 11:26:12 +00:00
|
|
|
}
|
2017-02-02 12:17:30 +00:00
|
|
|
}
|
|
|
|
},
|
2018-01-02 13:10:17 +00:00
|
|
|
numericality: (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
|
|
|
},
|
2018-01-02 13:49:11 +00:00
|
|
|
inclusion: (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`);
|
|
|
|
},
|
2018-01-02 13:49:11 +00:00
|
|
|
exclusion: (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`);
|
|
|
|
},
|
2018-01-02 13:49:11 +00:00
|
|
|
format: (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`);
|
|
|
|
},
|
2018-01-02 13:49:11 +00:00
|
|
|
custom: (value, conf) => {
|
2017-09-25 15:39:51 +00:00
|
|
|
if (!conf.bindedFunction(value))
|
2017-02-02 12:17:30 +00:00
|
|
|
throw new Error(`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
|
|
|
|
*/
|
2017-03-07 11:37:59 +00:00
|
|
|
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
|
|
|
|
*/
|
2017-03-07 11:37:59 +00:00
|
|
|
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);
|
2017-03-07 11:37:59 +00:00
|
|
|
} 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 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
|
|
|
*/
|
|
|
|
export function checkNull(value, conf) {
|
2017-05-26 15:05:33 +00:00
|
|
|
if (conf.allowBlank === false && value === '')
|
2017-05-26 12:39:36 +00:00
|
|
|
throw new Error(`Value can't be blank`);
|
2017-05-26 15:05:33 +00:00
|
|
|
else if (conf.allowNull === false && value == null)
|
2017-03-07 11:37:59 +00:00
|
|
|
throw new Error(`Value can't be null`);
|
|
|
|
}
|