-
Notifications
You must be signed in to change notification settings - Fork 19
[JSON Logic] Part 1: JSON Logic Skeleton #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
443395b
feat: JSON Logic skeleton and plumbing setup
brennj fab9eb9
chore: one last thing to cleanup
brennj 7adee33
chore: add some jsdocs here
brennj eac5628
chore: clean ups from suggestions
brennj 63e379a
chore: better fn name
brennj 1aaa39e
chore: update docs + var name
brennj 17e8a35
chore: dont create two scopes for an array
brennj e89145e
chore: rename validations to logic
brennj e5fa90a
chore: wrong jsondoc
brennj 096e86a
chore: doc nit
brennj 6f4a7b9
chore: naming suggestions
brennj 82db200
chore: more clarification in test names
brennj fe7d0c6
chore: rework some tests
brennj c96c723
chore: more fixes
brennj 637551d
chore: everything resolved?
brennj 0d7a4ae
chore: one last fix
brennj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,124 @@ | ||
| import jsonLogic from 'json-logic-js'; | ||
|
|
||
| /** | ||
| * Parses the JSON schema to extract the json-logic rules and returns an object | ||
| * containing the validation scopes, functions to retrieve the scopes, and evaluate the | ||
| * validation rules. | ||
| * | ||
| * @param {Object} schema - JSON schema node | ||
| * @returns {Object} An object containing: | ||
| * - scopes {Map} - A Map of the validation scopes (with IDs as keys) | ||
| * - getScope {Function} - Function to retrieve a scope by name/ID | ||
| * - validate {Function} - Function to evaluate a validation rule | ||
| * - applyValidationRuleInCondition {Function} - Evaluate a validation rule used in a condition | ||
| * - applyComputedValueInField {Function} - Evaluate a computed value rule for a field | ||
| * - applyComputedValueRuleInCondition {Function} - Evaluate a computed value rule used in a condition | ||
| */ | ||
| export function createValidationChecker(schema) { | ||
| const scopes = new Map(); | ||
|
|
||
| function createScopes(jsonSchema, key = 'root') { | ||
johnstonbl01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| scopes.set(key, createValidationsScope(jsonSchema)); | ||
| Object.entries(jsonSchema?.properties ?? {}) | ||
| .filter(([, property]) => property.type === 'object' || property.type === 'array') | ||
| .forEach(([key, property]) => { | ||
| if (property.type === 'array') { | ||
| createScopes(property.items, `${key}[]`); | ||
| } else { | ||
| createScopes(property, key); | ||
| } | ||
| }); | ||
brennj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| createScopes(schema); | ||
|
|
||
| return { | ||
| scopes, | ||
| getScope(name = 'root') { | ||
| return scopes.get(name); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createValidationsScope(schema) { | ||
| const validationMap = new Map(); | ||
| const computedValuesMap = new Map(); | ||
|
|
||
| const logic = schema?.['x-jsf-logic'] ?? { | ||
| validations: {}, | ||
| computedValues: {}, | ||
| }; | ||
|
|
||
| const validations = Object.entries(logic.validations ?? {}); | ||
| const computedValues = Object.entries(logic.computedValues ?? {}); | ||
|
|
||
| validations.forEach(([id, validation]) => { | ||
| validationMap.set(id, validation); | ||
| }); | ||
|
|
||
| computedValues.forEach(([id, computedValue]) => { | ||
| computedValuesMap.set(id, computedValue); | ||
| }); | ||
|
|
||
| function validate(rule, values) { | ||
| return jsonLogic.apply(rule, replaceUndefinedValuesWithNulls(values)); | ||
| } | ||
|
|
||
| return { | ||
| validationMap, | ||
| computedValuesMap, | ||
| validate, | ||
| applyValidationRuleInCondition(id, values) { | ||
| const validation = validationMap.get(id); | ||
| return validate(validation.rule, values); | ||
| }, | ||
| applyComputedValueInField(id, values) { | ||
| const validation = computedValuesMap.get(id); | ||
| return validate(validation.rule, values); | ||
| }, | ||
| applyComputedValueRuleInCondition(id, values) { | ||
| const validation = computedValuesMap.get(id); | ||
| return validate(validation.rule, values); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * We removed undefined values in this function as `json-logic` ignores them. | ||
| * Means we will always check against a value for validations. | ||
| * | ||
| * @param {Object} values - a set of values from a form | ||
| * @returns {Object} values object without any undefined | ||
| */ | ||
| function replaceUndefinedValuesWithNulls(values = {}) { | ||
| return Object.entries(values).reduce((prev, [key, value]) => { | ||
| return { ...prev, [key]: value === undefined ? null : value }; | ||
| }, {}); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a Yup validation test function with custom JSON Logic for a specific field. | ||
| * | ||
| * @param {Object} options - The options for creating the validation function. | ||
| * @param {Object} options.field - The field configuration object. | ||
| * @param {string} options.field.name - The name of the field. | ||
| * @param {Object} options.logic - The logic object containing validation scopes and rules. | ||
| * @param {Object} options.config - Additional configuration options. | ||
| * @param {string} options.id - The ID of the validation rule. | ||
| * @param {string} [options.config.parentID='root'] - The ID of the validation rule scope. | ||
| * @returns {Function} A Yup validation test function. | ||
| */ | ||
| export function yupSchemaWithCustomJSONLogic({ field, logic, config, id }) { | ||
| const { parentID = 'root' } = config; | ||
| const validation = logic.getScope(parentID).validationMap.get(id); | ||
|
|
||
| return (yupSchema) => | ||
| yupSchema.test( | ||
| `${field.name}-validation-${id}`, | ||
| validation?.errorMessage ?? 'This field is invalid.', | ||
| (value, { parent }) => { | ||
| if (value === undefined && !field.required) return true; | ||
| return jsonLogic.apply(validation.rule, parent); | ||
| } | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.