nope-validator
Version:
Fast and simple JS validator
165 lines (162 loc) • 5.04 kB
JavaScript
import { NopePrimitive } from './NopePrimitive.js';
import { runValidators, isNil, deepEquals } from './utils.js';
function ofType(entry, primitive) {
let done = false;
return entry.reduce(function (previous, next) {
if (done) {
return previous;
}
return previous.then(function (error) {
if (error) {
done = true;
return error;
}
return primitive.validateAsync(next);
});
}, Promise.resolve());
}
class NopeArray {
constructor() {
this._type = 'object';
this.validationRules = [];
this.ofShape = null;
}
getType() {
return this._type;
}
required(message = 'This field is required') {
const rule = (entry) => {
if (isNil(entry)) {
return message;
}
};
return this.test(rule);
}
of(primitive, message = 'One or more elements are of invalid type') {
this.ofShape = primitive;
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.some((value) => primitive.getType() !== typeof value)) {
return message;
}
const error = entry.find((value) => primitive.validate(value));
if (error) {
return message;
}
};
return this.test(rule);
}
ofAsync(primitive, message = 'One or more elements are of invalid type') {
this.ofShape = primitive;
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.some((value) => primitive.getType() !== typeof value)) {
return message;
}
return ofType(entry, primitive).then((error) => (error && message) || undefined);
};
return this.test(rule);
}
minLength(length, message = 'Input is too short') {
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.length <= length) {
return message;
}
};
return this.test(rule);
}
maxLength(length, message = 'Input is too long') {
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.length >= length) {
return message;
}
};
return this.test(rule);
}
mustContain(value, message = 'Input does not contain required value') {
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.indexOf(value) === -1) {
return message;
}
};
return this.test(rule);
}
hasOnly(values, message = 'Input elements must correspond to value values') {
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.some((value) => {
if (typeof value === 'object') {
return !values.find((v) => deepEquals(value, v));
}
return values.indexOf(value) === -1;
})) {
return message;
}
};
return this.test(rule);
}
every(callback, message = 'Input does not satisfy condition') {
const rule = (entry) => {
if (isNil(entry)) {
return;
}
if (entry.some((value) => !callback(value))) {
return message;
}
};
return this.test(rule);
}
some(callback, message = 'Input does not satisfy condition') {
const rule = (entry) => {
if (isNil(entry) || entry.length === 0) {
return;
}
if (!entry.some((value) => callback(value))) {
return message;
}
};
return this.test(rule);
}
test(rule) {
this.validationRules.push(rule);
return this;
}
validate(entry, context) {
for (const rule of this.validationRules) {
const error = rule(entry, context);
if (error instanceof NopePrimitive) {
return error.validate(entry, context);
}
else if (error) {
return `${error}`;
}
}
}
validateAsync(entry, context) {
return runValidators(this.validationRules, entry, context).then((error) => {
if (error instanceof NopePrimitive) {
return error.validateAsync(entry, context);
}
else if (error) {
return error;
}
});
}
}
export { NopeArray };
//# sourceMappingURL=NopeArray.js.map