Skip to content

Commit

Permalink
Add options to control whether to error on unexpected properties
Browse files Browse the repository at this point in the history
  • Loading branch information
karlvr committed Mar 21, 2024
1 parent b32ffcc commit e899a8a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-jeans-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openapi-generator-plus/typescript-express-passport-server-generator": minor
---

Add options to control whether to error on unexpected properties
5 changes: 4 additions & 1 deletion templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
4 changes: 4 additions & 0 deletions templates/indexTypes.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export interface ApiImplementation {
{{identifier name}}: {{identifier name}}.{{className name}}Api
{{/each}}
}

export interface ApiOptions {
failOnUnknownProperties?: boolean
}
7 changes: 7 additions & 0 deletions templates/validation.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions templates/validationGeneric.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
}
}
}

Expand All @@ -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}`
}
}
}

Expand Down

0 comments on commit e899a8a

Please sign in to comment.