Skip to content

Commit 39fb5a8

Browse files
authoredNov 18, 2024··
Merge pull request #236 from overture-stack/bug_nested_restriction
bug fix - nested restrictions
2 parents f0b9473 + 82fe79f commit 39fb5a8

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
 

‎packages/client/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export type {
4545
ParseDictionaryData,
4646
ParseDictionaryFailure,
4747
ParseDictionaryResult,
48+
ParseFieldError,
4849
ParseSchemaError,
4950
ParseSchemaFailureData,
5051
ParseSchemaResult,

‎packages/validation/src/parseValues/matchCodeListFormatting.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const collectAllNestedCodeLists = (
2929
return TypeUtils.asArray(restrictions).flatMap((restrictionsObject) => {
3030
if ('if' in restrictionsObject) {
3131
const thenCodeLists = restrictionsObject.then ? collectAllNestedCodeLists(restrictionsObject.then) : [];
32-
const elseCodeLists = restrictionsObject.else ? collectAllNestedCodeLists(restrictionsObject) : [];
32+
const elseCodeLists = restrictionsObject.else ? collectAllNestedCodeLists(restrictionsObject.else) : [];
3333
return [...thenCodeLists, ...elseCodeLists];
3434
} else {
3535
return restrictionsObject.codeList ? restrictionsObject.codeList : [];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { SchemaField, type SchemaStringField } from '@overture-stack/lectern-dictionary';
2+
import { fieldStringNoRestriction } from '../noRestrictions/fieldStringNoRestriction';
3+
import { validateFixture } from '../../../testUtils/validateFixture';
4+
5+
export const fieldStringConditionalExistsWithouthThenElse = {
6+
name: 'conditional-field',
7+
valueType: 'string',
8+
description: 'Required if `fieldStringNoRestriction` field exists, otherwise must be empty',
9+
restrictions: {
10+
if: {
11+
conditions: [
12+
{
13+
fields: [fieldStringNoRestriction.name],
14+
match: {
15+
exists: true,
16+
},
17+
},
18+
],
19+
},
20+
},
21+
} as const satisfies SchemaStringField;
22+
23+
validateFixture(
24+
fieldStringConditionalExistsWithouthThenElse,
25+
SchemaField,
26+
'fieldStringConditionalExistsWithouthThenElse is not a valid SchemaField',
27+
);

‎packages/validation/test/parseValues/parseField.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import { fieldBooleanArrayRequired } from '../fixtures/fields/simpleRestrictions
3030
import { fieldStringCodeList } from '../fixtures/fields/simpleRestrictions/string/fieldStringCodeList';
3131
import { codeListString } from '../fixtures/restrictions/codeListsFixtures';
3232
import { fieldStringArrayCodeList } from '../fixtures/fields/simpleRestrictions/string/fieldStringArrayCodeList';
33+
import { fieldStringConditionalExists } from '../fixtures/fields/conditionalRestrictions/fieldStringConditionalExists';
34+
import { fieldStringConditionalExistsWithouthThenElse } from '../fixtures/fields/conditionalRestrictions/fieldStringConditionalExistsWithouthThenElse';
3335

3436
describe('Parse Values - parseFieldValue', () => {
3537
describe('Single Value Fields', () => {
@@ -165,6 +167,34 @@ describe('Parse Values - parseFieldValue', () => {
165167
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringNoRestriction).success).true;
166168
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringNoRestriction).data).equals('!@#$%^&* ()_+');
167169
});
170+
it('Successfuly parses strings, with conditional restrictions', () => {
171+
const value = 'any random string value!!!';
172+
const result = parseFieldValue(value, fieldStringConditionalExists);
173+
expect(result.success).true;
174+
expect(result.data).equal(value);
175+
176+
expect(parseFieldValue(' 123', fieldStringConditionalExists).success).true;
177+
expect(parseFieldValue(' 123', fieldStringConditionalExists).data).equals('123');
178+
expect(parseFieldValue('false ', fieldStringConditionalExists).success).true;
179+
expect(parseFieldValue('false ', fieldStringConditionalExists).data).equals('false');
180+
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExists).success).true;
181+
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExists).data).equals('!@#$%^&* ()_+');
182+
});
183+
it('Successfuly parses strings, with conditional restrictions without then or else', () => {
184+
const value = 'any random string value!!!';
185+
const result = parseFieldValue(value, fieldStringConditionalExistsWithouthThenElse);
186+
expect(result.success).true;
187+
expect(result.data).equal(value);
188+
189+
expect(parseFieldValue(' 123', fieldStringConditionalExistsWithouthThenElse).success).true;
190+
expect(parseFieldValue(' 123', fieldStringConditionalExistsWithouthThenElse).data).equals('123');
191+
expect(parseFieldValue('false ', fieldStringConditionalExistsWithouthThenElse).success).true;
192+
expect(parseFieldValue('false ', fieldStringConditionalExistsWithouthThenElse).data).equals('false');
193+
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExistsWithouthThenElse).success).true;
194+
expect(parseFieldValue(' !@#$%^&* ()_+ ', fieldStringConditionalExistsWithouthThenElse).data).equals(
195+
'!@#$%^&* ()_+',
196+
);
197+
});
168198
it('Updates string to match formatting of codeList value', () => {
169199
const value = 'banana';
170200
const result = parseFieldValue(value, fieldStringCodeList);

0 commit comments

Comments
 (0)
Please sign in to comment.