|
| 1 | +import { AdminForthDataTypes } from '../types/Common.js'; |
| 2 | +import type { AdminForthResource, AdminForthResourceColumn, IAdminForth } from '../types/Back.js'; |
| 3 | + |
| 4 | +const LENGTH_LIMITED_TYPES = [AdminForthDataTypes.STRING, AdminForthDataTypes.TEXT]; |
| 5 | +const VALUE_LIMITED_TYPES = [AdminForthDataTypes.INTEGER, AdminForthDataTypes.DECIMAL, AdminForthDataTypes.FLOAT]; |
| 6 | + |
| 7 | +function isEmptyValue(value: any): boolean { |
| 8 | + return value === undefined || value === null || value === ''; |
| 9 | +} |
| 10 | + |
| 11 | +// connectors discover an absent limit as null |
| 12 | +function isLimitSet(limit: number | null | undefined): boolean { |
| 13 | + return limit !== undefined && limit !== null; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Same emptiness as the create/edit form uses for required columns |
| 18 | + */ |
| 19 | +export function isEmptyColumnValue(column: AdminForthResourceColumn, value: any): boolean { |
| 20 | + return isEmptyValue(value) || (!!column.isArray?.enabled && value.length === 0); |
| 21 | +} |
| 22 | + |
| 23 | +function validateItemValue(column: AdminForthResourceColumn, type: AdminForthDataTypes, value: any): string | null { |
| 24 | + if (LENGTH_LIMITED_TYPES.includes(type)) { |
| 25 | + if (isLimitSet(column.maxLength) && value.length > column.maxLength) { |
| 26 | + return `Value in "${column.name}" must not be longer than ${column.maxLength} characters`; |
| 27 | + } |
| 28 | + if (isLimitSet(column.minLength) && value.length < column.minLength) { |
| 29 | + return `Value in "${column.name}" must not be shorter than ${column.minLength} characters`; |
| 30 | + } |
| 31 | + } |
| 32 | + if (VALUE_LIMITED_TYPES.includes(type)) { |
| 33 | + if (isLimitSet(column.minValue) && value < column.minValue) { |
| 34 | + return `Value in "${column.name}" must be greater than ${column.minValue}`; |
| 35 | + } |
| 36 | + if (isLimitSet(column.maxValue) && value > column.maxValue) { |
| 37 | + return `Value in "${column.name}" must be less than ${column.maxValue}`; |
| 38 | + } |
| 39 | + } |
| 40 | + if (column.enum && !column.enum.some((item) => item.value === value)) { |
| 41 | + return `Value in "${column.name}" must be one of: ${column.enum.map((item) => item.value).join(', ')}`; |
| 42 | + } |
| 43 | + return null; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Checks a non-empty column value against length and value limits, enum and array rules |
| 48 | + */ |
| 49 | +export function validateColumnValue(column: AdminForthResourceColumn, value: any): string | null { |
| 50 | + if (!column.isArray?.enabled) { |
| 51 | + return validateItemValue(column, column.type, value); |
| 52 | + } |
| 53 | + if (value.some(isEmptyValue)) { |
| 54 | + return `Array in "${column.name}" cannot contain empty items`; |
| 55 | + } |
| 56 | + if (!column.isArray.allowDuplicateItems && new Set(value).size !== value.length) { |
| 57 | + return `Array in "${column.name}" cannot contain duplicate items`; |
| 58 | + } |
| 59 | + for (const item of value) { |
| 60 | + const error = validateItemValue(column, column.isArray.itemType, item); |
| 61 | + if (error) { |
| 62 | + return error; |
| 63 | + } |
| 64 | + } |
| 65 | + return null; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Runs custom `validator` callbacks of the column and returns the first failed result |
| 70 | + */ |
| 71 | +export async function runColumnValidators( |
| 72 | + column: AdminForthResourceColumn, |
| 73 | + value: any, |
| 74 | + record: any, |
| 75 | + adminforth: IAdminForth, |
| 76 | +): Promise<{ isValid: false, message?: string } | null> { |
| 77 | + for (const val of column.validation ?? []) { |
| 78 | + if (val.validator) { |
| 79 | + const result = await val.validator(value, record, adminforth); |
| 80 | + if (typeof result === 'object' && result.isValid === false) { |
| 81 | + return { isValid: false, message: result.message }; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + return null; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Runs custom validators of the columns present in `values`, `record` is the whole record they are passed |
| 90 | + */ |
| 91 | +export async function runRecordValidators( |
| 92 | + resource: AdminForthResource, |
| 93 | + values: any, |
| 94 | + record: any, |
| 95 | + adminforth: IAdminForth, |
| 96 | +): Promise<string | null> { |
| 97 | + for (const column of resource.columns.filter((col) => col.name in values)) { |
| 98 | + const failed = await runColumnValidators(column, values[column.name], record, adminforth); |
| 99 | + if (failed) { |
| 100 | + return failed.message ?? `Value in "${column.name}" is invalid`; |
| 101 | + } |
| 102 | + } |
| 103 | + return null; |
| 104 | +} |
0 commit comments