-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from formio/FIO-7964-add-resource-validation
FIO-7964: add resource-based select component validation
- Loading branch information
Showing
5 changed files
with
104 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { ValidationRuleInfo } from "types"; | ||
import { validateRemoteSelectValueInfo } from "./validateRemoteSelectValue"; | ||
import { validateUrlSelectValueInfo } from "./validateUrlSelectValue"; | ||
|
||
// These are the validations that are asynchronouse (e.g. require fetch | ||
export const asynchronousRules: ValidationRuleInfo[] = [ | ||
validateRemoteSelectValueInfo, | ||
validateUrlSelectValueInfo, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { ValidationRuleInfo } from "types"; | ||
import { validateUniqueInfo } from "./validateUnique"; | ||
import { validateCaptchaInfo } from "./validateCaptcha"; | ||
import { validateResourceSelectValueInfo } from "./validateResourceSelectValue"; | ||
|
||
// These are the validations that require a database connection. | ||
export const databaseRules: ValidationRuleInfo[] = [ | ||
validateUniqueInfo, | ||
validateCaptchaInfo | ||
validateCaptchaInfo, | ||
validateResourceSelectValueInfo | ||
]; |
89 changes: 89 additions & 0 deletions
89
src/process/validation/rules/validateResourceSelectValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { FieldError, ProcessorError } from 'error'; | ||
import { SelectComponent, RuleFn, ValidationContext } from 'types'; | ||
import { Evaluator } from 'utils'; | ||
import { isEmptyObject, toBoolean } from '../util'; | ||
import { getErrorMessage } from 'utils/error'; | ||
import { ProcessorInfo } from 'types/process/ProcessorInfo'; | ||
|
||
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, data, 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, component } = 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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters