Skip to content
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

fix(fieldset): support root conditionals for fieldsets #23

Merged
merged 31 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3312f29
create playground to test things
gabrielremote Jun 7, 2023
bd02e44
docs(select-validation) - extract options values into an array
gabrielremote Jun 14, 2023
049e9e2
docs(select-validation) - extract options values into an array
gabrielremote Jun 14, 2023
be51f44
Merge branch 'main' into rmt-23-jsf-select-validation-is-incomplete
gabrielremote Jun 21, 2023
d265ea9
feat(select) - update yupSchema
gabrielremote Jun 21, 2023
f2dfe7f
fix(yupSchema) - tests
gabrielremote Jun 21, 2023
bc07113
add whitespace to yupSchemas
gabrielremote Jun 21, 2023
7aa64e4
docs(select) - add tests for select
gabrielremote Jun 22, 2023
3ec54ff
remove comments
gabrielremote Jun 22, 2023
d0415e9
Merge branch 'main' into rmt-23-jsf-select-validation-is-incomplete
gabrielremote Jun 22, 2023
35c4dd4
add missing assertions
gabrielremote Jun 22, 2023
f8a8736
Revert "create playground to test things"
sandrina-p Jul 2, 2023
2d2fdc6
Handle null and empty string. Set custom error message. Add more test…
sandrina-p Jul 2, 2023
58e0efc
Sandrina's self-review
sandrina-p Jul 2, 2023
54e090d
fix the bug with a lot of changes. clean next
sandrina-p Jul 1, 2023
48a60ab
Clean unnecessary changes - commit it for future story
sandrina-p Jul 1, 2023
d51ccd2
write test
sandrina-p Jul 2, 2023
1c7ed57
branch-of-branch - verify old option is no longer valid
sandrina-p Jul 2, 2023
5ccfda9
Release 0.4.1-dev.20230702181843
sandrina-p Jul 2, 2023
989d3fd
also allow null in required select/radio fields
sandrina-p Jul 3, 2023
58b2850
Release 0.4.1-dev.20230703082439
sandrina-p Jul 3, 2023
fc00b24
fix schema type string -> number
sandrina-p Jul 3, 2023
3b261f6
fix incorrect json schema - default value was invalid
sandrina-p Jul 3, 2023
fea8d28
Still allow "null" in conventional empty fields
sandrina-p Jul 3, 2023
aa4c2fc
Release 0.4.1-dev.20230703161935
sandrina-p Jul 3, 2023
6679325
handle conditionals that include select fields
sandrina-p Jul 3, 2023
433603a
Release 0.4.1-dev.20230703195833
sandrina-p Jul 3, 2023
ec2b2eb
refactor ''/null edge-case to use .transform()
sandrina-p Jul 3, 2023
21d2f64
Release 0.4.1-dev.20230703203242
sandrina-p Jul 3, 2023
ecc7d18
Merge remote-tracking branch 'origin' into rmt-517-support-root-condi…
sandrina-p Jul 3, 2023
bbd5927
Revert back to 0.4.0-beta.0
sandrina-p Jul 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/calculateConditionalProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import { buildYupSchema } from './yupSchema';
/**
* Verifies if a field is required
* @param {Object} node - JSON schema parent node
* @param {String} inputName - input name
* @param {String} field - input name
* @return {Boolean}
*/
function isFieldRequired(node, inputName) {
// For nested properties (case of fieldset) we need to check recursively
if (node?.required) {
return node.required.includes(inputName);
}

return false;
function isFieldRequired(node, field) {
return (
// Check base root required
field.scopedJsonSchema?.required?.includes(field.name) ||
sandrina-p marked this conversation as resolved.
Show resolved Hide resolved
// Check conditional required
node?.required?.includes(field.name)
);
}

/**
Expand All @@ -32,25 +32,34 @@ function isFieldRequired(node, inputName) {
* @param {Object} property - property that relates with the list of fields
* @returns {Object}
*/
function rebuildInnerFieldsRequiredProperty(fields, property) {
function rebuildFieldset(fields, property) {
sandrina-p marked this conversation as resolved.
Show resolved Hide resolved
if (property?.properties) {
return fields.map((field) => {
const propertyConditionals = property.properties[field.name];
if (!propertyConditionals) {
return field;
}

const newFieldParams = extractParametersFromNode(propertyConditionals);
sandrina-p marked this conversation as resolved.
Show resolved Hide resolved

if (field.fields) {
return {
...field,
fields: rebuildInnerFieldsRequiredProperty(field.fields, property.properties[field.name]),
...newFieldParams,
fields: rebuildFieldset(field.fields, propertyConditionals),
};
}
return {
...field,
required: isFieldRequired(property, field.name),
...newFieldParams,
required: isFieldRequired(property, field),
};
});
}

return fields.map((field) => ({
...field,
required: isFieldRequired(property, field.name),
required: isFieldRequired(property, field),
}));
}

Expand Down Expand Up @@ -85,10 +94,7 @@ export function calculateConditionalProperties(fieldParams, customProperties) {
let fieldSetFields;

if (fieldParams.inputType === supportedTypes.FIELDSET) {
sandrina-p marked this conversation as resolved.
Show resolved Hide resolved
fieldSetFields = rebuildInnerFieldsRequiredProperty(
fieldParams.fields,
conditionalProperty
);
fieldSetFields = rebuildFieldset(fieldParams.fields, conditionalProperty);
newFieldParams.fields = fieldSetFields;
}

Expand Down
2 changes: 2 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ function processNode(node, formValues, formFields, accRequired = new Set()) {

if (node.if) {
const matchesCondition = checkIfConditionMatches(node, formValues, formFields);
// BUG HERE (unreleated) - what if it matches but doesn't has a then,
// it should do nothing, but instead it jumps to node.else when it shouldn't.
sandrina-p marked this conversation as resolved.
Show resolved Hide resolved
if (matchesCondition && node.then) {
const { required: branchRequired } = processNode(
node.then,
Expand Down
Loading