@42.nl/jarb-final-form
Version:
Validating forms through JaRB.
176 lines (175 loc) • 7.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mostSpecificInputTypeFor = mostSpecificInputTypeFor;
exports.getFieldConstraintsFor = getFieldConstraintsFor;
exports.getFieldConstraints = getFieldConstraints;
exports.isRequired = isRequired;
exports.hasMinimumLength = hasMinimumLength;
exports.hasMaximumLength = hasMaximumLength;
exports.hasFractionLength = hasFractionLength;
exports.hasRadix = hasRadix;
exports.hasPattern = hasPattern;
exports.hasMin = hasMin;
exports.hasMax = hasMax;
const constraints_1 = require("./constraints");
// List of <input> types sorted on most specific first.
const inputTypes = [
'enum',
'boolean',
'color',
'datetime-local',
'datetime',
'month',
'week',
'date',
'time',
'email',
'tel',
'number',
'url',
'password',
'file',
'image',
'text'
];
/**
* Finds the most specific <input> type for the types parameter. For example if
* types is ['email', 'text'] the function returns 'email' because 'email'
* is the most specific input type. If nothing is found returns 'text'.
*
* If the types array is not defined or null 'text` is returned.
*
* @param {Array<string>} The types you want the closest type for.
* @return {FieldType} The closest <input> type, based on the types parameter.
*/
function mostSpecificInputTypeFor(types) {
if (!types) {
return 'text';
}
// Default to the last inputType which should be 'text'.
let index = inputTypes.length - 1;
for (let i = 0; i < types.length; i += 1) {
const type = types[i];
for (let j = 0; j < inputTypes.length; j += 1) {
const inputType = inputTypes[j];
if (type === inputType) {
index = Math.min(index, j);
break;
}
}
}
return inputTypes[index];
}
/**
* Finds the FieldConstraints rules for a specific validator in the
* Constraints object.
*
* If no constraints can be found for a validator the boolean false
* is returned.
*
* @param {validator} 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @param {Constraints} The constraints to find the validator in.
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
* @returns {FieldConstraints | false} The constraints for the specific field
*/
function getFieldConstraintsFor(validator, constraints) {
const [className] = validator.split('.', 1);
const propertyName = validator.substring(className.length + 1);
const classConstraints = constraints[className];
if (classConstraints !== undefined) {
const fieldConstraints = classConstraints[propertyName];
return fieldConstraints !== undefined ? fieldConstraints : false;
}
else {
return false;
}
}
/**
* Finds the FieldConstraints rules for a specific validator.
*
* If no constraints can be found for a validator the boolean false
* is returned.
*
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
* @returns FieldConstraints | false The constraints for the specific field
*/
function getFieldConstraints(validator) {
const constraints = (0, constraints_1.getConstraints)();
if (!constraints) {
return false;
}
return getFieldConstraintsFor(validator, constraints);
}
/**
* Determine if constraints tell a specific validator makes a field required.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function isRequired(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && fieldConstraints.required === true;
}
/**
* Determine if constraints tell a specific validator makes a field have a minimum length.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasMinimumLength(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.minimumLength === 'number';
}
/**
* Determine if constraints tell a specific validator makes a field have a maximum length.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasMaximumLength(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.maximumLength === 'number';
}
/**
* Determine if constraints tell a specific validator makes a field have a fraction length.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasFractionLength(validator) {
const fieldConstraints = getFieldConstraints(validator);
return (fieldConstraints && typeof fieldConstraints.fractionLength === 'number');
}
/**
* Determine if constraints tell a specific validator makes a field have a radix.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasRadix(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.radix === 'number';
}
/**
* Determine if constraints tell a specific validator makes a field comply to a pattern.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasPattern(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.pattern === 'string';
}
/**
* Determine if constraints tell a specific validator makes a field have a minimum.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasMin(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.min === 'number';
}
/**
* Determine if constraints tell a specific validator makes a field have a maximum.
* @param validator 'validator' is a string with the format: 'Class.field' for example: 'User.age'
* @throws {error} When the validator doesn't match the format 'className.fieldName'.
*/
function hasMax(validator) {
const fieldConstraints = getFieldConstraints(validator);
return fieldConstraints && typeof fieldConstraints.max === 'number';
}