Skip to content

Commit

Permalink
feat: do not display min/max values for oas formats (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Apr 9, 2021
1 parent 01724d4 commit b7d90bb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/components/shared/Validations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ const validationFormatters: Record<string, (value: unknown) => ValidationFormat
default: createValidationsFormatter('Default'),
};

const oasFormats = {
int32: {
minimum: 0 - 2 ** 31,
maximum: 2 ** 31 - 1,
},
int64: {
minimum: 0 - 2 ** 63,
maximum: 2 ** 63 - 1,
},
float: {
minimum: 0 - 2 ** 128,
maximum: 2 ** 128 - 1,
},
double: {
minimum: 0 - Number.MAX_VALUE,
maximum: Number.MAX_VALUE,
},
byte: {
pattern: '^[\\w\\d+\\/=]*$',
},
};

function filterOutOasFormatValidations(format: string, values: Dictionary<unknown>) {
if (!(format in oasFormats)) return values;

const newValues = { ...values };

for (const [key, value] of Object.entries(oasFormats[format])) {
if (value === newValues[key]) {
delete newValues[key];
}
}

return newValues;
}

export const Validations: React.FunctionComponent<IValidations> = ({ validations }) => {
const numberValidations = pick(validations, numberValidationNames);
const booleanValidations = omit(
Expand Down Expand Up @@ -172,6 +208,14 @@ export function getValidationsFromSchema(schemaNode: RegularNode) {
...(schemaNode.annotations['x-example'] ? { ['x-example']: schemaNode.annotations['x-example'] } : null),
}
: null),
...schemaNode.validations,
...getFilteredValidations(schemaNode),
};
}

function getFilteredValidations(schemaNode: RegularNode) {
if (schemaNode.format !== null) {
return filterOutOasFormatValidations(schemaNode.format, schemaNode.validations);
}

return schemaNode.validations;
}
30 changes: 30 additions & 0 deletions src/components/shared/__tests__/Validations.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,34 @@ describe('Validations component', () => {
expect(wrapper).toIncludeText('Example values:"Example 1""Example 2"');
expect(wrapper).toIncludeText('Allowed value:"bar"');
});

describe('OAS formats', () => {
it('given default range, should not render any validation', () => {
const node = new RegularNode({
type: 'integer',
format: 'int32',
minimum: 0 - Math.pow(2, 31),
maximum: Math.pow(2, 31) - 1,
});

const validations = getValidationsFromSchema(node);
const wrapper = mount(<Validations validations={validations} />);

expect(wrapper).toBeEmptyRender();
});

it('should render non-standard values', () => {
const node = new RegularNode({
type: 'integer',
format: 'int64',
minimum: 0,
maximum: Math.pow(2, 63) - 1,
});

const validations = getValidationsFromSchema(node);
const wrapper = mount(<Validations validations={validations} />);

expect(wrapper).toIncludeText('>= 0');
});
});
});

0 comments on commit b7d90bb

Please sign in to comment.