Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/zui",
"version": "2.1.1",
"version": "2.2.1",
"description": "A fork of Zod with additional features",
"type": "module",
"source": "./src/index.ts",
Expand Down
73 changes: 38 additions & 35 deletions packages/zui/src/transforms/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,84 +11,87 @@ type Transform =
export abstract class ZuiTransformError extends Error {
public constructor(
public readonly transform: Transform,
message?: string
message?: string,
public readonly path?: string
) {
super(message)
const msg = path ? `${path} : ${message}` : message
super(msg)
}
}

// json-schema-to-zui-error
export class JSONSchemaToZuiError extends ZuiTransformError {
public constructor(message?: string) {
super('json-schema-to-zui', message)
public constructor(message?: string, path?: string) {
super('json-schema-to-zui', message, path)
}
}
export class UnsupportedJSONSchemaToZuiError extends JSONSchemaToZuiError {
public constructor(schema: JSONSchema7, path?: string) {
super(`JSON Schema ${JSON.stringify(schema)} cannot be transformed to ZUI type.`, path)
}
}

// object-to-zui-error
export class ObjectToZuiError extends ZuiTransformError {
public constructor(message?: string) {
super('object-to-zui', message)
public constructor(message?: string, path?: string) {
super('object-to-zui', message, path)
}
}

// zui-to-json-schema-error
export class ZuiToJSONSchemaError extends ZuiTransformError {
public constructor(message?: string) {
super('zui-to-json-schema', message)
public constructor(message?: string, path?: string) {
super('zui-to-json-schema', message, path)
}
}
export class UnsupportedZuiToJSONSchemaError extends ZuiToJSONSchemaError {
public constructor(type: ZodNativeTypeName, { suggestedAlternative }: { suggestedAlternative?: string } = {}) {
super(
`Zod type ${type} cannot be transformed to JSON Schema.` +
(suggestedAlternative ? ` Suggested alternative: ${suggestedAlternative}` : '')
)
public constructor(
type: ZodNativeTypeName,
path?: string,
{ suggestedAlternative }: { suggestedAlternative?: string } = {}
) {
const msg = suggestedAlternative
? `Zod type ${type} cannot be transformed to JSON Schema. Suggested alternative: ${suggestedAlternative}`
: `Zod type ${type} cannot be transformed to JSON Schema.`
super(msg, path)
}
}
export class UnsupportedZuiCheckToJSONSchemaError extends ZuiToJSONSchemaError {
public constructor({ zodType, checkKind }: { zodType: ZodNativeTypeName; checkKind: string }) {
super(`Zod check .${checkKind}() of type ${zodType} cannot be transformed to JSON Schema.`)
}
}

export class UnsupportedJSONSchemaToZuiError extends JSONSchemaToZuiError {
public constructor(schema: JSONSchema7) {
super(`JSON Schema ${JSON.stringify(schema)} cannot be transformed to ZUI type.`)
public constructor(zodType: ZodNativeTypeName, checkKind: string, path?: string) {
super(`Zod check .${checkKind}() of type ${zodType} cannot be transformed to JSON Schema.`, path)
}
}

// zui-to-typescript-schema-error
export class ZuiToTypescriptSchemaError extends ZuiTransformError {
public constructor(message?: string) {
super('zui-to-typescript-schema', message)
public constructor(message?: string, path?: string) {
super('zui-to-typescript-schema', message, path)
}
}
export class UnsupportedZuiToTypescriptSchemaError extends ZuiToTypescriptSchemaError {
public constructor(type: ZodNativeTypeName) {
super(`Zod type ${type} cannot be transformed to TypeScript schema.`)
public constructor(type: ZodNativeTypeName, path?: string) {
super(`Zod type ${type} cannot be transformed to TypeScript schema.`, path)
}
}

// zui-to-typescript-type-error
export class ZuiToTypescriptTypeError extends ZuiTransformError {
public constructor(message?: string) {
super('zui-to-typescript-type', message)
public constructor(message?: string, path?: string) {
super('zui-to-typescript-type', message, path)
}
}
export class UnsupportedZuiToTypescriptTypeError extends ZuiToTypescriptTypeError {
public constructor(type: ZodNativeTypeName) {
super(`Zod type ${type} cannot be transformed to TypeScript type.`)
public constructor(type: ZodNativeTypeName, path?: string) {
super(`Zod type ${type} cannot be transformed to TypeScript type.`, path)
}
}

export class UntitledDeclarationError extends ZuiToTypescriptTypeError {
public constructor() {
super('Schema must have a title to be transformed to a TypeScript type with a declaration.')
public constructor(path?: string) {
super('Schema must have a title to be transformed to a TypeScript type with a declaration.', path)
}
}

export class UnrepresentableGenericError extends ZuiToTypescriptTypeError {
public constructor() {
super('ZodRef can only be transformed to a TypeScript type with a "type" declaration.')
public constructor(path?: string) {
super('ZodRef can only be transformed to a TypeScript type with a "type" declaration.', path)
}
}
19 changes: 19 additions & 0 deletions packages/zui/src/transforms/zui-to-json-schema/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,23 @@ describe('zuiToJSONSchemaNext', () => {
const schema = toJSONSchema(z.ref('foo'))
expect(schema).toEqual({ $ref: 'foo' })
})

test('should show complete path section in error message', () => {
try {
toJSONSchema(z.object({ foo: z.object({ bar: z.tuple([z.number(), z.void()]) }) }))
expect.fail('should have thrown')
} catch (e) {
expect(e instanceof Error && e.message).toContain('#.foo.bar[1]')
}
})

test('should expose path as a property on the error', () => {
try {
toJSONSchema(z.object({ foo: z.object({ bar: z.tuple([z.number(), z.void()]) }) }))
expect.fail('should have thrown')
} catch (e) {
expect(e).toBeInstanceOf(errs.ZuiTransformError)
expect((e as errs.ZuiTransformError).path).toBe('#.foo.bar[1]')
}
})
})
Loading
Loading