You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
932 B
JavaScript

"use strict";
const errors = require("../errors");
const getValueType = require("../util/get-value-type");
module.exports = function createStringValidatorFunction(options = {}, name = "<unknown>") {
let hasMatchCondition = (options.matches != null);
let matchCondition = options.matches;
return function validateString(value) {
if (typeof value !== "string") {
return new errors.ValidationError(`Expected a string, got ${getValueType(value)} instead`);
// return new errors.ValidationError(`Specified value for property '${name}' is not a string`, {
// value: value,
// property: name
// });
} else if (hasMatchCondition && !matchCondition.test(value)) {
/* TODO: Improve regex display */
return new errors.ValidationError(`Value for property '${name}' failed \`matches\` condition`, {
value: value,
property: name,
condition: matchCondition
});
} else {
return true;
}
};
};