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/orm/src/utils/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type TypeMap = {
Decimal: Decimal;
DateTime: Date;
Bytes: Uint8Array;
Json: JsonValue;
Json: JsonValue | null;
Null: null;
Object: Record<string, unknown>;
Any: unknown;
Expand Down
6 changes: 3 additions & 3 deletions packages/zod/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ type MapFieldTypeToZod<Schema extends SchemaDef, FieldType> = FieldType extends
? z.ZodObject<GetTypeDefFieldsShape<Schema, FieldType>, z.core.$strict>
: z.ZodUnknown;

export type JsonValue = string | number | boolean | JsonObject | JsonArray;
type JsonObject = { [key: string]: JsonValue | null };
type JsonArray = Array<JsonValue | null>;
export type JsonValue = string | number | boolean | JsonObject | JsonArray | null;
type JsonObject = { [key: string]: JsonValue };
type JsonArray = Array<JsonValue>;

type JsonZodType = z.ZodType<JsonValue>;

Expand Down
33 changes: 33 additions & 0 deletions tests/regression/test/issue-2647/regression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createTestClient } from '@zenstackhq/testtools';
import { createSchemaFactory } from '@zenstackhq/zod';
import { describe, expect, expectTypeOf, it } from 'vitest';
import z from 'zod';
import { schema } from './schema';

// https://github.com/zenstackhq/zenstack/issues/2647

const factory = createSchemaFactory(schema);

describe('Regression for issue #2647', () => {
it('ORM-inferred type for a required Json field allows null', async () => {
const db = await createTestClient(schema);
type Test = Awaited<ReturnType<typeof db.test.findFirstOrThrow>>;

// A required Json column can still hold a JSON `null`, so the inferred
// model type for the field must include `null`.
expectTypeOf<null>().toExtend<Test['metaData']>();
});

it('zod-inferred type for a required Json field allows null', () => {
const _schema = factory.makeModelSchema('Test');
type Test = z.infer<typeof _schema>;

expectTypeOf<null>().toExtend<Test['metaData']>();
});

it('zod schema for a required Json field parses null at runtime', () => {
const _schema = factory.makeModelSchema('Test');
const result = _schema.safeParse({ id: 'test', metaData: null });
expect(result.success).toBe(true);
});
});
37 changes: 37 additions & 0 deletions tests/regression/test/issue-2647/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//////////////////////////////////////////////////////////////////////////////////////////////
// DO NOT MODIFY THIS FILE //
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
//////////////////////////////////////////////////////////////////////////////////////////////

/* eslint-disable */

import { type SchemaDef, type AttributeApplication, type FieldDefault, ExpressionUtils } from "@zenstackhq/schema";
export class SchemaType implements SchemaDef {
provider = {
type: "postgresql"
} as const;
models = {
Test: {
name: "Test",
fields: {
id: {
name: "id",
type: "String",
id: true,
attributes: [{ name: "@id" }, { name: "@default", args: [{ name: "value", value: ExpressionUtils.call("uuid") }] }] as readonly AttributeApplication[],
default: ExpressionUtils.call("uuid") as FieldDefault
},
metaData: {
name: "metaData",
type: "Json"
}
},
idFields: ["id"],
uniqueFields: {
id: { type: "String" }
}
}
} as const;
plugins = {};
}
export const schema = new SchemaType();
9 changes: 9 additions & 0 deletions tests/regression/test/issue-2647/schema.zmodel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
datasource db {
provider = 'postgresql'
url = env("DATABASE_URL")
}

model Test {
id String @id @default(uuid())
metaData Json
}
Loading