|
| 1 | +import { FieldError, ProcessorError } from 'error'; |
| 2 | +import { SelectComponent, RuleFn, ValidationContext } from 'types'; |
| 3 | +import { Evaluator } from 'utils'; |
| 4 | +import { isEmptyObject, toBoolean } from '../util'; |
| 5 | +import { getErrorMessage } from 'utils/error'; |
| 6 | +import { ProcessorInfo } from 'types/process/ProcessorInfo'; |
| 7 | + |
| 8 | +const isValidatableSelectComponent = (component: any): component is SelectComponent => { |
| 9 | + return ( |
| 10 | + component && |
| 11 | + component.type === 'select' && |
| 12 | + toBoolean(component.dataSrc === 'resource') && |
| 13 | + toBoolean(component.validate?.select) |
| 14 | + ); |
| 15 | +}; |
| 16 | + |
| 17 | +export const generateUrl = (baseUrl: URL, component: SelectComponent, value: any) => { |
| 18 | + const url = baseUrl; |
| 19 | + const query = url.searchParams; |
| 20 | + if (component.searchField) { |
| 21 | + let searchValue = value; |
| 22 | + if (component.valueProperty) { |
| 23 | + searchValue = value[component.valueProperty]; |
| 24 | + } else { |
| 25 | + searchValue = value; |
| 26 | + } |
| 27 | + query.set(component.searchField, typeof searchValue === 'string' ? searchValue : JSON.stringify(searchValue)) |
| 28 | + } |
| 29 | + if (component.selectFields) { |
| 30 | + query.set('select', component.selectFields); |
| 31 | + } |
| 32 | + if (component.sort) { |
| 33 | + query.set('sort', component.sort); |
| 34 | + } |
| 35 | + if (component.filter) { |
| 36 | + const filterQueryStrings = new URLSearchParams(component.filter); |
| 37 | + filterQueryStrings.forEach((value, key) => query.set(key, value)); |
| 38 | + } |
| 39 | + return url; |
| 40 | +}; |
| 41 | + |
| 42 | +export const shouldValidate = (context: ValidationContext) => { |
| 43 | + const { component, value, data, config } = context; |
| 44 | + // Only run this validation if server-side |
| 45 | + if (!config?.server) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + if (!isValidatableSelectComponent(component)) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + if ( |
| 52 | + !value || |
| 53 | + isEmptyObject(value) || |
| 54 | + (Array.isArray(value) && (value as Array<Record<string, any>>).length === 0) |
| 55 | + ) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + // If given an invalid configuration, do not validate the remote value |
| 60 | + if (component.dataSrc !== 'resource' || !component.data?.resource) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + return true; |
| 65 | +}; |
| 66 | + |
| 67 | +export const validateResourceSelectValue: RuleFn = async (context: ValidationContext) => { |
| 68 | + const { value, config, component } = context; |
| 69 | + if (!shouldValidate(context)) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + if (!config || !config.database) { |
| 74 | + throw new ProcessorError("Can't validate for resource value without a database config object", context, 'validate:validateResourceSelectValue'); |
| 75 | + } |
| 76 | + try { |
| 77 | + const resourceSelectValueResult: boolean = await config.database?.validateResourceSelectValue(context, value); |
| 78 | + return (resourceSelectValueResult === true) ? null : new FieldError('select', context); |
| 79 | + } |
| 80 | + catch (err: any) { |
| 81 | + throw new ProcessorError(err.message || err, context, 'validate:validateResourceSelectValue'); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +export const validateResourceSelectValueInfo: ProcessorInfo<ValidationContext, FieldError | null> = { |
| 86 | + name: 'validateResourceSelectValue', |
| 87 | + process: validateResourceSelectValue, |
| 88 | + shouldProcess: shouldValidate, |
| 89 | +} |
0 commit comments