Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 23x 23x 5x 3x 2x 23x 10x 11x 23x 3x 3x 4x 4x 23x 3x 3x 4x 4x 23x 3x 3x 3x 23x 3x 3x 3x 23x 3x 3x 4x 23x 3x 3x 4x 23x 23x 240x 2x 34x 31x 3x 2x | import { I18n } from '../i18n';
import { IDictionary } from './interfaces';
/**
* Tries to iterate over a value and returns false if iteration is possible, true otherwise.
* @param value The value to try iterating over.
* @returns True if no interation is made over the passed value.
*/
const isEmpty = (value: any) => {
if (Array.isArray(value) || typeof value === 'string') {
return value.length === 0;
}
return !(value instanceof File) && Object.keys(value).length === 0;
};
/**
* Validates field not empty
* @param config Configuration options (message)
* @returns Rule method
*/
const required = (config: { message?: string } = {}) => {
const message = I18n.translate(config.message || 'VALIDATION_REQUIRED');
return (value: any) => (value && !isEmpty(value)) || typeof value === 'number' || message;
};
/**
* Validates email format
* @param config Configuration options (message)
* @returns Rule method
*/
const email = (config: { message?: string } = {}) => {
const message = I18n.translate(config.message || 'VALIDATION_REQUIRED_EMAIL');
return (value: string) => {
const pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
return !value || pattern.test(value) || message;
};
};
const emailPrefix = (config: { message?: string } = {}) => {
const message = I18n.translate(config.message || 'VALIDATION_REQUIRED_EMAIL');
return (value: string) => {
const pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*$/;
return !value || pattern.test(value) || message;
};
};
/**
* Validates field min value
* @param config Configuration options (limit, message)
* @returns Rule method
*/
const min = (config: { limit: number; message?: string }) => {
const { limit } = config;
const message = I18n.translate(config.message || 'VALIDATION_MINIMUM_VALUE', { limit });
return (value: any) => limit <= parseFloat(value) || message;
};
/**
* Validates field max value
* @param config Configuration options (limit, message)
* @returns Rule method
*/
const max = (config: { limit: number; message?: string }) => {
const { limit } = config;
const message = I18n.translate(config.message || 'VALIDATION_MAXIMUM_VALUE', { limit });
return (value: any) => limit >= parseFloat(value) || message;
};
/**
* Validates value minimum length
* @param config Configuration options (limit, message)
* @returns Rule method
*/
const minLength = (config: { limit: number; message?: string }) => {
const { limit } = config;
const message = I18n.translate(config.message || 'VALIDATION_MINIMUM_LENGTH', { limit });
return (value: any) => limit <= (value || '').toString().length || message;
};
/**
* Validates value maximum length
* @param config Configuration options (limit, message)
* @returns Rule method
*/
const maxLength = (config: { limit: number; message?: string }) => {
const { limit } = config;
const message = I18n.translate(config.message || 'VALIDATION_MAXIMUM_LENGTH', { limit });
return (value: any) => limit >= (value || '').toString().length || message;
};
/**
* All registered validations
*/
const validations: { [key: string]: (config?: any) => (value: any) => boolean | string } = {
email,
emailPrefix,
max,
min,
required,
minLength,
maxLength,
};
/**
* Used to create validations for fields in JSON
*/
export class Validation {
/**
* Registers a new validation or overwrites if it exists.
* All validation methods have an optional configuration parameter and return a method.
* @param name Validation name
* @param method Validation method
*/
public static register(name: string, method: (config?: IDictionary<any>) => (value: any) => boolean | string) {
validations[name] = method;
}
/**
* Unregisters a validation
* @param name Validation name
*/
public static unregister(name: string) {
delete validations[name];
}
/**
* Retrieves a validation
* @param name Validation name
* @returns Registered validation
* @throws Will throw if validation not exists
*/
public static get(name: string) {
if (validations[name]) {
return validations[name];
}
throw new Error(`Validation ${name} not found`);
}
/**
* Retrieves all registered validations
* @returns All registered validations
*/
public static getAll() {
return validations;
}
}
|