zodselect 1.2.7 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/refine-schema.js +7 -7
- package/dist/refine-schema.spec.js +13 -0
- package/dist/utilities.d.ts +3 -1
- package/dist/utilities.js +6 -0
- package/package.json +1 -1
package/dist/refine-schema.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import {
|
|
2
|
+
import { isZodArray, isZodCatch, isZodDefault, isZodLazy, isZodNullable, isZodObject, isZodOptional, isZodType, isZodUnion } from './utilities.js';
|
|
3
3
|
// Main function to refine the schema
|
|
4
4
|
export function refineSchema(schema, shape) {
|
|
5
5
|
if (isZodObject(schema)) {
|
|
@@ -45,12 +45,12 @@ function refineSchemaField(fieldSchema, fieldShape) {
|
|
|
45
45
|
else if (isZodNullable(fieldSchema)) {
|
|
46
46
|
return refineSchemaField(fieldSchema.def.innerType, fieldShape).nullable();
|
|
47
47
|
}
|
|
48
|
-
else {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
48
|
+
else if (isZodDefault(fieldSchema) || isZodCatch(fieldSchema)) {
|
|
49
|
+
// A default or catch is a whole value, which a picked part no longer fits, so it gives way to what it wraps.
|
|
50
|
+
return refineSchemaField(fieldSchema.def.innerType, fieldShape);
|
|
51
|
+
}
|
|
52
|
+
else if (isZodLazy(fieldSchema)) {
|
|
53
|
+
return refineSchemaField(fieldSchema.def.getter(), fieldShape);
|
|
54
54
|
}
|
|
55
55
|
return fieldSchema;
|
|
56
56
|
}
|
|
@@ -128,6 +128,19 @@ describe('.refineSchema()', () => {
|
|
|
128
128
|
});
|
|
129
129
|
assert.equal(parsedWithoutNestedArray.nestedArray, undefined);
|
|
130
130
|
});
|
|
131
|
+
it('keeps an array under a default, a catch or a lazy an array of the picked fields', () => {
|
|
132
|
+
const line = z.object({ id: z.string(), secret: z.string() });
|
|
133
|
+
const order = z.object({
|
|
134
|
+
caught: z.array(line).catch([]),
|
|
135
|
+
defaulted: z.array(line).default([]),
|
|
136
|
+
lazy: z.lazy(() => z.array(line))
|
|
137
|
+
});
|
|
138
|
+
const refined = refineSchema(order, { caught: { id: true }, defaulted: { id: true }, lazy: { id: true } });
|
|
139
|
+
const lines = [{ id: 'a', secret: 's' }];
|
|
140
|
+
deepStrictEqual(refined.parse({ caught: lines, defaulted: lines, lazy: lines }), { caught: [{ id: 'a' }], defaulted: [{ id: 'a' }], lazy: [{ id: 'a' }] });
|
|
141
|
+
assert.ok(refined.shape.defaulted instanceof z.ZodArray);
|
|
142
|
+
assert.ok(refined.shape.caught instanceof z.ZodArray);
|
|
143
|
+
});
|
|
131
144
|
it('should handle custom fields', () => {
|
|
132
145
|
const userSchema = z.object({
|
|
133
146
|
address: z.custom().optional(),
|
package/dist/utilities.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { ZodArray, ZodLazy, ZodNullable, ZodObject, ZodOptional, ZodRecord, ZodType, ZodUnion, ZodUnknown } from 'zod';
|
|
1
|
+
import { ZodArray, ZodCatch, ZodDefault, ZodLazy, ZodNullable, ZodObject, ZodOptional, ZodRecord, ZodType, ZodUnion, ZodUnknown } from 'zod';
|
|
2
2
|
export declare function getInnerType(schema: ZodType, unwrapLazy?: boolean): ZodType;
|
|
3
3
|
export declare function isZodArray(obj: unknown): obj is ZodArray<ZodType>;
|
|
4
|
+
export declare function isZodCatch(obj: unknown): obj is ZodCatch<ZodType>;
|
|
5
|
+
export declare function isZodDefault(obj: unknown): obj is ZodDefault<ZodType>;
|
|
4
6
|
export declare function isZodLazy(obj: unknown): obj is ZodLazy;
|
|
5
7
|
export declare function isZodNullable(obj: unknown): obj is ZodNullable<ZodType>;
|
|
6
8
|
export declare function isZodObject(obj: unknown, isLoose?: boolean): obj is ZodObject;
|
package/dist/utilities.js
CHANGED
|
@@ -17,6 +17,12 @@ export function getInnerType(schema, unwrapLazy = true) {
|
|
|
17
17
|
export function isZodArray(obj) {
|
|
18
18
|
return isZodType(obj) && (obj.def.type === 'array');
|
|
19
19
|
}
|
|
20
|
+
export function isZodCatch(obj) {
|
|
21
|
+
return isZodType(obj) && (obj.def.type === 'catch');
|
|
22
|
+
}
|
|
23
|
+
export function isZodDefault(obj) {
|
|
24
|
+
return isZodType(obj) && (obj.def.type === 'default');
|
|
25
|
+
}
|
|
20
26
|
export function isZodLazy(obj) {
|
|
21
27
|
return isZodType(obj) && (obj.def.type === 'lazy');
|
|
22
28
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zodselect",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"description": "Type-safe selection utility built on top of Zod. Allows for strongly typed field selection, schema merging, and runtime refinement for frontend or backend use.",
|
|
5
5
|
"author": "SecureDataSystems",
|
|
6
6
|
"license": "MIT",
|