Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Dec 28, 2024
1 parent b78ce3d commit c2ac328
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
2 changes: 2 additions & 0 deletions packages/openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export * from './openapi'
export * from './openapi-content-builder'
export * from './openapi-generator'
export * from './openapi-parameters-builder'
export * from './openapi-path-parser'
export * from './schema'
export * from './schema-converter'
export * from './schema-utils'
export * from './utils'
1 change: 1 addition & 0 deletions packages/openapi/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as JSONSchema from 'json-schema-typed/draft-2020-12'

export { Format as JSONSchemaFormat } from 'json-schema-typed/draft-2020-12'
export { JSONSchema }

export type ObjectSchema = JSONSchema.JSONSchema & { type: 'object' } & object
Expand Down
62 changes: 31 additions & 31 deletions packages/zod/src/converter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Schema } from '@orpc/contract'
import type { SchemaConverter, SchemaConvertOptions } from '@orpc/openapi'
import type { JSONSchema, SchemaConverter, SchemaConvertOptions } from '@orpc/openapi'
import type { StandardSchemaV1 } from '@standard-schema/spec'
import { JSONSchemaFormat } from '@orpc/openapi'
import escapeStringRegexp from 'escape-string-regexp'
import { Format, type JSONSchema, type keywords } from 'json-schema-typed/draft-2020-12'
import {
type EnumLike,
type KeySchema,
Expand Down Expand Up @@ -70,7 +70,7 @@ export const NON_LOGIC_KEYWORDS = [
'$vocabulary',
'$dynamicAnchor',
'$dynamicRef',
] satisfies (typeof keywords)[number][]
] satisfies (typeof JSONSchema.keywords)[number][]

