Skip to content

Commit

Permalink
fix(fieldset): support root conditionals for fieldsets (#23)
Browse files Browse the repository at this point in the history
* create playground to test things

* docs(select-validation) - extract options values into an array

* docs(select-validation) - extract options values into an array

* feat(select) - update yupSchema

* fix(yupSchema) - tests

* add whitespace to yupSchemas

* docs(select) - add tests for select

* remove comments

* add missing assertions

* Revert "create playground to test things"

This reverts commit 3312f29.

* Handle null and empty string. Set custom error message. Add more tests and examples

* Sandrina's self-review

* fix the bug with a lot of changes. clean next

* Clean unnecessary changes - commit it for future story

* write test

* branch-of-branch - verify old option is no longer valid

* Release 0.4.1-dev.20230702181843

* also allow null in required select/radio fields

* Release 0.4.1-dev.20230703082439

* fix schema type string -> number

* fix incorrect json schema - default value was invalid

* Still allow "null" in conventional empty fields

* Release 0.4.1-dev.20230703161935

* handle conditionals that include select fields

* Release 0.4.1-dev.20230703195833

* refactor ''/null edge-case to use .transform()

* Release 0.4.1-dev.20230703203242

* Revert back to 0.4.0-beta.0

---------

Co-authored-by: Gabriel <gabriel.garcia@remote.com>
  • Loading branch information
sandrina-p and gabrielremote committed Jul 3, 2023
1 parent 37501d2 commit 65d87b3
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 199 deletions.
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) ||
// 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) {
if (property?.properties) {
return fields.map((field) => {
const propertyConditionals = property.properties[field.name];
if (!propertyConditionals) {
return field;
}

const newFieldParams = extractParametersFromNode(propertyConditionals);

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) {
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.
if (matchesCondition && node.then) {
const { required: branchRequired } = processNode(
node.then,
Expand Down
Loading

0 comments on commit 65d87b3

Please sign in to comment.