Skip to content

Commit

Permalink
fix: check for array child enum (#163)
Browse files Browse the repository at this point in the history
* fix: check for array child enum

* chore: update snapshot

Co-authored-by: Nauman <[email protected]>
  • Loading branch information
Domagoj Kriskovic and mnaumanali94 authored Aug 24, 2021
1 parent d3b7fa6 commit ddc09d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/index.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3617,6 +3617,13 @@ exports[`HTML Output should match tickets.schema.json 1`] = `
</div>
</div>
</div>
<div>
<span>Allowed values:</span>
<span>POSTAL</span>
<span>PICK_UP_STATION</span>
<span>E-MAIL</span>
<span>LOYALTY_CARD</span>
</div>
</div>
<div></div>
</div>
Expand All @@ -3633,6 +3640,13 @@ exports[`HTML Output should match tickets.schema.json 1`] = `
</div>
</div>
</div>
<div>
<span>Allowed values:</span>
<span>BOOKER</span>
<span>CUSTOMER</span>
<span>PASSENGER</span>
<span>THIRD_PARTY</span>
</div>
</div>
<div></div>
</div>
Expand Down
12 changes: 10 additions & 2 deletions src/components/shared/Validations.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RegularNode } from '@stoplight/json-schema-tree';
import { isRegularNode, RegularNode } from '@stoplight/json-schema-tree';
import { Flex, Text } from '@stoplight/mosaic';
import { Dictionary } from '@stoplight/types';
import capitalize from 'lodash/capitalize.js';
Expand Down Expand Up @@ -202,7 +202,15 @@ export function validationCount(schemaNode: RegularNode) {

export function getValidationsFromSchema(schemaNode: RegularNode) {
return {
...(schemaNode.enum !== null ? { enum: schemaNode.enum } : null),
...(schemaNode.enum !== null
? { enum: schemaNode.enum }
: // in case schemaNode is type: "array", check if its child have defined enum
schemaNode.primaryType === 'array' &&
schemaNode.children?.length === 1 &&
isRegularNode(schemaNode.children[0]) &&
schemaNode.children[0].enum !== null
? { enum: schemaNode.children[0].enum }
: null),
...('annotations' in schemaNode
? {
...(schemaNode.annotations.default ? { default: schemaNode.annotations.default } : null),
Expand Down
20 changes: 19 additions & 1 deletion src/components/shared/__tests__/Validations.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'jest-enzyme';

import { RegularNode } from '@stoplight/json-schema-tree';
import { isRegularNode, RegularNode } from '@stoplight/json-schema-tree';
import { mount } from 'enzyme';
import * as React from 'react';

import { Validations } from '../../shared';
import { getValidationsFromSchema } from '../Validations';
import { buildTree } from './utils';

describe('Validations component', () => {
it('should render number type validations', () => {
Expand Down Expand Up @@ -48,6 +49,23 @@ describe('Validations component', () => {
expect(wrapper).toIncludeText('Allowed value:bar');
});

it('should check for array child enum', () => {
const tree = buildTree({
type: 'array',
items: {
enum: ['p1', 'p2', 'p3'],
},
});

const node = tree.children[0] as RegularNode;

expect(isRegularNode(node)).toBe(true);
const validations = getValidationsFromSchema(node);
const wrapper = mount(<Validations validations={validations} />);

expect(wrapper).toIncludeText('Allowed values:p1p2p3');
});

it('should not render hidden example validations', () => {
const node = new RegularNode({
type: 'number',
Expand Down

0 comments on commit ddc09d8

Please sign in to comment.