From 77de4f94db9796e72682ce39193dab3c1b8a6853 Mon Sep 17 00:00:00 2001 From: Leonardo Rivera Date: Thu, 31 Oct 2024 09:23:43 -0400 Subject: [PATCH 1/2] bug fix - nested restrictions --- packages/client/src/index.ts | 1 + packages/validation/src/parseValues/matchCodeListFormatting.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index ce41fb2..23d8c98 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -45,6 +45,7 @@ export type { ParseDictionaryData, ParseDictionaryFailure, ParseDictionaryResult, + ParseFieldError, ParseSchemaError, ParseSchemaFailureData, ParseSchemaResult, diff --git a/packages/validation/src/parseValues/matchCodeListFormatting.ts b/packages/validation/src/parseValues/matchCodeListFormatting.ts index 9553686..03ffe55 100644 --- a/packages/validation/src/parseValues/matchCodeListFormatting.ts +++ b/packages/validation/src/parseValues/matchCodeListFormatting.ts @@ -29,7 +29,7 @@ const collectAllNestedCodeLists = ( return TypeUtils.asArray(restrictions).flatMap((restrictionsObject) => { if ('if' in restrictionsObject) { const thenCodeLists = restrictionsObject.then ? collectAllNestedCodeLists(restrictionsObject.then) : []; - const elseCodeLists = restrictionsObject.else ? collectAllNestedCodeLists(restrictionsObject) : []; + const elseCodeLists = restrictionsObject.else ? collectAllNestedCodeLists(restrictionsObject.else) : []; return [...thenCodeLists, ...elseCodeLists]; } else { return restrictionsObject.codeList ? restrictionsObject.codeList : []; From 82fe79f69e16ab70da99881d33d09d7fd29ae060 Mon Sep 17 00:00:00 2001 From: Leonardo Rivera Date: Thu, 14 Nov 2024 11:51:35 -0500 Subject: [PATCH 2/2] add unit test on conditional restrictions --- ...StringConditionalExistsWithouthThenElse.ts | 27 +++++++++++++++++ .../test/parseValues/parseField.spec.ts | 30 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 packages/validation/test/fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse.ts diff --git a/packages/validation/test/fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse.ts b/packages/validation/test/fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse.ts new file mode 100644 index 0000000..3943ac3 --- /dev/null +++ b/packages/validation/test/fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse.ts @@ -0,0 +1,27 @@ +import { SchemaField, type SchemaStringField } from '@overture-stack/lectern-dictionary'; +import { fieldStringNoRestriction } from '../noRestrictions/fieldStringNoRestriction'; +import { validateFixture } from '../../../testUtils/validateFixture'; + +export const fieldStringConditionalExistsWithouthThenElse = { + name: 'conditional-field', + valueType: 'string', + description: 'Required if `fieldStringNoRestriction` field exists, otherwise must be empty', + restrictions: { + if: { + conditions: [ + { + fields: [fieldStringNoRestriction.name], + match: { + exists: true, + }, + }, + ], + }, + }, +} as const satisfies SchemaStringField; + +validateFixture( + fieldStringConditionalExistsWithouthThenElse, + SchemaField, + 'fieldStringConditionalExistsWithouthThenElse is not a valid SchemaField', +); diff --git a/packages/validation/test/parseValues/parseField.spec.ts b/packages/validation/test/parseValues/parseField.spec.ts index d89101f..70e6d44 100644 --- a/packages/validation/test/parseValues/parseField.spec.ts +++ b/packages/validation/test/parseValues/parseField.spec.ts @@ -30,6 +30,8 @@ import { fieldBooleanArrayRequired } from '../fixtures/fields/simpleRestrictions import { fieldStringCodeList } from '../fixtures/fields/simpleRestrictions/string/fieldStringCodeList'; import { codeListString } from '../fixtures/restrictions/codeListsFixtures'; import { fieldStringArrayCodeList } from '../fixtures/fields/simpleRestrictions/string/fieldStringArrayCodeList'; +import { fieldStringConditionalExists } from '../fixtures/fields/conditionalRestrictions/fieldStringConditionalExists'; +import { fieldStringConditionalExistsWithouthThenElse } from '../fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse'; describe('Parse Values - parseFieldValue', () => { describe('Single Value Fields', () => { @@ -165,6 +167,34 @@ describe('Parse Values - parseFieldValue', () => { expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringNoRestriction).success).true; expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringNoRestriction).data).equals('!@#$%^&* ()_+'); }); + it('Successfuly parses strings, with conditional restrictions', () => { + const value = 'any random string value!!!'; + const result = parseFieldValue(value, fieldStringConditionalExists); + expect(result.success).true; + expect(result.data).equal(value); + + expect(parseFieldValue(' 123', fieldStringConditionalExists).success).true; + expect(parseFieldValue(' 123', fieldStringConditionalExists).data).equals('123'); + expect(parseFieldValue('false ', fieldStringConditionalExists).success).true; + expect(parseFieldValue('false ', fieldStringConditionalExists).data).equals('false'); + expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExists).success).true; + expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExists).data).equals('!@#$%^&* ()_+'); + }); + it('Successfuly parses strings, with conditional restrictions without then or else', () => { + const value = 'any random string value!!!'; + const result = parseFieldValue(value, fieldStringConditionalExistsWithouthThenElse); + expect(result.success).true; + expect(result.data).equal(value); + + expect(parseFieldValue(' 123', fieldStringConditionalExistsWithouthThenElse).success).true; + expect(parseFieldValue(' 123', fieldStringConditionalExistsWithouthThenElse).data).equals('123'); + expect(parseFieldValue('false ', fieldStringConditionalExistsWithouthThenElse).success).true; + expect(parseFieldValue('false ', fieldStringConditionalExistsWithouthThenElse).data).equals('false'); + expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExistsWithouthThenElse).success).true; + expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExistsWithouthThenElse).data).equals( + '!@#$%^&* ()_+', + ); + }); it('Updates string to match formatting of codeList value', () => { const value = 'banana'; const result = parseFieldValue(value, fieldStringCodeList);