Skip to content

Commit

Permalink
refactor: change validation function names away from json to request/…
Browse files Browse the repository at this point in the history
…response
  • Loading branch information
karlvr committed Jul 2, 2024
1 parent 00cec1e commit 5691149
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 90 deletions.
32 changes: 16 additions & 16 deletions templates/frag/fromRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
30 changes: 15 additions & 15 deletions templates/frag/toResponse.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
96 changes: 48 additions & 48 deletions templates/validation.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Express, Request, Response, NextFunction } from 'express'
import { Api } from './models'
import { ApiOptions } from './types'

type FromJsonFunction<T> = (name: string, value: any) => T
type ToFromRequestFunction<T> = (name: string, value: any) => T
type MapOf<T> = { [name: string]: T }

let __options: ApiOptions | undefined
Expand All @@ -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<T> =
type ToResponse<T> =
T extends Date ? string
: T extends object ? {
[P in keyof T]: ToJson<T[P]>
[P in keyof T]: ToResponse<T[P]>
}
: T extends (infer R)[] ? ToJson<R>[]
: T extends (infer R)[] ? ToResponse<R>[]
: T

export function arrayFromJson<T>(next: FromJsonFunction<T>): FromJsonFunction<T[]> {
export function arrayFromRequest<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<T[]> {
return function(name: string, value: any) {
if (typeof value !== 'object' || typeof value.length !== 'number') {
throw `Invalid type for ${name}: expected array got ${typeof value}`
Expand All @@ -40,7 +40,7 @@ export function arrayFromJson<T>(next: FromJsonFunction<T>): FromJsonFunction<T[
}
}

export function arrayFromJsonWithNullable<T>(next: FromJsonFunction<T | null>): FromJsonFunction<(T | null)[]> {
export function arrayFromRequestWithNullable<T>(next: ToFromRequestFunction<T | null>): 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}`
Expand All @@ -58,15 +58,15 @@ export function arrayFromJsonWithNullable<T>(next: FromJsonFunction<T | null>):
}
}

export function arrayToJson<T>(next: FromJsonFunction<T>): FromJsonFunction<T[]> {
return arrayFromJson(next)
export function arrayToResponse<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<T[]> {
return arrayFromRequest(next)
}

export function arrayToJsonWithNullable<T>(next: FromJsonFunction<T | null>): FromJsonFunction<(T | null)[]> {
return arrayFromJsonWithNullable(next)
export function arrayToResponseWithNullable<T>(next: ToFromRequestFunction<T | null>): ToFromRequestFunction<(T | null)[]> {
return arrayFromRequestWithNullable(next)
}

export function mapFromJson<T>(next: FromJsonFunction<T>): FromJsonFunction<MapOf<T>> {
export function mapFromRequest<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<MapOf<T>> {
return function(name: string, value: any) {
if (typeof value !== 'object') {
throw `Invalid type for ${name}: expected object got ${typeof value}`
Expand All @@ -82,11 +82,11 @@ export function mapFromJson<T>(next: FromJsonFunction<T>): FromJsonFunction<MapO
}
}

export function mapToJson<T>(next: FromJsonFunction<T>): FromJsonFunction<MapOf<T>> {
return mapFromJson(next)
export function mapToResponse<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<MapOf<T>> {
return mapFromRequest(next)
}

export function allowNull<T>(next: FromJsonFunction<T>): FromJsonFunction<T | null> {
export function allowNull<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<T | null> {
return function(name: string, value: any) {
if (value === null) {
return null
Expand All @@ -95,7 +95,7 @@ export function allowNull<T>(next: FromJsonFunction<T>): FromJsonFunction<T | nu
}
}

export function allowUndefined<T>(next: FromJsonFunction<T>): FromJsonFunction<T | undefined> {
export function allowUndefined<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<T | undefined> {
return function(name: string, value: any) {
if (value === undefined) {
return undefined
Expand All @@ -104,7 +104,7 @@ export function allowUndefined<T>(next: FromJsonFunction<T>): FromJsonFunction<T
}
}

export function allowNullOrUndefined<T>(next: FromJsonFunction<T>): FromJsonFunction<T | null | undefined> {
export function allowNullOrUndefined<T>(next: ToFromRequestFunction<T>): ToFromRequestFunction<T | null | undefined> {
return function(name: string, value: any) {
if (value === null) {
return null
Expand All @@ -116,15 +116,15 @@ export function allowNullOrUndefined<T>(next: FromJsonFunction<T>): 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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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}`
}
Expand All @@ -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 {
Expand All @@ -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}`
}
Expand All @@ -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 {
Expand All @@ -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}`
}
Expand All @@ -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}`
}
Expand All @@ -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}`
}
Expand All @@ -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 {
Expand All @@ -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}`
}
Expand Down
Loading

0 comments on commit 5691149

Please sign in to comment.