- Lock typescript version to
~4.7.4as~4.9.4seems to have breaking changes.
- Fix bug introduced in previous version
- Bump Typescript version
$refis not validated correctly when it hasnullable: true
- Add generation of openAPI enums as yaml file from all object literals annotated by
constkeyword in a given directory.
const ObjectEnumStr = {
Foo: "FOO",
Bar: "BAR"
} as const;
const ObjectEnumNum = {
One: 1,
Bar: 2
} as const;
const ObjectEnumWithFunc = {
func: () => 1
} as const;
const ObjectEnumWithBool = {
bool: true
} as const;
const MixedObjectEnum = {
Foo: "FOO",
One: 1
} as const;
const ObjectEnumEmpty = {} as const;
/* open-ts: ignore-convert-enums */
const ObjectEnumIgnored = {
Baz: "BAZ",
Qux: "QUX"
} as const;
const ObjectEnumMutable = {
Quux: "QUUX",
quuz: "QUUZ"
};ObjectEnumNum:
type: number
enum:
- 1
- 2
ObjectEnumStr:
type: string
enum:
- FOO
- BARImplemented OpenAPI enum definitions to be generated as TypeScript UnionTypes instead of TypeScript Enums.
NumberEnum:
type: number
enum:
- 1
- 2
StringEnum:
type: string
enum:
- a
- b
PetExternalEnum:
$ref: './schemas/fileJoin.yml#/ExternalEnum'
(-- Abbreviation --)
petNumberType:
$ref: '#/components/schemas/NumberEnum'
petStringType:
$ref: '#/components/schemas/StringEnum'
petExternalEnum:
$ref: '#/components/schemas/PetExternalEnum'
petListEnum:
type: array
items:
$ref: '#/components/schemas/StringEnum'
petDirectEnum:
type: string
enum:
- x
- y
- zexport type NumberEnum = typeof NumberEnum[keyof typeof NumberEnum];
export type StringEnum = typeof StringEnum[keyof typeof StringEnum];
export type PetExternalEnum = typeof PetExternalEnum[keyof typeof PetExternalEnum];
export const NumberEnum = {
1: 1,
2: 2
} as const;
export const StringEnum = {
a: "a",
b: "b"
} as const;
export const PetExternalEnum = {
1: 1,
2: 2
} as const;
export class AddPetRequestBodyValidator {
/**
* petNumberType
*/
@IsOptional()
@IsIn(Object.values(NumberEnum))
petNumberType: NumberEnum;
/**
* petStringType
*/
@IsOptional()
@IsIn(Object.values(StringEnum))
petStringType: StringEnum;
/**
* petExternalEnum
*/
@IsOptional()
@IsIn(Object.values(PetExternalEnum))
petExternalEnum: PetExternalEnum;
/**
* petListEnum
*/
@IsOptional()
@IsArray()
@IsIn(Object.values(StringEnum), { each: true })
petListEnum: StringEnum[];
/**
* petDirectEnum
*/
@IsOptional()
@IsIn(["x","y","z"])
petDirectEnum: "x" | "y" | "z";
}- Fix nested array check
petDataList:
type: array
items:
$ref: '#/components/schemas/NewPet' /**
* petDataList
*/
@IsOptional()
@IsArray()
@IsEnum(undefined, { each: true })
petDataList: NewPet[]; /**
* petDataList
*/
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => NewPetValidator)
petDataList: NewPet[];- Allow nested object validation.
petData:
$ref: '#/components/schemas/NewPet'
inlineObject:
type: object
properties:
food:
type: string /**
* petData
*/
@IsOptional()
@ValidateNested()
@Type(() => NewPetValidator)
petData: NewPet;
/**
* inlineObject
*/
@IsOptional()
@ValidateNested()
@Type(() => InlineObject2Validator)
inlineObject: {
food?: string;
};
export class NewPetValidator {
...
}
export class InlineObject2Validator {
...
}- Throw and error and exit process with schema validation error is encountered.
- Show warning when missing required properties are found.
Validation Error
Error parsing ~/open-ts/test-files/pet.yaml
duplicated mapping key at line 248, column -764:
properties:
^
- Allow validating required nullable parameters by adding conditional validation using
ValidateIf.- Target Command:
gen-agent
- Target Command:
Customer:
type: object
required:
- name
properties:
name:
type: string
nullable: true
birthday:
type: string
format: date
nullable: true /**
* name
*/
@IsNotEmpty()
@ValidateIf(o => o.name !== null)
name: string;
/**
* birthday
*/
@IsOptional()
@Type(() => Date)
birthday: Date;- Throw and error and exit process with schema validation error is encountered.
- Show warning when missing required properties are found.
Validation Error
Error parsing ~/open-ts/test-files/pet.yaml
duplicated mapping key at line 248, column -764:
properties:
^
- Allow class validator to validate array of enums.
- Target Command:
gen-agent
- Target Command:
petListEnum:
type: array
items:
$ref: '#/components/schemas/StringEnum' /**
* petListEnum
*/
@IsOptional()
@IsArray()
@IsEnum(StringEnumEnum, { each: true })
petListEnum: StringEnum[];- Allow generating Typescript enums for validation.
- Target Command:
gen-agent
- Target Command:
NumberEnum:
type: number
enum:
- 1
- 2
StringEnum:
type: string
enum:
- a
- benum NumberEnumEnum {
_1 = 1,
_2 = 2
}
enum StringEnumEnum {
_a = "a",
_b = "b"
} /**
* petNumberType
*/
@IsOptional()
@IsEnum(NumberEnumEnum)
petNumberType: NumberEnum;- Fix bug when number enums are found in the specs file.
- Target Command:
gen-agent
- Target Command:
NumberEnum:
type: number
enum:
- 1
- 2
StringEnum:
type: string
enum:
- a
- bexport type NumberEnum = 1 | 2;
export type StringEnum = "a" | "b"; gen-agent <sourceFile> <destinationFile> Generate API Agent
convert-enums <sourceDir> <destinationDir> Generate OpenAPI enums from Typescript enums
- Generate openAPI enums as yaml file from all enums found in TS files within a specified directory.
- Allow using refs to external files.
- Fix bug caused by wrong null check.
When
minimumormaximumis set to 0,MinandMaxvalidators are not created.
pickupHour:
type: integer
minimum: 0
maximum: 0before:
@IsOptional()
@IsInt()
pickupHour: number;After:
@IsOptional()
@IsInt()
@Min(0)
@Max(0)
pickupHour: number;- Fix bug in caused by wrong import from
class-validator.- IsMin => Min
- IsMax => Max