Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update HyTTS #23

Merged
merged 2 commits into from
Sep 29, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@
"express": "4.18.2",
"lodash": "4.17.21",
"qs": "6.11.2",
"zod": "3.21.4"
"zod": "3.22.2"
},
"devDependencies": {
"@types/express": "4.17.17",
"@types/jest": "29.5.3",
"@types/lodash": "4.14.196",
"@types/node": "18.17.3",
"@typescript-eslint/eslint-plugin": "6.3.0",
"@typescript-eslint/parser": "6.3.0",
"esbuild": "0.19.0",
"eslint-import-resolver-typescript": "3.5.5",
"eslint-plugin-jest": "27.2.3",
"eslint-plugin-import": "2.28.0",
"@types/express": "4.17.18",
"@types/jest": "29.5.5",
"@types/lodash": "4.14.199",
"@types/node": "18.18.1",
"@typescript-eslint/eslint-plugin": "6.7.3",
"@typescript-eslint/parser": "6.7.3",
"esbuild": "0.19.4",
"eslint-import-resolver-typescript": "3.6.1",
"eslint-plugin-jest": "27.4.0",
"eslint-plugin-import": "2.28.1",
"eslint-plugin-no-relative-import-paths": "1.5.2",
"eslint": "8.46.0",
"jest-circus": "29.6.2",
"jest-environment-jsdom": "29.6.2",
"jest": "29.6.2",
"prettier": "3.0.1",
"tslib": "2.6.1",
"eslint": "8.50.0",
"jest-circus": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest": "29.7.0",
"prettier": "3.0.3",
"tslib": "2.6.2",
"tsup": "7.2.0",
"typescript": "5.1.6"
"typescript": "5.2.2"
},
"engines": {
"node": ">=18"
Expand Down
10 changes: 10 additions & 0 deletions source/serialization/data-packing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ describe("data packing", () => {
expect(() => unpack(z.object({}), "test")).toThrow("Data is not an object.");
});

it("handles readonly objects", () => {
expect(
unpack(z.object({ a: z.number(), b: z.string(), c: z.null() }).readonly(), {
a: "1",
b: "",
c: null,
}),
).toStrictEqual({ a: 1, b: "", c: null });
});

it("handles intersections", () => {
const schema = z.object({ a: z.number() }).and(z.object({ b: z.string(), c: z.null() }));
expect(unpack(schema, { a: "1", b: "", c: null })).toStrictEqual({ a: 1, b: "", c: null });
Expand Down
3 changes: 3 additions & 0 deletions source/serialization/data-packing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
ZodNumber,
ZodObject,
ZodOptional,
ZodReadonly,
ZodString,
type ZodType,
type ZodTypeAny,
Expand Down Expand Up @@ -156,6 +157,8 @@ export function unpack<Output, Def extends ZodTypeDef, Input>(
return unpackRecursive(schema.removeDefault(), data, insideUnion);
} else if (schema instanceof ZodOptional) {
return unpackRecursive(schema.unwrap(), data, insideUnion);
} else if (schema instanceof ZodReadonly) {
return unpackRecursive(schema._def.innerType, data, insideUnion);
} else if (schema instanceof ZodEnum) {
return data;
} else if (schema instanceof ZodEffects) {
Expand Down
1 change: 1 addition & 0 deletions source/serialization/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const supporedFirstPartyTypes: Record<ZodFirstPartyTypeKind, boolean> = {
[z.ZodFirstPartyTypeKind.ZodNullable]: true,
[z.ZodFirstPartyTypeKind.ZodDefault]: true,
[z.ZodFirstPartyTypeKind.ZodUnknown]: true,
[z.ZodFirstPartyTypeKind.ZodReadonly]: true,

// Zod types with partial support:
[z.ZodFirstPartyTypeKind.ZodUnion]: false, // mostly HyTTS-internal
Expand Down
8 changes: 8 additions & 0 deletions source/serialization/to-partial-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ describe("structural form schema", () => {

const x3: { b?: number | string } = parseResult3;
const y3: typeof parseResult3 = x3;

const schema4 = toPartialSchema(z.object({ b: z.number() }).readonly());

const parseResult4 = schema4.parse({ b: 1 });
expect(parseResult4).toStrictEqual({ b: 1 });

const x4: { readonly b: number | string } = parseResult4;
const y4: typeof parseResult4 = x4;
});

it("returns strings or actual types for all leaf array elements", () => {
Expand Down
5 changes: 5 additions & 0 deletions source/serialization/to-partial-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ZodObject,
ZodOptional,
type ZodRawShape,
ZodReadonly,
ZodString,
type ZodType,
type ZodTypeAny,
Expand Down Expand Up @@ -106,6 +107,8 @@ export function toPartialSchema<T extends ZodTypeAny>(schema: T): ToPartialSchem
return recurse(schema._def.left).and(recurse(schema._def.right));
} else if (schema instanceof ZodUnion) {
return z.union(schema.options.map(recurse));
} else if (schema instanceof ZodReadonly) {
return schema._def.innerType.readonly();
} else if (schema instanceof ZodAny) {
return schema;
} else {
Expand All @@ -132,6 +135,8 @@ export type ToPartialSchema<T extends ZodTypeAny> = T extends ZodObject<
? ZodOptional<ToPartialSchema<U>>
: T extends ZodIntersection<infer U, infer V>
? ZodIntersection<ToPartialSchema<U>, ToPartialSchema<V>>
: T extends ZodReadonly<infer I>
? ZodReadonly<ToPartialSchema<I>>
: T extends ZodUnion<infer U>
? ZodUnion<ConvertUnionCases<U>>
: ZodUnion<[T, ZodString]>;
Expand Down
6 changes: 6 additions & 0 deletions source/serialization/url-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ describe("uRL search params", () => {
).toStrictEqual({ o: { a: true } });
});

it("roundtrip nested readonly objects", () => {
expect(
roundtrip(z.object({ o: z.object({ a: z.boolean() }) }).readonly(), { o: { a: true } }),
).toStrictEqual({ o: { a: true } });
});

it("roundtrip nested objects and arrays", () => {
expect(
roundtrip(
Expand Down