validd
Version:
An experiment on data validation
159 lines (158 loc) • 6.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validate = exports.addSchemaDefaultErrorMessages = void 0;
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
var isArray = function isArray(value) {
return _typeof(value) === 'object' && value !== null && !isNaN(Number(value.length));
};
var getType = function getType(value) {
return isArray(value) ? 'array' : _typeof(value);
};
var resolveObjectPromises = function resolveObjectPromises(object) {
var promises = [];
var objectKeys = Object.keys(object);
objectKeys.forEach(function (key) {
return promises.push(object[key]);
});
return Promise.all(promises).then(function (resolvedValues) {
return resolvedValues.reduce(function (resolvedObject, property, index) {
resolvedObject[objectKeys[index]] = property;
return resolvedObject;
}, {});
});
};
var addSchemaDefaultErrorMessages = exports.addSchemaDefaultErrorMessages = function addSchemaDefaultErrorMessages(schema) {
var defaultErrorMessages = {
invalidType: 'Invalid data type',
isRequired: 'The field is required',
minLength: 'This field must be larger',
maxLength: 'This field must be shorter',
regex: 'The field value is invalid'
};
var schemaMessages = schema.messages || {};
var messages = Object.keys(defaultErrorMessages).map(function (errorType) {
return _defineProperty({}, errorType, schemaMessages[errorType] || defaultErrorMessages[errorType]);
}).reduce(function (prev, curr) {
return Object.assign({}, prev, curr);
}, {});
return Object.assign({}, schema, {
messages: messages
});
};
var _validate = exports.validate = function validate(schema, data) {
return new Promise(function (resolve) {
var result = {};
if (!schema || Object.keys(schema).length === 0 || !schema.type) {
resolve(result);
return;
}
schema = addSchemaDefaultErrorMessages(schema);
var dataType = getType(data);
if (data && dataType !== schema.type) {
result.errors = result.errors || [];
result.errors.push({
error: 'invalidType',
message: schema.messages['invalidType']
});
resolve(result);
}
if (schema.isRequired && !data) {
result.errors = result.errors || [];
result.errors.push({
error: 'isRequired',
message: schema.messages['isRequired']
});
}
if (schema.type === 'object' && data !== null) {
// null is also of type 'object'
var fieldNames = schema.fields && Object.keys(schema.fields);
var validationPromises = fieldNames.map(function (name) {
return _defineProperty({}, name, _validate(schema.fields[name], data[name]));
}).reduce(function (prev, curr) {
return _objectSpread(_objectSpread({}, prev), curr);
}, {});
resolveObjectPromises(validationPromises).then(function (fieldsValidationResult) {
result.fields = fieldsValidationResult;
resolve(result);
});
} else if (schema.type === 'array') {
if (schema.minLength) {
if (!data || data.length === 0) {
resolve(result);
} else if (data.length < schema.minLength) {
result.errors = result.errors || [];
result.errors.push({
error: 'minLength',
message: schema.messages['minLength']
});
resolve(result);
}
}
resolve(result);
} else if (schema.type === 'string') {
if (schema.minLength) {
if (!data || data.length === 0) {
resolve(result);
} else if (data.length < schema.minLength) {
result.errors = result.errors || [];
result.errors.push({
error: 'minLength',
message: schema.messages['minLength']
});
resolve(result);
}
}
if (schema.maxLength && data.length > schema.maxLength) {
result.errors = result.errors || [];
result.errors.push({
error: 'maxLength',
message: schema.messages['maxLength']
});
resolve(result);
}
if (schema.validation) {
if (typeof schema.validation === 'function') {
var customValidationResult = schema.validation(data);
if (customValidationResult && customValidationResult.then) {
// Is a promise
customValidationResult.then(function (promiseValidationResult) {
if (promiseValidationResult) {
result.errors = result.errors || [];
result.errors.push(promiseValidationResult);
}
resolve(result);
});
} else if (customValidationResult) {
// Is a validation result
result.errors = result.errors || [];
result.errors.push(customValidationResult);
}
resolve(result);
}
} else {
resolve(result);
}
if (schema.regex && schema.regex instanceof RegExp) {
if (!schema.regex.test(data)) {
result.errors = result.errors || [];
result.errors.push({
error: 'regex',
message: schema.messages['regex']
});
}
resolve(result);
}
} else if (schema.type === 'number') {
resolve(result);
} else {
resolve(result);
}
});
};