mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-22 16:46:34 +03:00
feat: display multipleOf
constrains (#1065)
This commit is contained in:
parent
dc5430e53d
commit
3e90133664
|
@ -738,6 +738,7 @@ components:
|
||||||
type: number
|
type: number
|
||||||
description: Average amount of honey produced per day in ounces
|
description: Average amount of honey produced per day in ounces
|
||||||
example: 3.14
|
example: 3.14
|
||||||
|
multipleOf: .01
|
||||||
required:
|
required:
|
||||||
- honeyPerDay
|
- honeyPerDay
|
||||||
Id:
|
Id:
|
||||||
|
|
|
@ -333,7 +333,8 @@ describe('Utils', () => {
|
||||||
const itemConstraintSchema = (
|
const itemConstraintSchema = (
|
||||||
min: number | undefined = undefined,
|
min: number | undefined = undefined,
|
||||||
max: number | undefined = undefined,
|
max: number | undefined = undefined,
|
||||||
) => ({ type: 'array', minItems: min, maxItems: max });
|
multipleOf: number | undefined = undefined,
|
||||||
|
) => ({ type: 'array', minItems: min, maxItems: max, multipleOf });
|
||||||
|
|
||||||
it('should not have a humanized constraint without schema constraints', () => {
|
it('should not have a humanized constraint without schema constraints', () => {
|
||||||
expect(humanizeConstraints(itemConstraintSchema())).toHaveLength(0);
|
expect(humanizeConstraints(itemConstraintSchema())).toHaveLength(0);
|
||||||
|
@ -355,9 +356,21 @@ describe('Utils', () => {
|
||||||
expect(humanizeConstraints(itemConstraintSchema(7, 7))).toContain('7 items');
|
expect(humanizeConstraints(itemConstraintSchema(7, 7))).toContain('7 items');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should have a humazined constraint when justMinItems is set, and it is equal to 1', () => {
|
it('should have a humanized constraint when justMinItems is set, and it is equal to 1', () => {
|
||||||
expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty');
|
expect(humanizeConstraints(itemConstraintSchema(1))).toContain('non-empty');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should have a humanized constraint when multipleOf is set, and it is in format of /^0\\.0*1$/', () => {
|
||||||
|
expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.01))).toContain(
|
||||||
|
'decimal places <= 2',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have a humanized constraint when multipleOf is set, and it is in format other than /^0\\.0*1$/', () => {
|
||||||
|
expect(humanizeConstraints(itemConstraintSchema(undefined, undefined, 0.5))).toContain(
|
||||||
|
'multiple of 0.5',
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('OpenAPI pluralizeType', () => {
|
describe('OpenAPI pluralizeType', () => {
|
||||||
|
@ -387,7 +400,9 @@ describe('Utils', () => {
|
||||||
expect(pluralizeType('objects (Pet)')).toEqual('objects (Pet)');
|
expect(pluralizeType('objects (Pet)')).toEqual('objects (Pet)');
|
||||||
expect(pluralizeType('strings <email>')).toEqual('strings <email>');
|
expect(pluralizeType('strings <email>')).toEqual('strings <email>');
|
||||||
expect(pluralizeType('objects or strings')).toEqual('objects or strings');
|
expect(pluralizeType('objects or strings')).toEqual('objects or strings');
|
||||||
expect(pluralizeType('objects (Pet) or numbers <int64>')).toEqual('objects (Pet) or numbers <int64>');
|
expect(pluralizeType('objects (Pet) or numbers <int64>')).toEqual(
|
||||||
|
'objects (Pet) or numbers <int64>',
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -368,6 +368,17 @@ export function isNamedDefinition(pointer?: string): boolean {
|
||||||
return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
|
return /^#\/components\/schemas\/[^\/]+$/.test(pointer || '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function humanizeMultipleOfConstraint(multipleOf: number | undefined): string | undefined {
|
||||||
|
if (multipleOf === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const strigifiedMultipleOf = multipleOf.toString(10);
|
||||||
|
if (!/^0\.0*1$/.test(strigifiedMultipleOf)) {
|
||||||
|
return `multiple of ${strigifiedMultipleOf}`;
|
||||||
|
}
|
||||||
|
return `decimal places <= ${strigifiedMultipleOf.split('.')[1].length}`;
|
||||||
|
}
|
||||||
|
|
||||||
function humanizeRangeConstraint(
|
function humanizeRangeConstraint(
|
||||||
description: string,
|
description: string,
|
||||||
min: number | undefined,
|
min: number | undefined,
|
||||||
|
@ -406,6 +417,11 @@ export function humanizeConstraints(schema: OpenAPISchema): string[] {
|
||||||
res.push(arrayRange);
|
res.push(arrayRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const multipleOfConstraint = humanizeMultipleOfConstraint(schema.multipleOf);
|
||||||
|
if (multipleOfConstraint !== undefined) {
|
||||||
|
res.push(multipleOfConstraint);
|
||||||
|
}
|
||||||
|
|
||||||
let numberRange;
|
let numberRange;
|
||||||
if (schema.minimum !== undefined && schema.maximum !== undefined) {
|
if (schema.minimum !== undefined && schema.maximum !== undefined) {
|
||||||
numberRange = schema.exclusiveMinimum ? '( ' : '[ ';
|
numberRange = schema.exclusiveMinimum ? '( ' : '[ ';
|
||||||
|
|
Loading…
Reference in New Issue
Block a user