Skip to content

Commit

Permalink
Upgrade to openapi3-ts v3.0.0 (epiphone#99)
Browse files Browse the repository at this point in the history
* Upgrade to `openapi3-ts` v3.0.0

* exclusive limits should be numbers

in [email protected] they are not using the v4 syntax anymore:

metadevpro/openapi3-ts@e7a0851

* Fix test

Co-authored-by: Daniel Varnai <[email protected]>
  • Loading branch information
epiphone and dvarnai authored Jan 14, 2023
1 parent 9e3eba3 commit be8d2f0
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions __tests__/defaultConverters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ describe('defaultConverters', () => {

isDivisibleByInt: { multipleOf: 4, type: 'number' },
isDivisibleByFloat: { multipleOf: 1.1, type: 'number' },
isPositive: { type: 'number', minimum: 0, exclusiveMinimum: true },
isNegative: { type: 'number', maximum: 0, exclusiveMaximum: true },
isPositive: { type: 'number', exclusiveMinimum: 0 },
isNegative: { type: 'number', exclusiveMaximum: 0 },
max: { type: 'number', maximum: 10 },
min: { type: 'number', minimum: 1 },

Expand Down
16 changes: 8 additions & 8 deletions __tests__/inheritedProperties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { JSONSchema, validationMetadatasToSchemas } from '../src'

@JSONSchema({
description: 'Contains email, password and phone',
summary: 'Base object',
title: 'Base object',
})
class BaseContent {
@JSONSchema({
Expand All @@ -29,7 +29,7 @@ class BaseContent {

@JSONSchema({
description: 'Password field',
summary: 'Password',
title: 'Password',
})
@IsString()
@IsOptional()
Expand All @@ -41,7 +41,7 @@ class BaseContent {
}

@JSONSchema({
summary: 'User object',
title: 'User object',
})
// @ts-ignore: not referenced
class User extends BaseContent {
Expand All @@ -57,7 +57,7 @@ class User extends BaseContent {
password: string

@JSONSchema({
summary: 'Mobile phone number',
title: 'Mobile phone number',
})
@IsOptional()
phone: string
Expand All @@ -82,7 +82,7 @@ describe('Inheriting validation decorators', () => {
},
password: {
description: 'Password field',
summary: 'Password',
title: 'Password',
type: 'string',
},
phone: {
Expand All @@ -92,7 +92,7 @@ describe('Inheriting validation decorators', () => {
},
},
required: ['email', 'phone'],
summary: 'Base object',
title: 'Base object',
type: 'object',
})

Expand All @@ -117,7 +117,7 @@ describe('Inheriting validation decorators', () => {
},
phone: {
format: 'mobile-phone',
summary: 'Mobile phone number',
title: 'Mobile phone number',
type: 'string',
not: { type: 'null' },
},
Expand All @@ -127,7 +127,7 @@ describe('Inheriting validation decorators', () => {
},
},
required: ['name', 'welcome', 'email'],
summary: 'User object',
title: 'User object',
type: 'object',
})
})
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"lodash.groupby": "^4.6.0",
"lodash.merge": "^4.6.2",
"openapi3-ts": "^2.0.2",
"openapi3-ts": "^3.0.0",
"reflect-metadata": "^0.1.13",
"tslib": "^2.4.1"
},
Expand Down
8 changes: 6 additions & 2 deletions src/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tslint:disable:ban-types
import { SchemaObject } from 'openapi3-ts'
import type { ReferenceObject, SchemaObject } from 'openapi3-ts'
import 'reflect-metadata'

import { IOptions } from './options'
Expand All @@ -12,8 +12,12 @@ const SCHEMA_KEY = Symbol('class-validator-jsonschema:JSONSchema')
* options, returning an updated schema.
*/
export type DecoratorSchema =
| ReferenceObject
| SchemaObject
| ((source: SchemaObject, options: IOptions) => SchemaObject)
| ((
source: SchemaObject,
options: IOptions
) => ReferenceObject | SchemaObject)

/**
* Supplement class or property with additional JSON Schema keywords.
Expand Down
17 changes: 9 additions & 8 deletions src/defaultConverters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:no-submodule-imports
import * as cv from 'class-validator'
import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata'
import { SchemaObject } from 'openapi3-ts'
import type { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata'
import type { ReferenceObject, SchemaObject } from 'openapi3-ts'
import 'reflect-metadata'

import { IOptions } from './options'
Expand All @@ -13,7 +13,7 @@ export interface ISchemaConverters {
export type SchemaConverter = (
meta: ValidationMetadata,
options: IOptions
) => SchemaObject | void
) => ReferenceObject | SchemaObject | void

export const defaultConverters: ISchemaConverters = {
[cv.ValidationTypes.CUSTOM_VALIDATION]: (meta, options) => {
Expand Down Expand Up @@ -126,13 +126,11 @@ export const defaultConverters: ISchemaConverters = {
type: 'number',
}),
[cv.IS_POSITIVE]: {
exclusiveMinimum: true,
minimum: 0,
exclusiveMinimum: 0,
type: 'number',
},
[cv.IS_NEGATIVE]: {
exclusiveMaximum: true,
maximum: 0,
exclusiveMaximum: 0,
type: 'number',
},
[cv.MIN]: (meta) => ({
Expand Down Expand Up @@ -381,7 +379,10 @@ function constraintToSchema(primitive: any): SchemaObject | void {
}
}

function targetToSchema(type: any, options: IOptions): SchemaObject | void {
function targetToSchema(
type: any,
options: IOptions
): ReferenceObject | SchemaObject | void {
if (typeof type === 'function') {
if (
type.prototype === String.prototype ||
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as cv from 'class-validator'
import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata'
import _groupBy from 'lodash.groupby'
import _merge from 'lodash.merge'
import { SchemaObject } from 'openapi3-ts'
import type { ReferenceObject, SchemaObject } from 'openapi3-ts'

import { getMetadataSchema } from './decorators'
import { defaultConverters } from './defaultConverters'
Expand Down Expand Up @@ -65,7 +65,7 @@ export function validationMetadataArrayToSchemas(
)
)

const properties: { [name: string]: SchemaObject } = {}
const properties: { [name: string]: ReferenceObject | SchemaObject } = {}

Object.entries(_groupBy(metas, 'propertyName')).forEach(
([propName, propMetas]) => {
Expand Down Expand Up @@ -94,7 +94,7 @@ export function validationMetadataArrayToSchemas(
target,
options,
target.name
)
) as SchemaObject
})

return schemas
Expand Down Expand Up @@ -241,7 +241,7 @@ function applyDecorators(
target: Function,
options: IOptions,
propertyName: string
): SchemaObject {
): ReferenceObject | SchemaObject {
const additionalSchema = getMetadataSchema(target.prototype, propertyName)
return typeof additionalSchema === 'function'
? additionalSchema(schema, options)
Expand Down

0 comments on commit be8d2f0

Please sign in to comment.