@onesy/ui-react
Version:
UI for React
232 lines (211 loc) • 12.6 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.onValidateError = exports.default = void 0;
var _is = _interopRequireDefault(require("@onesy/utils/is"));
var _isValid = _interopRequireDefault(require("@onesy/utils/isValid"));
var _utils = require("@onesy/utils");
var _errors = require("@onesy/errors");
const onValidateError = (options, model, message) => {
const error = new _errors.ValidationError(message);
// Entire model message
if (options.message !== undefined) error.message = options.message;
// model item message
else if (model.message !== undefined) error.message = model.message;
// error
throw error;
};
exports.onValidateError = onValidateError;
const validate = async (model, property, form, options_) => {
const options = (0, _utils.merge)(options_ && (0, _is.default)('object', options_) ? options_ : {}, {
uriDecode: true,
parse: true
});
const l = (options === null || options === void 0 ? void 0 : options.l) || (item => item);
const name = l((0, _is.default)('function', model.propertyNameUpdate) ? model.propertyNameUpdate(model.name) : model.capitalize !== false ? (0, _utils.capitalize)(model.name) : model.name);
const value = model.value;
// value validate
// with options above
// required
if (model.required) {
var _model$messages;
const response = value;
if (response === undefined) onValidateError(options, model, ((_model$messages = model.messages) === null || _model$messages === void 0 ? void 0 : _model$messages.required) || `${name} ${l('is required')}`);
}
// only validate
// if value is not undefined
// as it is optional
if (value === undefined) return;
// is
if (model.is !== undefined) {
const is_ = ((0, _is.default)('array', model.is) ? model.is : [model.is]).filter(Boolean);
for (const item of is_) {
var _model$messages2;
const itemType = (item === null || item === void 0 ? void 0 : item.type) || item;
const itemOptions = (item === null || item === void 0 ? void 0 : item.options) || undefined;
const response = (0, _is.default)(itemType, value, itemOptions);
if (!response) onValidateError(options, model, ((_model$messages2 = model.messages) === null || _model$messages2 === void 0 ? void 0 : _model$messages2.is) || `${name} ${l('has to be a valid')} ${(0, _utils.cleanValue)(l(itemType))}`);
}
}
// is valid
if (model.isValid !== undefined) {
const isValid_ = ((0, _is.default)('array', model.isValid) ? model.isValid : [model.isValid]).filter(Boolean);
for (const item of isValid_) {
var _model$messages3;
const itemType = (item === null || item === void 0 ? void 0 : item.type) || item;
const itemOptions = (item === null || item === void 0 ? void 0 : item.options) || undefined;
const response = (0, _isValid.default)(itemType, value, itemOptions);
if (!response) onValidateError(options, model, ((_model$messages3 = model.messages) === null || _model$messages3 === void 0 ? void 0 : _model$messages3.isValid) || `${name} ${l('has to be a valid')} ${(0, _utils.cleanValue)(l(itemType))}`);
}
}
// of
if (model.of !== undefined) {
const of_ = ((0, _is.default)('array', model.of) ? model.of : [model.of]).filter(Boolean);
if ((0, _is.default)('array', value)) {
var _model$messages4;
const response = value.every(valueItem => {
return of_.some(item => {
const itemType = (item === null || item === void 0 ? void 0 : item.type) || item;
const itemOptions = (item === null || item === void 0 ? void 0 : item.options) || undefined;
return (0, _is.default)(itemType, valueItem, itemOptions);
});
});
if (!response) onValidateError(options, model, ((_model$messages4 = model.messages) === null || _model$messages4 === void 0 ? void 0 : _model$messages4.of) || `${name} ${l('items have to be one of')} ${of_.map(item => l((item === null || item === void 0 ? void 0 : item.type) || item)).join(', ')}`);
}
}
// ofValid
if (model.ofValid !== undefined) {
const ofValid = ((0, _is.default)('array', model.ofValid) ? model.ofValid : [model.ofValid]).filter(Boolean);
if ((0, _is.default)('array', value)) {
var _model$messages5;
const response = value.every(valueItem => {
return ofValid.some(item => {
const itemType = (item === null || item === void 0 ? void 0 : item.type) || item;
const itemOptions = (item === null || item === void 0 ? void 0 : item.options) || undefined;
return (0, _isValid.default)(itemType, valueItem, itemOptions);
});
});
if (!response) onValidateError(options, model, ((_model$messages5 = model.messages) === null || _model$messages5 === void 0 ? void 0 : _model$messages5.ofValid) || `${name} ${l('items have to be one of valid')} ${ofValid.map(item => l((item === null || item === void 0 ? void 0 : item.type) || item)).join(', ')}`);
}
}
// equal
if (model.equal !== undefined) {
var _model$messages6;
const response = value === model.equal;
if (!response) onValidateError(options, model, ((_model$messages6 = model.messages) === null || _model$messages6 === void 0 ? void 0 : _model$messages6.equal) || `${name} ${l('has to be equal to')} ${(0, _utils.stringify)(model.equal)}`);
}
// not equal
if (model.notEqual !== undefined) {
var _model$messages7;
const response = value !== model.equal;
if (!response) onValidateError(options, model, ((_model$messages7 = model.messages) === null || _model$messages7 === void 0 ? void 0 : _model$messages7.notEqual) || `${name} ${l('has to not be equal to')} ${(0, _utils.stringify)(model.equal)}`);
}
// equal deep
if (model.equalDeep !== undefined) {
var _model$messages8;
const response = (0, _utils.equalDeep)(value, model.equalDeep);
if (!response) onValidateError(options, model, ((_model$messages8 = model.messages) === null || _model$messages8 === void 0 ? void 0 : _model$messages8.equalDeep) || `${name} ${l('has to be equal to')} ${(0, _utils.stringify)(model.equalDeep)}`);
}
// not equal deep
if (model.notEqualDeep !== undefined) {
var _model$messages9;
const response = !(0, _utils.equalDeep)(value, model.notEqualDeep);
if (!response) onValidateError(options, model, ((_model$messages9 = model.messages) === null || _model$messages9 === void 0 ? void 0 : _model$messages9.notEqualDeep) || `${name} ${l('has to not be equal to')} ${(0, _utils.stringify)(model.notEqualDeep)}`);
}
// some
if ((0, _is.default)('array', model.some)) {
let response;
if ((0, _is.default)('string', value)) {
var _model$messages0;
response = !!model.some.find(item => (0, _utils.equalDeep)(value, item));
if (!response) onValidateError(options, model, ((_model$messages0 = model.messages) === null || _model$messages0 === void 0 ? void 0 : _model$messages0.some) || `${name} ${l('has to be one of')} ${model.some.map(item => (0, _utils.stringify)(item)).join(', ')}`);
} else if ((0, _is.default)('array', value)) {
var _model$messages1;
response = value.some(item => !!model.some.find(item_ => (0, _utils.equalDeep)(item, item_)));
if (!response) onValidateError(options, model, ((_model$messages1 = model.messages) === null || _model$messages1 === void 0 ? void 0 : _model$messages1.some) || `${name} ${l('has to include some of')} ${model.some.map(item => (0, _utils.stringify)(item)).join(', ')}`);
}
}
// in
// every
const every = model.in || model.every;
if ((0, _is.default)('array', every)) {
let response;
if ((0, _is.default)('string', value)) {
var _model$messages10, _model$messages11;
response = !!every.find(item => (0, _utils.equalDeep)(value, item));
if (!response) onValidateError(options, model, ((_model$messages10 = model.messages) === null || _model$messages10 === void 0 ? void 0 : _model$messages10.in) || ((_model$messages11 = model.messages) === null || _model$messages11 === void 0 ? void 0 : _model$messages11.every) || `${name} ${l('has to be one of')} ${every.map(item => (0, _utils.stringify)(item)).join(', ')}`);
} else if ((0, _is.default)('array', value)) {
var _model$messages12, _model$messages13;
response = value.every(item => !!every.find(item_ => (0, _utils.equalDeep)(item, item_)));
if (!response) onValidateError(options, model, ((_model$messages12 = model.messages) === null || _model$messages12 === void 0 ? void 0 : _model$messages12.in) || ((_model$messages13 = model.messages) === null || _model$messages13 === void 0 ? void 0 : _model$messages13.every) || `${name} ${l('has to include one of')} ${every.map(item => (0, _utils.stringify)(item)).join(', ')}`);
}
}
// properties
if ((0, _is.default)('array', model.properties)) {
var _model$messages14;
const allowed = model.properties;
const keys = Object.keys(value);
const response = keys.every(item => allowed.includes(item));
if (!response) onValidateError(options, model, ((_model$messages14 = model.messages) === null || _model$messages14 === void 0 ? void 0 : _model$messages14.properties) || `${name} ${l('allowed properties are')} ${allowed.join(', ')}`);
}
// not properties
if ((0, _is.default)('array', model.notProperties)) {
var _model$messages15;
const notAllowed = model.notProperties;
const keys = Object.keys(value);
const response = keys.every(item => !notAllowed.includes(item));
if (!response) onValidateError(options, model, ((_model$messages15 = model.messages) === null || _model$messages15 === void 0 ? void 0 : _model$messages15.notProperties) || `${name} ${l('includes not allowed property')}. ${l('Not allowed properties are')} ${notAllowed.join(', ')}`);
}
// min
// max
// length
if (![undefined, null].includes(value) && ((0, _is.default)('number', model.min) || (0, _is.default)('number', model.max) || (0, _is.default)('number', model.length))) {
let length = value;
// object
if ((0, _is.default)('object', value)) length = Object.keys(value).length;
// number
else if ((0, _is.default)('number', value)) length = value;
// string, array, map, etc.
else {
length = (value === null || value === void 0 ? void 0 : value.length) !== undefined ? value === null || value === void 0 ? void 0 : value.length : value === null || value === void 0 ? void 0 : value.size;
}
if ((0, _is.default)('number', model.min)) {
var _model$messages16;
const response = length >= model.min;
if (!response) onValidateError(options, model, ((_model$messages16 = model.messages) === null || _model$messages16 === void 0 ? void 0 : _model$messages16.min) || `${name} ${l('has to be minimum')} ${model.min}`);
}
if ((0, _is.default)('number', model.max)) {
var _model$messages17;
const response = length <= model.max;
if (!response) onValidateError(options, model, ((_model$messages17 = model.messages) === null || _model$messages17 === void 0 ? void 0 : _model$messages17.max) || `${name} ${l('can be maximum')} ${model.max}`);
}
if ((0, _is.default)('number', model.length)) {
var _model$messages18;
const response = length === model.length;
if (!response) onValidateError(options, model, ((_model$messages18 = model.messages) === null || _model$messages18 === void 0 ? void 0 : _model$messages18.length) || `${name} ${l('has to be exactly')} ${model.length} ${l('in length/size')}`);
}
}
// method
const methods = ((0, _is.default)('array', model.method) ? model.method : [model.method]).filter(Boolean);
for (const method_ of methods) {
try {
// either throw error or Promise.reject or return false
const response = await method_(value, {
form,
object: model,
property,
options
});
if (response !== undefined) {
if (!response) throw new _errors.ValidationError(`${name} ${l('is invalid')}`);
}
} catch (error) {
var _model$messages19;
const messageValue = (error === null || error === void 0 ? void 0 : error.message) !== undefined ? error.message : error;
onValidateError(options, model, ((_model$messages19 = model.messages) === null || _model$messages19 === void 0 ? void 0 : _model$messages19.method) || messageValue);
}
}
};
var _default = exports.default = validate;