diff --git a/.changeset/ninety-jeans-prove.md b/.changeset/ninety-jeans-prove.md new file mode 100644 index 0000000..f588046 --- /dev/null +++ b/.changeset/ninety-jeans-prove.md @@ -0,0 +1,5 @@ +--- +"@openapi-generator-plus/typescript-express-passport-server-generator": minor +--- + +Add options to control whether to error on unexpected properties diff --git a/templates/index.hbs b/templates/index.hbs index b4826fe..bed6e1d 100644 --- a/templates/index.hbs +++ b/templates/index.hbs @@ -5,8 +5,11 @@ import { Express } from 'express' import {{identifier name}} from './api/{{identifier name}}' {{/each}} import * as t from './types' +import { setValidationOptions } from './validation' -export default function(app: Express, impl: t.ApiImplementation) { +export default function(app: Express, impl: t.ApiImplementation, options?: t.ApiOptions) { + setValidationOptions(options) + {{#each groups}} {{identifier name}}(app, impl.{{identifier name}}) {{/each}} diff --git a/templates/indexTypes.hbs b/templates/indexTypes.hbs index ed41471..f7f9a88 100644 --- a/templates/indexTypes.hbs +++ b/templates/indexTypes.hbs @@ -9,3 +9,7 @@ export interface ApiImplementation { {{identifier name}}: {{identifier name}}.{{className name}}Api {{/each}} } + +export interface ApiOptions { + failOnUnknownProperties?: boolean +} diff --git a/templates/validation.hbs b/templates/validation.hbs index 748821a..e266bc0 100644 --- a/templates/validation.hbs +++ b/templates/validation.hbs @@ -2,10 +2,17 @@ import { Express } from 'express' import { Api } from './models' +import { ApiOptions } from './types' type FromJsonFunction<T> = (name: string, value: any) => T type MapOf<T> = { [name: string]: T } +let __options: ApiOptions | undefined + +export function setValidationOptions(options: ApiOptions | undefined) { + __options = options +} + /** * A conditional type to convert an interface model to the equivalent JSON model. * We may represent dates as Date objects in our object model, but we must translate diff --git a/templates/validationGeneric.hbs b/templates/validationGeneric.hbs index 9af9f6e..ef75be1 100644 --- a/templates/validationGeneric.hbs +++ b/templates/validationGeneric.hbs @@ -88,11 +88,11 @@ export function model{{className nativeType.parentType}}FromJson(name: string, v const result: {{nativeType}} = model{{className nativeType.parentType}}FromJsonContent(name, value, knownKeys) /* Known keys */ - // TODO if we don't ignore unknown properties - for (const key of Object.keys(value)) { - if (!knownKeys[key]) { - // throw `Unexpected key: ${key}` - console.warn(`Unexpected key in {{nativeType}}: ${key}`) + if (__options?.failOnUnknownProperties) { + for (const key of Object.keys(value)) { + if (!knownKeys[key]) { + throw `Unexpected key in {{nativeType}}: ${key}` + } } } @@ -116,11 +116,11 @@ export function model{{className nativeType.parentType}}ToJson(name: string, val const result: ToJson<{{nativeType}}> = model{{className nativeType.parentType}}ToJsonContent(name, value, knownKeys) /* Known keys */ - // TODO if we don't ignore unknown properties - for (const key of Object.keys(value)) { - if (!knownKeys[key]) { - // throw `Unexpected key: ${key}` - console.warn(`Unexpected key in {{nativeType}}: ${key}`) + if (__options?.failOnUnknownProperties) { + for (const key of Object.keys(value)) { + if (!knownKeys[key]) { + throw `Unexpected key in {{nativeType}}: ${key}` + } } }