mirror of
https://github.com/ferdikoomen/openapi-typescript-codegen.git
synced 2025-12-08 20:16:21 +00:00
- Aded validation
This commit is contained in:
parent
ea0bfd94b3
commit
35d7ac10c8
@ -8,15 +8,16 @@ export function getEnumSymbols(values?: (string | number)[]): Shape[] {
|
||||
})
|
||||
.map(
|
||||
(value: string | number): Shape => {
|
||||
return typeof value === 'number'
|
||||
? {
|
||||
name: `NUM_${value}`,
|
||||
value: String(value),
|
||||
}
|
||||
: {
|
||||
name: value.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(),
|
||||
value: `'${value}'`,
|
||||
};
|
||||
if (typeof value === 'number') {
|
||||
return {
|
||||
name: `NUM_${value}`,
|
||||
value: String(value),
|
||||
};
|
||||
}
|
||||
return {
|
||||
name: value.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(),
|
||||
value: `'${value}'`,
|
||||
};
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@ -12,7 +12,10 @@ export function getEnumSymbolsFromDescription(description: string): Shape[] {
|
||||
const name: string = match.split('=')[0];
|
||||
const value: number = parseInt(match.split('=')[1].replace(/[^0-9]/g, ''));
|
||||
if (name && Number.isInteger(value)) {
|
||||
symbols.push({ name, value: String(value) });
|
||||
symbols.push({
|
||||
name: name.replace(/([a-z])([A-Z]+)/g, '$1_$2').toUpperCase(),
|
||||
value: String(value),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -103,8 +103,6 @@ export function getSchema(openApi: OpenApi, schema: OpenApiSchema, name: string)
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO: Properties kunnen weer simple REFS zijn, of zelfs geneste properties, testcase nodig!
|
||||
|
||||
if (schema.type === 'object' && schema.properties) {
|
||||
result.isInterface = true;
|
||||
result.type = 'interface';
|
||||
@ -139,6 +137,9 @@ export function getSchema(openApi: OpenApi, schema: OpenApiSchema, name: string)
|
||||
readOnly: property.readOnly || false,
|
||||
description: property.description,
|
||||
});
|
||||
|
||||
// TODO: This also needs a validation logic, maybe we can store that
|
||||
// per schema and have them 'concatenate' on demand??
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
import { {{{this}}} } from '../models/{{{this}}}';
|
||||
{{/each}}
|
||||
{{/if}}
|
||||
import * as yup from 'yup';
|
||||
{{#if description}}
|
||||
|
||||
/**
|
||||
@ -25,9 +26,49 @@ export interface {{{name}}}{{{template}}}{{#if extend}} extends {{{extend}}}{{/i
|
||||
{{#if readOnly}}readonly {{/if}}{{{name}}}{{#unless required}}?{{/unless}}: {{{type}}}{{#if nullable}} | null{{/if}};
|
||||
{{/each}}
|
||||
}
|
||||
|
||||
export namespace {{{name}}} {
|
||||
|
||||
{{#each enums}}
|
||||
{{#if description}}
|
||||
/**
|
||||
* {{{description}}}
|
||||
*/
|
||||
{{/if}}
|
||||
export enum {{{name}}} {
|
||||
{{#each values}}
|
||||
{{{name}}} = {{{value}}},
|
||||
{{/each}}
|
||||
}
|
||||
|
||||
{{/each}}
|
||||
export const schema = yup.object<{{{name}}}{{{template}}}>().shape({
|
||||
// Add properties
|
||||
});
|
||||
|
||||
export function validate(value: {{{name}}}{{{template}}}): Promise<{{{name}}}{{{template}}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: {{{name}}}{{{template}}}): {{{name}}}{{{template}}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
{{/if}}
|
||||
{{#if isType}}
|
||||
export type {{{name}}} = {{{type}}}{{#if nullable}} | null{{/if}};
|
||||
|
||||
export namespace {{{name}}} {
|
||||
export const schema = yup.string();
|
||||
|
||||
export function validate(value: any): Promise<{{{name}}}{{#if nullable}} | null{{/if}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: any): {{{name}}}{{#if nullable}} | null{{/if}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
{{/if}}
|
||||
{{#if isEnum}}
|
||||
export enum {{{name}}} {
|
||||
@ -35,4 +76,16 @@ export enum {{{name}}} {
|
||||
{{{name}}} = {{{value}}},
|
||||
{{/each}}
|
||||
}
|
||||
|
||||
export namespace {{{name}}} {
|
||||
export const schema = yup.string();
|
||||
|
||||
export function validate(value: any): Promise<{{{name}}}> {
|
||||
return schema.validate(value, { strict: true });
|
||||
}
|
||||
|
||||
export function validateSync(value: any): {{{name}}} {
|
||||
return schema.validateSync(value, { strict: true });
|
||||
}
|
||||
}
|
||||
{{/if}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user