Skip to content

Commit

Permalink
fix(rulesets): exclusiveMinimum must be a number thrown for OAS 3.0 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 25, 2021
1 parent f808053 commit 65d287a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ testRule('oas3-valid-schema-example', [
errors: [],
},

{
name: `${field} containing a valid default example with booleanish exclusiveMinimum & exclusiveMaximum`,
document: {
openapi: '3.0.2',
[field]: {
schemas: {
xoxo: {
type: 'number',
minimum: 1,
maximum: 3,
exclusiveMinimum: true,
exclusiveMaximum: true,
example: 2,
},
},
},
},
errors: [],
},

{
name: `invalid simple example in ${field}`,
document: {
Expand Down
106 changes: 69 additions & 37 deletions packages/rulesets/src/oas/functions/__tests__/oasSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,48 +57,80 @@ describe('oasSchema', () => {
});
});

test('given OAS 3.0, supports nullable', () => {
const document = {
formats: new Set([oas3, oas3_0]),
};
describe('given OAS 3.0', () => {
it('supports nullable', () => {
const document = {
formats: new Set([oas3, oas3_0]),
};

const testSchema = {
type: 'object',
nullable: true,
properties: {
foo: {
type: 'number',
nullable: true,
const testSchema = {
type: 'object',
nullable: true,
properties: {
foo: {
type: 'number',
nullable: true,
},
},
},
};
};

expect(runSchema({}, testSchema, { document })).toEqual([]);
expect(runSchema(null, testSchema, { document })).toEqual([]);
expect(runSchema(2, testSchema, { document })).toEqual([
{
message: 'Value type must be object,null',
path: [],
},
]);
expect(runSchema({ foo: null }, testSchema, { document })).toEqual([]);
expect(runSchema({ foo: 2 }, testSchema, { document })).toEqual([]);
expect(runSchema({ foo: 'test' }, testSchema, { document })).toEqual([
{
message: '"foo" property type must be number,null',
path: ['foo'],
},
]);
expect(runSchema({}, testSchema, { document })).toEqual([]);
expect(runSchema(null, testSchema, { document })).toEqual([]);
expect(runSchema(2, testSchema, { document })).toEqual([
{
message: 'Value type must be object,null',
path: [],
},
]);
expect(runSchema({ foo: null }, testSchema, { document })).toEqual([]);
expect(runSchema({ foo: 2 }, testSchema, { document })).toEqual([]);
expect(runSchema({ foo: 'test' }, testSchema, { document })).toEqual([
{
message: '"foo" property type must be number,null',
path: ['foo'],
},
]);

expect(testSchema).toStrictEqual({
type: 'object',
nullable: true,
properties: {
foo: {
type: 'number',
nullable: true,
expect(testSchema).toStrictEqual({
type: 'object',
nullable: true,
properties: {
foo: {
type: 'number',
nullable: true,
},
},
},
});
});

it('supports booleanish exclusiveMinimum & exclusiveMaximum', () => {
const document = {
formats: new Set([oas3, oas3_0]),
};

const testSchema = {
type: 'number',
minimum: 1,
maximum: 3,
exclusiveMinimum: true,
exclusiveMaximum: true,
};

expect(runSchema(1, testSchema, { document })).toEqual([
{
message: 'Number must be > 1',
path: [],
},
]);

expect(runSchema(3, testSchema, { document })).toEqual([
{
message: 'Number must be < 3',
path: [],
},
]);

expect(runSchema(1.5, testSchema, { document })).toEqual([]);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/rulesets/src/oas/functions/oasSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ export default createRulesetFunction<unknown, Options>(

let { schema } = opts;

let dialect: SchemaOptions['dialect'];
let dialect: SchemaOptions['dialect'] = 'draft4';
let prepareResults: SchemaOptions['prepareResults'];

if (!formats) {
dialect = 'draft4';
dialect = 'auto';
} else if (formats.has(oas3_1)) {
if (isPlainObject(context.document.data) && typeof context.document.data.jsonSchemaDialect === 'string') {
dialect =
Expand Down

0 comments on commit 65d287a

Please sign in to comment.