Skip to content

Add tests for conditionally required logic #2

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 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 10 additions & 4 deletions packages/core/src/configDefault.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -45,4 +45,10 @@ export const configDefault = {
* [text] if asterisks in labels for required fields should be hidden
*/
hideRequiredAsterisk: false,

/**
* [text] if dynamic checks for conditional application of properties
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[text] ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is according to the other comments about config properties

* should be performed (e.g. check for conditional required)
*/
allowDynamicCheck: false,
};
65 changes: 46 additions & 19 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ import {
} from '../i18n/arrayTranslations';
import { resolveSchema } from './resolvers';
import cloneDeep from 'lodash/cloneDeep';
import { has } from 'lodash';
import { all, any } from 'lodash/fp';
import isEqual from 'lodash/isEqual';
import has from 'lodash/has';
import any from 'lodash/fp/any';
import all from 'lodash/fp/all';

const checkDataCondition = (
propertyCondition: unknown,
Expand All @@ -92,12 +94,15 @@ const checkDataCondition = (
) => {
if (has(propertyCondition, 'const')) {
return (
has(data, property) && data[property] === get(propertyCondition, 'const')
has(data, property) &&
isEqual(data[property], get(propertyCondition, 'const'))
);
} else if (has(propertyCondition, 'enum')) {
return (
has(data, property) &&
(get(propertyCondition, 'enum') as unknown[]).includes(data[property])
(get(propertyCondition, 'enum') as unknown[]).find((value) =>
isEqual(value, data[property])
) !== undefined
);
} else if (has(propertyCondition, 'pattern')) {
const pattern = new RegExp(get(propertyCondition, 'pattern'));
Expand Down Expand Up @@ -169,11 +174,14 @@ const evaluateCondition = (
let satisfied = false;

for (let i = 0; i < subschemas.length; i++) {
if (satisfied) {
const current = evaluateCondition(subschemas[i], data);
if (current && satisfied) {
return false;
}

satisfied = evaluateCondition(subschemas[i], data);
if (current && !satisfied) {
satisfied = true;
}
}

return satisfied;
Expand All @@ -185,21 +193,25 @@ const evaluateCondition = (
}

const requiredCondition = all(
(property) => data[property],
(property) => has(data, property),
requiredProperties
);

const propertiesCondition = get(schema, 'properties') as Record<
string,
unknown
>;
if (has(schema, 'properties')) {
const propertiesCondition = get(schema, 'properties') as Record<
string,
unknown
>;

const valueCondition = all(
(property) => checkPropertyCondition(propertiesCondition, property, data),
Object.keys(propertiesCondition)
);
const valueCondition = all(
(property) => checkPropertyCondition(propertiesCondition, property, data),
Object.keys(propertiesCondition)
);

return requiredCondition && valueCondition;
return requiredCondition && valueCondition;
}

return requiredCondition;
};

/**
Expand Down Expand Up @@ -359,7 +371,8 @@ const isRequired = (
schema: JsonSchema,
schemaPath: string,
rootSchema: JsonSchema,
data: any
data: any,
config: any
): boolean => {
const pathSegments = schemaPath.split('/');
const lastSegment = pathSegments[pathSegments.length - 1];
Expand All @@ -376,6 +389,14 @@ const isRequired = (
);
const currentData = Resolve.data(data, toDataPath(nextHigherSchemaPath));

if (!config?.allowDynamicCheck) {
return (
nextHigherSchema !== undefined &&
nextHigherSchema.required !== undefined &&
nextHigherSchema.required.indexOf(lastSegment) !== -1
);
}

const requiredInIf =
has(nextHigherSchema, 'if') &&
checkRequiredInIf(nextHigherSchema, lastSegment, [], currentData);
Expand Down Expand Up @@ -797,9 +818,16 @@ export const mapStateToControlProps = (
const controlElement = uischema as ControlElement;
const id = ownProps.id;
const rootSchema = getSchema(state);
const config = getConfig(state);
const required =
controlElement.scope !== undefined &&
isRequired(ownProps.schema, controlElement.scope, rootSchema, rootData);
isRequired(
ownProps.schema,
controlElement.scope,
rootSchema,
rootData,
config
);
const resolvedSchema = Resolve.schema(
ownProps.schema || rootSchema,
controlElement.scope,
Expand All @@ -812,7 +840,6 @@ export const mapStateToControlProps = (
const data = Resolve.data(rootData, path);
const labelDesc = createLabelDescriptionFrom(uischema, resolvedSchema);
const label = labelDesc.show ? labelDesc.text : '';
const config = getConfig(state);
const enabled: boolean = isInherentlyEnabled(
state,
ownProps,
Expand Down
Loading