Skip to content

Commit

Permalink
Merge pull request #236 from overture-stack/bug_nested_restriction
Browse files Browse the repository at this point in the history
bug fix - nested restrictions
  • Loading branch information
leoraba authored Nov 18, 2024
2 parents f0b9473 + 82fe79f commit 39fb5a8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export type {
ParseDictionaryData,
ParseDictionaryFailure,
ParseDictionaryResult,
ParseFieldError,
ParseSchemaError,
ParseSchemaFailureData,
ParseSchemaResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [];
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
);
30 changes: 30 additions & 0 deletions packages/validation/test/parseValues/parseField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 39fb5a8

Please sign in to comment.