export const UNSUPPORTED_JSON_SCHEMA = { not: {} }
export const UNDEFINED_JSON_SCHEMA = { const: 'undefined' }
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface ZodToJsonSchemaOptions {
export function zodToJsonSchema(
schema: StandardSchemaV1,
options?: ZodToJsonSchemaOptions,
): Exclude<JSONSchema, boolean> {
): Exclude<JSONSchema.JSONSchema, boolean> {
if (schema['~standard'].vendor !== 'zod') {
console.warn(`Generate JSON schema not support ${schema['~standard'].vendor} yet`)
return {}
Expand Down Expand Up @@ -161,7 +161,7 @@ export function zodToJsonSchema(
}

case 'URL': {
return { type: 'string', format: Format.URI }
return { type: 'string', format: JSONSchemaFormat.URI }
}
}

Expand All @@ -173,7 +173,7 @@ export function zodToJsonSchema(
case ZodFirstPartyTypeKind.ZodString: {
const schema_ = schema__ as ZodString

const json: JSONSchema = { type: 'string' }
const json: JSONSchema.JSONSchema = { type: 'string' }

for (const check of schema_._def.checks) {
switch (check.kind) {
Expand All @@ -184,13 +184,13 @@ export function zodToJsonSchema(
json.pattern = '^[0-9A-HJKMNP-TV-Z]{26}$'
break
case 'email':
json.format = Format.Email
json.format = JSONSchemaFormat.Email
break
case 'url':
json.format = Format.URI
json.format = JSONSchemaFormat.URI
break
case 'uuid':
json.format = Format.UUID
json.format = JSONSchemaFormat.UUID
break
case 'regex':
json.pattern = check.regex.source
Expand Down Expand Up @@ -228,19 +228,19 @@ export function zodToJsonSchema(
json.pattern = '^[0-9A-HJKMNP-TV-Z]{26}$'
break
case 'datetime':
json.format = Format.DateTime
json.format = JSONSchemaFormat.DateTime
break
case 'date':
json.format = Format.Date
json.format = JSONSchemaFormat.Date
break
case 'time':
json.format = Format.Time
json.format = JSONSchemaFormat.Time
break
case 'duration':
json.format = Format.Duration
json.format = JSONSchemaFormat.Duration
break
case 'ip':
json.format = Format.IPv4
json.format = JSONSchemaFormat.IPv4
break
case 'jwt':
json.pattern = '^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$'
Expand All @@ -260,7 +260,7 @@ export function zodToJsonSchema(
case ZodFirstPartyTypeKind.ZodNumber: {
const schema_ = schema__ as ZodNumber

const json: JSONSchema = { type: 'number' }
const json: JSONSchema.JSONSchema = { type: 'number' }

for (const check of schema_._def.checks) {
switch (check.kind) {
Expand Down Expand Up @@ -290,7 +290,7 @@ export function zodToJsonSchema(
}

case ZodFirstPartyTypeKind.ZodBigInt: {
const json: JSONSchema = { type: 'string', pattern: '^-?[0-9]+$' }
const json: JSONSchema.JSONSchema = { type: 'string', pattern: '^-?[0-9]+$' }

// WARN: ignore checks

Expand All @@ -302,11 +302,11 @@ export function zodToJsonSchema(
}

case ZodFirstPartyTypeKind.ZodDate: {
const jsonSchema: JSONSchema = { type: 'string', format: Format.Date }
const schema: JSONSchema.JSONSchema = { type: 'string', format: JSONSchemaFormat.Date }

// WARN: ignore checks

return jsonSchema
return schema
}

case ZodFirstPartyTypeKind.ZodNull: {
Expand Down Expand Up @@ -343,7 +343,7 @@ export function zodToJsonSchema(
const schema_ = schema__ as ZodArray<ZodTypeAny>
const def = schema_._def

const json: JSONSchema = { type: 'array' }
const json: JSONSchema.JSONSchema = { type: 'array' }

if (def.exactLength) {
json.maxItems = def.exactLength.value
Expand All @@ -367,8 +367,8 @@ export function zodToJsonSchema(
ZodTypeAny | null
>

const prefixItems: JSONSchema[] = []
const json: JSONSchema = { type: 'array' }
const prefixItems: JSONSchema.JSONSchema[] = []
const json: JSONSchema.JSONSchema = { type: 'array' }

for (const item of schema_._def.items) {
prefixItems.push(zodToJsonSchema(item, childOptions))
Expand All @@ -391,8 +391,8 @@ export function zodToJsonSchema(
case ZodFirstPartyTypeKind.ZodObject: {
const schema_ = schema__ as ZodObject<ZodRawShape>

const json: JSONSchema = { type: 'object' }
const properties: Record<string, JSONSchema> = {}
const json: JSONSchema.JSONSchema = { type: 'object' }
const properties: Record<string, JSONSchema.JSONSchema> = {}
const required: string[] = []

for (const [key, value] of Object.entries(schema_.shape)) {
Expand Down Expand Up @@ -443,7 +443,7 @@ export function zodToJsonSchema(
case ZodFirstPartyTypeKind.ZodRecord: {
const schema_ = schema__ as ZodRecord<KeySchema, ZodAny>

const json: JSONSchema = { type: 'object' }
const json: JSONSchema.JSONSchema = { type: 'object' }

json.additionalProperties = zodToJsonSchema(
schema_._def.valueType,
Expand Down Expand Up @@ -485,7 +485,7 @@ export function zodToJsonSchema(
| ZodUnion<ZodUnionOptions>
| ZodDiscriminatedUnion<string, [ZodObject<any>, ...ZodObject<any>[]]>

const anyOf: JSONSchema[] = []
const anyOf: JSONSchema.JSONSchema[] = []

for (const s of schema_._def.options) {
anyOf.push(zodToJsonSchema(s, childOptions))
Expand All @@ -497,7 +497,7 @@ export function zodToJsonSchema(
case ZodFirstPartyTypeKind.ZodIntersection: {
const schema_ = schema__ as ZodIntersection<ZodTypeAny, ZodTypeAny>

const allOf: JSONSchema[] = []
const allOf: JSONSchema.JSONSchema[] = []

for (const s of [schema_._def.left, schema_._def.right]) {
allOf.push(zodToJsonSchema(s, childOptions))
Expand Down Expand Up @@ -600,10 +600,10 @@ export function zodToJsonSchema(
}

function extractJSONSchema(
schema: JSONSchema,
check: (schema: JSONSchema) => boolean,
matches: JSONSchema[] = [],
): { schema: JSONSchema | undefined, matches: JSONSchema[] } {
schema: JSONSchema.JSONSchema,
check: (schema: JSONSchema.JSONSchema) => boolean,
matches: JSONSchema.JSONSchema[] = [],
): { schema: JSONSchema.JSONSchema | undefined, matches: JSONSchema.JSONSchema[] } {
if (check(schema)) {
matches.push(schema)
return { schema: undefined, matches }
Expand Down Expand Up @@ -671,7 +671,7 @@ export class ZodToJsonSchemaConverter implements SchemaConverter {
return Boolean(schema && schema['~standard'].vendor === 'zod')
}

convert(schema: Schema, options: SchemaConvertOptions): any { // TODO
convert(schema: Schema, options: SchemaConvertOptions): JSONSchema.JSONSchema { // TODO
const jsonSchema = schema as ZodTypeAny
return zodToJsonSchema(jsonSchema, { mode: options.strategy })
}
Expand Down

0 comments on commit c2ac328

Please sign in to comment.