-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvalidateResourceSelectValue.ts
More file actions
98 lines (90 loc) · 2.87 KB
/
Copy pathvalidateResourceSelectValue.ts
File metadata and controls
98 lines (90 loc) · 2.87 KB
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
import { FieldError, ProcessorError } from 'error';
import { SelectComponent, RuleFn, ValidationContext } from 'types';
import { toBoolean } from '../util';
import { ProcessorInfo } from 'types/process/ProcessorInfo';
import { isEmptyObject } from 'utils';
const isValidatableSelectComponent = (component: any): component is SelectComponent => {
return (
component &&
component.type === 'select' &&
toBoolean(component.dataSrc === 'resource') &&
toBoolean(component.validate?.select)
);
};
export const generateUrl = (baseUrl: URL, component: SelectComponent, value: any) => {
const url = baseUrl;
const query = url.searchParams;
if (component.searchField) {
let searchValue = value;
if (component.valueProperty) {
searchValue = value[component.valueProperty];
} else {
searchValue = value;
}
query.set(
component.searchField,
typeof searchValue === 'string' ? searchValue : JSON.stringify(searchValue),
);
}
if (component.selectFields) {
query.set('select', component.selectFields);
}
if (component.sort) {
query.set('sort', component.sort);
}
if (component.filter) {
const filterQueryStrings = new URLSearchParams(component.filter);
filterQueryStrings.forEach((value, key) => query.set(key, value));
}
return url;
};
export const shouldValidate = (context: ValidationContext) => {
const { component, value, config } = context;
// Only run this validation if server-side
if (!config?.server) {
return false;
}
if (!isValidatableSelectComponent(component)) {
return false;
}
if (
!value ||
isEmptyObject(value) ||
(Array.isArray(value) && (value as Array<Record<string, any>>).length === 0)
) {
return false;
}
// If given an invalid configuration, do not validate the remote value
if (component.dataSrc !== 'resource' || !component.data?.resource) {
return false;
}
return true;
};
export const validateResourceSelectValue: RuleFn = async (context: ValidationContext) => {
const { value, config } = context;
if (!shouldValidate(context)) {
return null;
}
if (!config || !config.database) {
throw new ProcessorError(
"Can't validate for resource value without a database config object",
context,
'validate:validateResourceSelectValue',
);
}
try {
const resourceSelectValueResult: boolean = await config.database?.validateResourceSelectValue(
context,
value,
);
return resourceSelectValueResult === true ? null : new FieldError('select', context);
} catch (err: any) {
throw new ProcessorError(err.message || err, context, 'validate:validateResourceSelectValue');
}
};
export const validateResourceSelectValueInfo: ProcessorInfo<ValidationContext, FieldError | null> =
{
name: 'validateResourceSelectValue',
process: validateResourceSelectValue,
shouldProcess: shouldValidate,
};