-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(zui): standardize the way transforms throw errors (#413)
- Loading branch information
1 parent
acb2673
commit e70a098
Showing
10 changed files
with
152 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import { jsonSchemaToZui } from './transforms/json-schema-to-zui' | ||
import { zuiToJsonSchema } from './transforms/zui-to-json-schema' | ||
import { objectToZui } from './transforms/object-to-zui' | ||
import { | ||
toTypescript, | ||
UntitledDeclarationError, | ||
TypescriptGenerationOptions, | ||
} from './transforms/zui-to-typescript-type' | ||
import { toTypescript, TypescriptGenerationOptions } from './transforms/zui-to-typescript-type' | ||
import { toTypescriptSchema } from './transforms/zui-to-typescript-schema' | ||
import * as transformErrors from './transforms/common/errors' | ||
|
||
export * from './ui' | ||
export * from './z' | ||
|
||
export const transforms = { | ||
errors: transformErrors, | ||
jsonSchemaToZui, | ||
zuiToJsonSchema, | ||
objectToZui, | ||
toTypescript, | ||
toTypescriptSchema, | ||
} | ||
|
||
export { UntitledDeclarationError, type TypescriptGenerationOptions } | ||
export { type TypescriptGenerationOptions } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ZodFirstPartyTypeKind } from '../../z' | ||
|
||
type Transform = | ||
| 'json-schema-to-zui' | ||
| 'object-to-zui' | ||
| 'zui-to-json-schema' | ||
| 'zui-to-typescript-schema' | ||
| 'zui-to-typescript-type' | ||
|
||
export abstract class ZuiTransformError extends Error { | ||
public constructor( | ||
public readonly transform: Transform, | ||
message?: string, | ||
) { | ||
super(message) | ||
} | ||
} | ||
|
||
// json-schema-to-zui-error | ||
export class JsonSchemaToZuiError extends ZuiTransformError { | ||
public constructor(message?: string) { | ||
super('json-schema-to-zui', message) | ||
} | ||
} | ||
|
||
// object-to-zui-error | ||
export class ObjectToZuiError extends ZuiTransformError { | ||
public constructor(message?: string) { | ||
super('object-to-zui', message) | ||
} | ||
} | ||
|
||
// zui-to-json-schema-error | ||
export class ZuiToJsonSchemaError extends ZuiTransformError { | ||
public constructor(message?: string) { | ||
super('zui-to-json-schema', message) | ||
} | ||
} | ||
export class UnsupportedZuiToJsonSchemaError extends ZuiToJsonSchemaError { | ||
public constructor(type: ZodFirstPartyTypeKind) { | ||
super(`Zod type ${type} cannot be transformed to JSON Schema.`) | ||
} | ||
} | ||
|
||
// zui-to-typescript-schema-error | ||
export class ZuiToTypescriptSchemaError extends ZuiTransformError { | ||
public constructor(message?: string) { | ||
super('zui-to-typescript-schema', message) | ||
} | ||
} | ||
export class UnsupportedZuiToTypescriptSchemaError extends ZuiToTypescriptSchemaError { | ||
public constructor(type: ZodFirstPartyTypeKind) { | ||
super(`Zod type ${type} cannot be transformed to TypeScript schema.`) | ||
} | ||
} | ||
|
||
// zui-to-typescript-type-error | ||
export class ZuiToTypescriptTypeError extends ZuiTransformError { | ||
public constructor(message?: string) { | ||
super('zui-to-typescript-type', message) | ||
} | ||
} | ||
export class UnsupportedZuiToTypescriptTypeError extends ZuiToTypescriptTypeError { | ||
public constructor(type: ZodFirstPartyTypeKind | string) { | ||
super(`Zod type ${type} cannot be transformed to TypeScript type.`) | ||
} | ||
} | ||
|
||
export class UntitledDeclarationError extends ZuiToTypescriptTypeError { | ||
public constructor() { | ||
super('Schema must have a title to be transformed to a TypeScript type with a declaration.') | ||
} | ||
} | ||
|
||
export class UnrepresentableGenericError extends ZuiToTypescriptTypeError { | ||
public constructor() { | ||
super(`${ZodFirstPartyTypeKind.ZodRef} can only be transformed to a TypeScript type with a "type" declaration.`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
import z, { ZodTypeAny } from '../../z' | ||
|
||
export class InvalidZuiStringError extends Error { | ||
public constructor(public readonly zuiString: string) { | ||
super(`String "${zuiString}" does not evaluate to a Zod type`) | ||
export type EvalZuiStringResult = | ||
| { | ||
sucess: true | ||
value: ZodTypeAny | ||
} | ||
| { | ||
sucess: false | ||
error: string | ||
} | ||
|
||
export const evalZuiString = (zuiString: string): EvalZuiStringResult => { | ||
let result: any | ||
|
||
try { | ||
result = new Function('z', `return ${zuiString}`)(z) | ||
} catch (thrown) { | ||
const err = thrown instanceof Error ? thrown : new Error(String(thrown)) | ||
return { sucess: false, error: `Failed to evaluate schema: ${err.message}` } | ||
} | ||
} | ||
|
||
export const evalZuiString = (zuiString: string): ZodTypeAny => { | ||
const result = new Function('z', `return ${zuiString}`)(z) | ||
if (!(result instanceof z.ZodType)) { | ||
throw new InvalidZuiStringError(zuiString) | ||
return { sucess: false, error: `String "${zuiString}" does not evaluate to a Zod schema` } | ||
} | ||
|
||
return { | ||
sucess: true, | ||
value: result, | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.