Skip to content

Commit 1c11c12

Browse files
authored
fix: array child pattern match validation (#248)
1 parent ea0ac6f commit 1c11c12

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

src/components/shared/Validations.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,24 @@ export function validationCount(schemaNode: RegularNode) {
183183
return uniq(validationKeys.map(key => ([...numberValidationNames].includes(key) ? 'number' : key))).length;
184184
}
185185

186+
const getArrayValidations = (schemaNode: RegularNode) => {
187+
if (schemaNode.children?.length === 1 && isRegularNode(schemaNode.children[0])) {
188+
if (schemaNode.children[0].enum !== null) {
189+
return { enum: schemaNode.children[0].enum };
190+
} else if (schemaNode.children[0].fragment.pattern !== void 0) {
191+
return { pattern: schemaNode.children[0].fragment.pattern };
192+
}
193+
}
194+
return null;
195+
};
196+
186197
export function getValidationsFromSchema(schemaNode: RegularNode) {
187198
return {
188199
...(schemaNode.enum !== null
189200
? { enum: schemaNode.enum }
190-
: // in case schemaNode is type: "array", check if its child have defined enum
191-
schemaNode.primaryType === 'array' &&
192-
schemaNode.children?.length === 1 &&
193-
isRegularNode(schemaNode.children[0]) &&
194-
schemaNode.children[0].enum !== null
195-
? { enum: schemaNode.children[0].enum }
201+
: schemaNode.primaryType === 'array'
202+
? // in case schemaNode is type: "array", check if its child has an additional validation
203+
getArrayValidations(schemaNode)
196204
: null),
197205
...('annotations' in schemaNode
198206
? {

src/components/shared/__tests__/Validations.spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ describe('Validations component', () => {
6666
expect(wrapper).toIncludeText('Allowed values:p1p2p3');
6767
});
6868

69+
it('should check for array child fragment.pattern', () => {
70+
const tree = buildTree({
71+
type: 'array',
72+
items: {
73+
type: 'string',
74+
pattern: '^[a-z0-9]{13}$',
75+
},
76+
});
77+
78+
const node = tree.children[0] as RegularNode;
79+
80+
expect(isRegularNode(node)).toBe(true);
81+
const validations = getValidationsFromSchema(node);
82+
const wrapper = mount(<Validations validations={validations} />);
83+
84+
expect(wrapper).toIncludeText('Match pattern:^[a-z0-9]{13}$');
85+
});
86+
6987
it('should not render hidden example validations', () => {
7088
const node = new RegularNode({
7189
type: 'number',

0 commit comments

Comments
 (0)