Skip to content

Commit

Permalink
BREAKING CHANGE: Rename value -> forcedValue (#66)
Browse files Browse the repository at this point in the history
* feat: rename value -> forcedValue

* Release 0.8.1-dev.20240209164254

* Revert "Release 0.8.1-dev.20240209164254"

This reverts commit 322c9e0.

* chore: add one test from chatting with Paula

* chore: trigger ci
  • Loading branch information
brennj committed Mar 11, 2024
1 parent 28cc278 commit 77c445a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,10 @@ export function extractParametersFromNode(schemaNode) {
const statementDescription = presentation.statement?.description;

const value =
typeof node.const !== 'undefined' && typeof node.default !== 'undefined'
? { value: node.const }
typeof node.const !== 'undefined' &&
typeof node.default !== 'undefined' &&
node.const === node.default
? { forcedValue: node.const }
: {};

return omitBy(
Expand Down Expand Up @@ -657,10 +659,12 @@ export const handleValuesChange = (fields, jsonSchema, config, logic) => (values
};

function getDecoratedComputedAttributes(computedAttributes) {
const isEqualConstAndDefault = computedAttributes?.const === computedAttributes?.default;

return {
...(computedAttributes ?? {}),
...(computedAttributes?.const && computedAttributes?.default
? { value: computedAttributes.const }
...(computedAttributes?.const && computedAttributes?.default && isEqualConstAndDefault
? { forcedValue: computedAttributes.const }
: {}),
};
}
42 changes: 39 additions & 3 deletions src/tests/const.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('validations: const', () => {
},
{ strictInputType: false }
);
expect(fields[0]).toMatchObject({ value: 10, const: 10, default: 10 });
expect(fields[0]).toMatchObject({ forcedValue: 10, const: 10, default: 10 });
});
});

Expand All @@ -130,7 +130,7 @@ describe('const/default with forced values', () => {
expect(handleValidation({ zero_only: 1 }).formErrors).toEqual({
zero_only: 'The only accepted value is 0.',
});
expect(fields[0]).toMatchObject({ value: 0, const: 0, default: 0 });
expect(fields[0]).toMatchObject({ forcedValue: 0, const: 0, default: 0 });
expect(handleValidation({ zero_only: 0 }).formErrors).toBeUndefined();
// null is also considered valid until we fix @BUG RMT-518
// Expectation: To fail with error "The only accepted value is 0."
Expand All @@ -151,9 +151,45 @@ describe('const/default with forced values', () => {
ten_only: 'The only accepted value is 10.',
});
expect(handleValidation({ ten_only: 10 }).formErrors).toBeUndefined();
expect(fields[0]).toMatchObject({ value: 10, const: 10, default: 10 });
expect(fields[0]).toMatchObject({ forcedValue: 10, const: 10, default: 10 });
// null is also considered valid until we fix @BUG RMT-518
// Expectation: To fail with error "The only accepted value is 10."
expect(handleValidation({ ten_only: null }).formErrors).toBeUndefined();
});

it('do not set a forced value if const and default do not equal', () => {
const { fields } = createHeadlessForm(
{
properties: {
bad_field_for_number: { type: 'number', const: 10, default: 20 },
},
},
{ strictInputType: false }
);
expect(fields[0]).not.toMatchObject({ forcedValue: expect.any(Number) });
});

it('Should work numbers with non-standard input types', () => {
const { fields, handleValidation } = createHeadlessForm(
{
properties: {
number: {
type: 'integer',
const: 300,
default: 300,
'x-jsf-presentation': {
inputType: 'money',
},
},
},
required: ['number'],
},
{ strictInputType: true }
);
expect(handleValidation({ number: 0 }).formErrors).toEqual({
number: 'The only accepted value is 300.',
});
expect(handleValidation({ number: 300 }).formErrors).toBeUndefined();
expect(fields[0]).toMatchObject({ forcedValue: 300 });
});
});
29 changes: 29 additions & 0 deletions src/tests/jsonLogic.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,35 @@ export const schemaWithComputedAttributes = {
},
};

export const badSchemaThatWillNotSetAForcedValue = {
properties: {
field_a: {
type: 'number',
},
field_b: {
type: 'number',
'x-jsf-logic-computedAttrs': {
const: 'a_times_three',
default: 'a_times_two',
},
},
},
'x-jsf-logic': {
computedValues: {
a_times_two: {
rule: {
'*': [{ var: 'field_a' }, 2],
},
},
a_times_three: {
rule: {
'*': [{ var: 'field_a' }, 3],
},
},
},
},
};

export const schemaWithInlineRuleForComputedAttributeWithoutCopy = {
properties: {
field_a: {
Expand Down
12 changes: 11 additions & 1 deletion src/tests/jsonLogic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
schemaWithUnknownVariableInComputedValues,
schemaWithUnknownVariableInValidations,
schemaWithValidationThatDoesNotExistOnProperty,
badSchemaThatWillNotSetAForcedValue,
} from './jsonLogic.fixtures';
import { mockConsole, restoreConsoleAndEnsureItWasNotCalled } from './testUtils';

Expand Down Expand Up @@ -335,12 +336,21 @@ describe('jsonLogic: cross-values validations', () => {
'This field is 2 times bigger than field_a with value of 4.'
);
expect(fieldB.default).toEqual(4);
expect(fieldB.value).toEqual(4);
expect(fieldB.forcedValue).toEqual(4);
handleValidation({ field_a: 4 });
expect(fieldB.default).toEqual(8);
expect(fieldB.label).toEqual('This is 8!');
});

it('A forced value will not be set when const and default are not equal', () => {
const { fields } = createHeadlessForm(badSchemaThatWillNotSetAForcedValue, {
strictInputType: false,
initialValues: { field_a: 2 },
});
expect(fields[1]).toMatchObject({ const: 6, default: 4 });
expect(fields[1]).not.toMatchObject({ forcedValue: expect.any(Number) });
});

it('Derived errorMessages and statements work', () => {
const { fields, handleValidation } = createHeadlessForm(
schemaWithComputedAttributesAndErrorMessages,
Expand Down

0 comments on commit 77c445a

Please sign in to comment.