From 56911494dae29d89d4dda3518f733f9904191c49 Mon Sep 17 00:00:00 2001 From: Karl von Randow Date: Tue, 2 Jul 2024 15:05:46 +1200 Subject: [PATCH] refactor: change validation function names away from json to request/response --- templates/frag/fromRequest.hbs | 32 +++++------ templates/frag/toResponse.hbs | 30 +++++------ templates/validation.hbs | 96 ++++++++++++++++----------------- templates/validationEnum.hbs | 4 +- templates/validationGeneric.hbs | 14 ++--- templates/validationOneOf.hbs | 4 +- 6 files changed, 90 insertions(+), 90 deletions(-) diff --git a/templates/frag/fromRequest.hbs b/templates/frag/fromRequest.hbs index 89616e1..17b054e 100644 --- a/templates/frag/fromRequest.hbs +++ b/templates/frag/fromRequest.hbs @@ -4,35 +4,35 @@ Output a reference to a function to transform the serialized request value to a @param prefix the import prefix for the functions --}} {{#if (isBoolean)}} -{{prefix}}booleanFromJson +{{prefix}}booleanFromRequest {{~else if (isInteger)~}} -{{prefix}}integerFromJson +{{prefix}}integerFromRequest {{~else if (isNumber)~}} -{{prefix}}numberFromJson +{{prefix}}numberFromRequest {{~else if (isString)~}} -{{prefix}}stringFromJson +{{prefix}}stringFromRequest {{~else if (isBinary)~}} -{{prefix}}binaryFromJson +{{prefix}}binaryFromRequest {{~else if (isFile)~}} -{{prefix}}fileFromValue +{{prefix}}fileFromRequest {{~else if (isObject)~}} -{{prefix}}model{{className nativeType.parentType}}FromJson +{{prefix}}model{{className nativeType.parentType}}FromRequest {{~else if (isArray)~}} -{{prefix}}arrayFromJson{{#if component.nullable}}WithNullable{{/if}}({{>frag/fromRequest component.schema prefix=prefix}}) +{{prefix}}arrayFromRequest{{#if component.nullable}}WithNullable{{/if}}({{>frag/fromRequest component.schema prefix=prefix}}) {{~else if (isEnum)~}} -{{prefix}}enum{{className nativeType.parentType}}FromJson +{{prefix}}enum{{className nativeType.parentType}}FromRequest {{~else if (isDate)~}} -{{prefix}}dateFromJson +{{prefix}}dateFromRequest {{~else if (isDateTime)~}} -{{prefix}}dateTimeFromJson +{{prefix}}dateTimeFromRequest {{~else if (isTime)~}} -{{prefix}}timeFromJson +{{prefix}}timeFromRequest {{~else if (isMap)~}} -{{prefix}}mapFromJson({{>frag/fromRequest component.schema prefix=prefix}}) +{{prefix}}mapFromRequest({{>frag/fromRequest component.schema prefix=prefix}}) {{~else if (isInterface)~}} -{{prefix}}model{{className nativeType.parentType}}FromJson +{{prefix}}model{{className nativeType.parentType}}FromRequest {{~else if (isOneOf)~}} -{{prefix}}oneOf{{className nativeType.parentType}}FromJson +{{prefix}}oneOf{{className nativeType.parentType}}FromRequest {{~else~}} -{{prefix}}unsupportedFromJson +{{prefix}}unsupportedFromRequest {{~/if}} diff --git a/templates/frag/toResponse.hbs b/templates/frag/toResponse.hbs index 706e9b6..ac11558 100644 --- a/templates/frag/toResponse.hbs +++ b/templates/frag/toResponse.hbs @@ -4,33 +4,33 @@ Output a reference to a function to serialize the value to the form suitable for @param prefix the import prefix for the functions --}} {{#if (isBoolean)}} -{{prefix}}booleanToJson +{{prefix}}booleanToResponse {{~else if (isInteger)~}} -{{prefix}}integerToJson +{{prefix}}integerToResponse {{~else if (isNumber)~}} -{{prefix}}numberToJson +{{prefix}}numberToResponse {{~else if (isString)~}} -{{prefix}}stringToJson +{{prefix}}stringToResponse {{~else if (isBinary)~}} -{{prefix}}binaryToJson +{{prefix}}binaryToResponse {{~else if (isObject)~}} -{{prefix}}model{{className nativeType.parentType}}ToJson +{{prefix}}model{{className nativeType.parentType}}ToResponse {{~else if (isArray)~}} -{{prefix}}arrayToJson({{>frag/toResponse component.schema prefix=prefix}}) +{{prefix}}arrayToResponse({{>frag/toResponse component.schema prefix=prefix}}) {{~else if (isEnum)~}} -{{prefix}}enum{{className nativeType.parentType}}ToJson +{{prefix}}enum{{className nativeType.parentType}}ToResponse {{~else if (isDate)~}} -{{prefix}}dateToJson +{{prefix}}dateToResponse {{~else if (isDateTime)~}} -{{prefix}}dateTimeToJson +{{prefix}}dateTimeToResponse {{~else if (isTime)~}} -{{prefix}}timeToJson +{{prefix}}timeToResponse {{~else if (isMap)~}} -{{prefix}}mapToJson({{>frag/toResponse component.schema prefix=prefix}}) +{{prefix}}mapToResponse({{>frag/toResponse component.schema prefix=prefix}}) {{~else if (isInterface)~}} -{{prefix}}model{{className nativeType.parentType}}ToJson +{{prefix}}model{{className nativeType.parentType}}ToResponse {{~else if (isOneOf)~}} -{{prefix}}oneOf{{className nativeType.parentType}}ToJson +{{prefix}}oneOf{{className nativeType.parentType}}ToResponse {{~else~}} -{{prefix}}unsupportedToJson +{{prefix}}unsupportedToResponse {{~/if}} \ No newline at end of file diff --git a/templates/validation.hbs b/templates/validation.hbs index c0c79ec..360bf04 100644 --- a/templates/validation.hbs +++ b/templates/validation.hbs @@ -4,7 +4,7 @@ import { Express, Request, Response, NextFunction } from 'express' import { Api } from './models' import { ApiOptions } from './types' -type FromJsonFunction = (name: string, value: any) => T +type ToFromRequestFunction = (name: string, value: any) => T type MapOf = { [name: string]: T } let __options: ApiOptions | undefined @@ -14,19 +14,19 @@ export function setValidationOptions(options: ApiOptions | undefined) { } /** - * A conditional type to convert an interface model to the equivalent JSON model. + * A conditional type to convert an interface model to the equivalent serialized model. * We may represent dates as Date objects in our object model, but we must translate - * them to strings for the JSON model. + * them to strings for the serialized model. */ -type ToJson = +type ToResponse = T extends Date ? string : T extends object ? { - [P in keyof T]: ToJson + [P in keyof T]: ToResponse } - : T extends (infer R)[] ? ToJson[] + : T extends (infer R)[] ? ToResponse[] : T -export function arrayFromJson(next: FromJsonFunction): FromJsonFunction { +export function arrayFromRequest(next: ToFromRequestFunction): ToFromRequestFunction { return function(name: string, value: any) { if (typeof value !== 'object' || typeof value.length !== 'number') { throw `Invalid type for ${name}: expected array got ${typeof value}` @@ -40,7 +40,7 @@ export function arrayFromJson(next: FromJsonFunction): FromJsonFunction(next: FromJsonFunction): FromJsonFunction<(T | null)[]> { +export function arrayFromRequestWithNullable(next: ToFromRequestFunction): ToFromRequestFunction<(T | null)[]> { return function(name: string, value: any) { if (typeof value !== 'object' || typeof value.length !== 'number') { throw `Invalid type for ${name}: expected array got ${typeof value}` @@ -58,15 +58,15 @@ export function arrayFromJsonWithNullable(next: FromJsonFunction): } } -export function arrayToJson(next: FromJsonFunction): FromJsonFunction { - return arrayFromJson(next) +export function arrayToResponse(next: ToFromRequestFunction): ToFromRequestFunction { + return arrayFromRequest(next) } -export function arrayToJsonWithNullable(next: FromJsonFunction): FromJsonFunction<(T | null)[]> { - return arrayFromJsonWithNullable(next) +export function arrayToResponseWithNullable(next: ToFromRequestFunction): ToFromRequestFunction<(T | null)[]> { + return arrayFromRequestWithNullable(next) } -export function mapFromJson(next: FromJsonFunction): FromJsonFunction> { +export function mapFromRequest(next: ToFromRequestFunction): ToFromRequestFunction> { return function(name: string, value: any) { if (typeof value !== 'object') { throw `Invalid type for ${name}: expected object got ${typeof value}` @@ -82,11 +82,11 @@ export function mapFromJson(next: FromJsonFunction): FromJsonFunction(next: FromJsonFunction): FromJsonFunction> { - return mapFromJson(next) +export function mapToResponse(next: ToFromRequestFunction): ToFromRequestFunction> { + return mapFromRequest(next) } -export function allowNull(next: FromJsonFunction): FromJsonFunction { +export function allowNull(next: ToFromRequestFunction): ToFromRequestFunction { return function(name: string, value: any) { if (value === null) { return null @@ -95,7 +95,7 @@ export function allowNull(next: FromJsonFunction): FromJsonFunction(next: FromJsonFunction): FromJsonFunction { +export function allowUndefined(next: ToFromRequestFunction): ToFromRequestFunction { return function(name: string, value: any) { if (value === undefined) { return undefined @@ -104,7 +104,7 @@ export function allowUndefined(next: FromJsonFunction): FromJsonFunction(next: FromJsonFunction): FromJsonFunction { +export function allowNullOrUndefined(next: ToFromRequestFunction): ToFromRequestFunction { return function(name: string, value: any) { if (value === null) { return null @@ -116,15 +116,15 @@ export function allowNullOrUndefined(next: FromJsonFunction): FromJsonFunc } } -export function unsupportedFromJson(name: string, value: any): unknown { +export function unsupportedFromRequest(name: string, value: any): unknown { if (value === undefined) { throw `Invalid type for ${name}: expected unknown got undefined` } return value } -export function unsupportedToJson(name: string, value: unknown): any { - return unsupportedFromJson(name, value) +export function unsupportedToResponse(name: string, value: unknown): any { + return unsupportedFromRequest(name, value) } export function parseUnsupported(name: string, value: any): unknown { @@ -134,15 +134,15 @@ export function parseUnsupported(name: string, value: any): unknown { return value } -export function booleanFromJson(name: string, value: any): boolean { +export function booleanFromRequest(name: string, value: any): boolean { if (typeof value !== 'boolean') { throw `Invalid type for ${name}: expected boolean got ${typeof value}` } return value } -export function booleanToJson(name: string, value: boolean): any { - return booleanFromJson(name, value) +export function booleanToResponse(name: string, value: boolean): any { + return booleanFromRequest(name, value) } export function parseBoolean(name: string, value: any): boolean { @@ -155,25 +155,25 @@ export function parseBoolean(name: string, value: any): boolean { } } -export function stringFromJson(name: string, value: any): string { +export function stringFromRequest(name: string, value: any): string { if (typeof value !== 'string') { throw `Invalid type for ${name}: expected string got ${typeof value}` } return value } -export function stringToJson(name: string, value: string): any { - return stringFromJson(name, value) +export function stringToResponse(name: string, value: string): any { + return stringFromRequest(name, value) } -export function binaryFromJson(name: string, value: any): Buffer { +export function binaryFromRequest(name: string, value: any): Buffer { if (typeof value !== 'string') { throw `Invalid type for ${name}: expected string got ${typeof value}` } return Buffer.from(value, 'base64') } -export function binaryToJson(name: string, value: string | Buffer): string { +export function binaryToResponse(name: string, value: string | Buffer): string { if (typeof value === 'string') { return value } else { @@ -197,7 +197,7 @@ export function parseString(name: string, value: any): string { throw `Invalid value for ${name}: expected string got ${typeof value}` } -export function integerFromJson(name: string, value: any): number { +export function integerFromRequest(name: string, value: any): number { if (typeof value !== 'number') { throw `Invalid type for ${name}: expected number got ${typeof value}` } @@ -210,8 +210,8 @@ export function integerFromJson(name: string, value: any): number { return value } -export function integerToJson(name: string, value: number): any { - return integerFromJson(name, value) +export function integerToResponse(name: string, value: number): any { + return integerFromRequest(name, value) } export function parseInteger(name: string, value: any): number { @@ -232,7 +232,7 @@ export function parseInteger(name: string, value: any): number { throw `Invalid value for ${name}: expected integer got ${typeof value}` } -export function numberFromJson(name: string, value: any): number { +export function numberFromRequest(name: string, value: any): number { if (typeof value !== 'number') { throw `Invalid type for ${name}: expected number got ${typeof value}` } @@ -242,8 +242,8 @@ export function numberFromJson(name: string, value: any): number { return value } -export function numberToJson(name: string, value: number): any { - return numberFromJson(name, value) +export function numberToResponse(name: string, value: number): any { + return numberFromRequest(name, value) } export function parseNumber(name: string, value: any): number { @@ -260,7 +260,7 @@ export function parseNumber(name: string, value: any): number { throw `Invalid value for ${name}: expected number got ${typeof value}` } -export function dateFromJson(name: string, value: any): string { +export function dateFromRequest(name: string, value: any): string { if (typeof value !== 'string') { throw `Invalid type for ${name}: expected string got ${typeof value}` } @@ -271,14 +271,14 @@ export function dateFromJson(name: string, value: any): string { } export function parseDate(name: string, value: any): string { - return dateFromJson(name, value) + return dateFromRequest(name, value) } -export function dateToJson(name: string, value: string): string { - return dateFromJson(name, value) +export function dateToResponse(name: string, value: string): string { + return dateFromRequest(name, value) } -export function dateTimeFromJson(name: string, value: any): {{#ifeq dateApproach 'native'}}Date{{else}}string{{/ifeq}} { +export function dateTimeFromRequest(name: string, value: any): {{#ifeq dateApproach 'native'}}Date{{else}}string{{/ifeq}} { if (typeof value !== 'string') { throw `Invalid type for ${name}: expected string got ${typeof value}` } @@ -293,21 +293,21 @@ export function dateTimeFromJson(name: string, value: any): {{#ifeq dateApproach } export function parseDateTime(name: string, value: any): {{#ifeq dateApproach 'native'}}Date{{else}}string{{/ifeq}} { - return dateTimeFromJson(name, value) + return dateTimeFromRequest(name, value) } -export function dateTimeToJson(name: string, value: {{#ifeq dateApproach 'native'}}Date{{else}}string{{/ifeq}}): string { +export function dateTimeToResponse(name: string, value: {{#ifeq dateApproach 'native'}}Date{{else}}string{{/ifeq}}): string { {{#ifeq dateApproach 'native'}} if (typeof value !== 'object' || typeof value.toISOString !== 'function') { throw `Invalid type for ${name}: expected Date got ${typeof value}` } return value.toISOString() {{else}} - return dateTimeFromJson(name, value) + return dateTimeFromRequest(name, value) {{/ifeq}} } -export function timeFromJson(name: string, value: any): string { +export function timeFromRequest(name: string, value: any): string { if (typeof value !== 'string') { throw `Invalid type for ${name}: expected string got ${typeof value}` } @@ -318,11 +318,11 @@ export function timeFromJson(name: string, value: any): string { } export function parseTime(name: string, value: any): string { - return timeFromJson(name, value) + return timeFromRequest(name, value) } -export function timeToJson(name: string, value: string): string { - return timeFromJson(name, value) +export function timeToResponse(name: string, value: string): string { + return timeFromRequest(name, value) } export function verifyMinimumFiles(fieldName: string, minFileCount: number): (req: Request, res: Response, next: NextFunction) => void { @@ -335,7 +335,7 @@ export function verifyMinimumFiles(fieldName: string, minFileCount: number): (re } } -export function fileFromValue(name: string, value: Express.Multer.File): any { +export function fileFromRequest(name: string, value: Express.Multer.File): any { if (typeof value !== 'object') { throw `Invalid type for ${name}: expected object got ${typeof value}` } diff --git a/templates/validationEnum.hbs b/templates/validationEnum.hbs index 9db7a91..819f4b2 100644 --- a/templates/validationEnum.hbs +++ b/templates/validationEnum.hbs @@ -1,4 +1,4 @@ -export function enum{{className nativeType}}FromJson(name: string, value: any): {{nativeType}} { +export function enum{{className nativeType}}FromRequest(name: string, value: any): {{nativeType}} { if (typeof value !== 'string' || value === undefined || value === null) { throw `Invalid type for ${name}: expected string got ${typeof value}` } @@ -12,7 +12,7 @@ export function enum{{className nativeType}}FromJson(name: string, value: any): throw `Unexpected enum value for {{nativeType}}: ${value}` } -export function enum{{className nativeType}}ToJson(name: string, value: {{nativeType}}): {{nativeType}} { +export function enum{{className nativeType}}ToResponse(name: string, value: {{nativeType}}): {{nativeType}} { if (typeof value !== 'string' || value === undefined || value === null) { throw `Invalid type for ${name}: expected string got ${typeof value}` } diff --git a/templates/validationGeneric.hbs b/templates/validationGeneric.hbs index d094cd9..cc3a4cd 100644 --- a/templates/validationGeneric.hbs +++ b/templates/validationGeneric.hbs @@ -2,7 +2,7 @@ const {{constantName (concat nativeType 'Keys')}}: string[] = [{{#each properties}}{{{stringLiteral serializedName}}}{{#hasMore}}, {{/hasMore}}{{/each}}] {{/ifneq}} -function model{{className nativeType.parentType}}FromJsonContent(name: string, value: any, knownKeys: Record = {}): {{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}} { +function model{{className nativeType.parentType}}FromRequestContent(name: string, value: any, knownKeys: Record = {}): {{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}} { {{#ifeq purpose 'METADATA'}} const result: {{{nativeType}}} = { {{#with properties.value}} @@ -60,7 +60,7 @@ function model{{className nativeType.parentType}}FromJsonContent(name: string, v return result } -function model{{className nativeType.parentType}}ToJsonContent(name: string, value: {{nativeType}}, knownKeys: Record = {}): ToJson<{{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}}> { +function model{{className nativeType.parentType}}ToResponseContent(name: string, value: {{nativeType}}, knownKeys: Record = {}): ToResponse<{{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}}> { {{#ifeq purpose 'METADATA'}} throw 'toJson not supported for metadata schemas' {{else}} @@ -70,7 +70,7 @@ function model{{className nativeType.parentType}}ToJsonContent(name: string, val {{constantName (concat nativeType 'Keys')}}.forEach(k => knownKeys[k] = true) - const result: ToJson<{{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}}> = { + const result: ToResponse<{{#if discriminator}}Omit<{{nativeType.parentType}}, {{{stringLiteral discriminator.serializedName}}}>{{else}}{{nativeType}}{{/if}}> = { {{#each parents}} ...{{>frag/toResponse . prefix=''}}Content(name, value as unknown as {{{nativeType}}}, knownKeys), {{/each}} @@ -100,7 +100,7 @@ function model{{className nativeType.parentType}}ToJsonContent(name: string, val {{/ifeq}} } -export function model{{className nativeType.parentType}}FromJson(name: string, value: any): {{nativeType}} { +export function model{{className nativeType.parentType}}FromRequest(name: string, value: any): {{nativeType}} { {{#if discriminator}} const discriminatorValue = value.{{discriminator.serializedName}} switch (discriminatorValue) { @@ -113,7 +113,7 @@ export function model{{className nativeType.parentType}}FromJson(name: string, v throw `Invalid value for ${name}: didn't contain a known discriminator value: ${discriminatorValue}` {{else}} const knownKeys: Record = {} - const result: {{nativeType}} = model{{className nativeType.parentType}}FromJsonContent(name, value, knownKeys) + const result: {{nativeType}} = model{{className nativeType.parentType}}FromRequestContent(name, value, knownKeys) /* Known keys */ if (__options?.failOnUnknownProperties) { @@ -128,7 +128,7 @@ export function model{{className nativeType.parentType}}FromJson(name: string, v {{/if}} } -export function model{{className nativeType.parentType}}ToJson(name: string, value: {{nativeType}}): ToJson<{{nativeType}}> { +export function model{{className nativeType.parentType}}ToResponse(name: string, value: {{nativeType}}): ToResponse<{{nativeType}}> { {{#if discriminator}} const discriminatorValue = value.{{discriminator.serializedName}} switch (discriminatorValue) { @@ -141,7 +141,7 @@ export function model{{className nativeType.parentType}}ToJson(name: string, val throw `Invalid value for ${name}: didn't contain a known discriminator value: ${discriminatorValue}` {{else}} const knownKeys: Record = {} - const result: ToJson<{{nativeType}}> = model{{className nativeType.parentType}}ToJsonContent(name, value, knownKeys) + const result: ToResponse<{{nativeType}}> = model{{className nativeType.parentType}}ToResponseContent(name, value, knownKeys) /* Known keys */ if (__options?.failOnUnknownProperties) { diff --git a/templates/validationOneOf.hbs b/templates/validationOneOf.hbs index 1d4e233..e7e1017 100644 --- a/templates/validationOneOf.hbs +++ b/templates/validationOneOf.hbs @@ -1,4 +1,4 @@ -export function oneOf{{className nativeType.parentType}}FromJson(name: string, value: any): {{nativeType}} { +export function oneOf{{className nativeType.parentType}}FromRequest(name: string, value: any): {{nativeType}} { {{#if discriminator}} const discriminatorValue = value.{{discriminator.name}} switch (value.{{discriminator.name}}) { @@ -23,7 +23,7 @@ export function oneOf{{className nativeType.parentType}}FromJson(name: string, v {{/if}} } -export function oneOf{{className nativeType.parentType}}ToJson(name: string, value: {{nativeType}}): ToJson<{{nativeType}}> { +export function oneOf{{className nativeType.parentType}}ToResponse(name: string, value: {{nativeType}}): ToResponse<{{nativeType}}> { {{#if discriminator}} const discriminatorValue = value.{{discriminator.serializedName}} switch (discriminatorValue) {