Fix StartOS 0.4 TypeScript packaging to match SDK API
This commit is contained in:
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright 2025 Amir Farzamnia
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
# Zod Deep Partial
|
||||
|
||||
[](https://www.npmjs.com/package/zod-deep-partial)
|
||||
[](https://www.npmjs.com/package/zod-deep-partial)
|
||||
[](https://www.npmjs.com/package/zod-deep-partial)
|
||||
[](https://www.npmjs.com/package/zod-deep-partial)
|
||||
[](https://www.npmjs.com/package/zod-deep-partial)
|
||||
|
||||
A utility to recursively make all properties in a Zod schema optional.
|
||||
|
||||
## Description
|
||||
|
||||
`zod-deep-partial` is a lightweight, zero-dependency package that provides a single function, `zodDeepPartial`. This function takes any Zod schema and returns a new schema where every property at every level of nesting is optional. This is particularly useful for creating schemas for patch updates or for handling incomplete data structures without sacrificing the benefits of Zod's validation.
|
||||
|
||||
## Features
|
||||
|
||||
- **Deeply Partial:** Makes all properties of a Zod schema optional, including nested objects.
|
||||
- **Type-Safe:** Preserves Zod's powerful type inference.
|
||||
- **Comprehensive Support:** Works with a wide range of Zod types:
|
||||
- Objects (`z.object`)
|
||||
- Arrays (`z.array`)
|
||||
- Unions (`z.union`)
|
||||
- Discriminated Unions (`z.discriminatedUnion`)
|
||||
- Intersections (`z.intersection`)
|
||||
- Tuples (`z.tuple`)
|
||||
- Records (`z.record`)
|
||||
- Maps (`z.map`)
|
||||
- Sets (`z.set`)
|
||||
- Promises (`z.promise`)
|
||||
- Lazy Schemas (`z.lazy`)
|
||||
- Readonly (`z.readonly`)
|
||||
- Default values (`z.default`)
|
||||
- Catch values (`z.catch`)
|
||||
- Prefault values (`z.prefault`)
|
||||
- NonOptional (`z.nonoptional`)
|
||||
- Transforms/Pipes (`z.pipe`, `.transform()`)
|
||||
- All primitive types (string, number, boolean, date, etc.)
|
||||
- **Zero Dependencies:** Relies only on `zod` as a peer dependency.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the package using your favorite package manager:
|
||||
|
||||
### npm
|
||||
|
||||
```bash
|
||||
npm install zod-deep-partial
|
||||
```
|
||||
|
||||
### yarn
|
||||
|
||||
```bash
|
||||
yarn add zod-deep-partial
|
||||
```
|
||||
|
||||
### pnpm
|
||||
|
||||
```bash
|
||||
pnpm add zod-deep-partial
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Here's a simple example of how to use `zodDeepPartial`:
|
||||
|
||||
```typescript
|
||||
import { z } from "zod";
|
||||
import { zodDeepPartial } from "zod-deep-partial";
|
||||
|
||||
// 1. Define your base schema
|
||||
const userSchema = z.object({
|
||||
name: z.string(),
|
||||
email: z.email(),
|
||||
profile: z.object({
|
||||
bio: z.string(),
|
||||
avatar: z.url(),
|
||||
}),
|
||||
tags: z.array(z.string()),
|
||||
});
|
||||
|
||||
// 2. Create the deep partial schema
|
||||
const partialUserSchema = zodDeepPartial(userSchema);
|
||||
|
||||
// 3. Use the partial schema for validation
|
||||
|
||||
// All of these are now valid:
|
||||
partialUserSchema.parse({});
|
||||
partialUserSchema.parse({ name: "John Doe" });
|
||||
partialUserSchema.parse({ profile: {} });
|
||||
partialUserSchema.parse({ profile: { bio: "A developer" } });
|
||||
partialUserSchema.parse({ tags: ["developer"] });
|
||||
|
||||
// Type inference is preserved
|
||||
type PartialUser = z.infer<typeof partialUserSchema>;
|
||||
/*
|
||||
{
|
||||
name?: string | undefined;
|
||||
email?: string | undefined;
|
||||
profile?: {
|
||||
bio?: string | undefined;
|
||||
avatar?: string | undefined;
|
||||
} | undefined;
|
||||
tags?: (string | undefined)[] | undefined;
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome! Please open an issue or submit a pull request on the [GitHub repository](https://github.com/amirfarzamnia/zod-deep-partial).
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
import { z } from "zod";
|
||||
import type { DeepPartial } from "./types";
|
||||
/**
|
||||
* Recursively makes all properties in a Zod schema optional at all levels.
|
||||
*
|
||||
* This is the main entry point for creating deeply partial Zod schemas. It transforms
|
||||
* a Zod schema so that every property, at every level of nesting, becomes optional.
|
||||
* This is useful for:
|
||||
* - Creating partial update schemas
|
||||
* - Handling incomplete data structures
|
||||
* - Building flexible API request/response validators
|
||||
* - Implementing patch operations
|
||||
*
|
||||
* The function preserves type safety and works with all Zod schema types including:
|
||||
* - Objects (with nested objects)
|
||||
* - Arrays
|
||||
* - Unions
|
||||
* - Intersections
|
||||
* - Records
|
||||
* - Tuples
|
||||
* - Lazy/recursive schemas
|
||||
* - Discriminated unions (preserves discriminator as required)
|
||||
*
|
||||
* @template T - The Zod schema type
|
||||
* @param schema - The Zod schema to make deeply partial
|
||||
* @returns A new Zod schema where all properties are optional at all levels
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { z } from "zod";
|
||||
* import { zodDeepPartial } from "zod-deep-partial";
|
||||
*
|
||||
* const userSchema = z.object({
|
||||
* name: z.string(),
|
||||
* email: z.email(),
|
||||
* profile: z.object({
|
||||
* bio: z.string(),
|
||||
* avatar: z.url(),
|
||||
* }),
|
||||
* });
|
||||
*
|
||||
* const partialUserSchema = zodDeepPartial(userSchema);
|
||||
*
|
||||
* // All of these are now valid:
|
||||
* partialUserSchema.parse({});
|
||||
* partialUserSchema.parse({ name: "John" });
|
||||
* partialUserSchema.parse({ profile: { bio: "Developer" } });
|
||||
* ```
|
||||
*/
|
||||
export declare function zodDeepPartial<T extends z.core.SomeType>(schema: T): DeepPartial<T>;
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.zodDeepPartial = zodDeepPartial;
|
||||
const zod_1 = require("zod");
|
||||
/**
|
||||
* Internal recursive implementation of zodDeepPartial.
|
||||
*
|
||||
* This function traverses a Zod schema and recursively makes all properties optional
|
||||
* at every level of nesting. It handles all Zod schema types including objects, arrays,
|
||||
* unions, intersections, records, tuples, lazy schemas, discriminated unions, sets,
|
||||
* promises, readonly, catch, pipe, nonoptional, and prefault.
|
||||
*
|
||||
* For discriminated unions, the discriminator field is preserved as required to maintain
|
||||
* the discriminator's functionality, while all other fields become optional.
|
||||
*
|
||||
* @template T - The Zod schema type being processed
|
||||
* @param schema - The Zod schema to process
|
||||
* @param isTopLevel - Whether this is the top-level call (used to apply strict mode only at root)
|
||||
* @returns A new Zod schema with all properties made optional recursively
|
||||
*
|
||||
* @internal This is an internal function. Use `zodDeepPartial` instead.
|
||||
*/
|
||||
function zodDeepPartialInternal(schema, isTopLevel = false) {
|
||||
// Handle optional schemas by unwrapping and re-applying optional
|
||||
if (schema instanceof zod_1.z.ZodOptional) {
|
||||
return zodDeepPartialInternal(schema.unwrap(), false).optional();
|
||||
}
|
||||
// Handle nullable schemas by unwrapping and re-applying nullable
|
||||
if (schema instanceof zod_1.z.ZodNullable) {
|
||||
return zodDeepPartialInternal(schema.unwrap(), false).nullable();
|
||||
}
|
||||
// Handle default schemas by unwrapping and re-applying default
|
||||
if (schema instanceof zod_1.z.ZodDefault) {
|
||||
const innerResult = zodDeepPartialInternal(schema.unwrap(), false);
|
||||
return innerResult.default(schema.def.defaultValue);
|
||||
}
|
||||
// Handle catch schemas by unwrapping and re-applying catch
|
||||
if (schema instanceof zod_1.z.ZodCatch) {
|
||||
const innerResult = zodDeepPartialInternal(schema.def.innerType, false);
|
||||
return innerResult.catch(schema.def.catchValue);
|
||||
}
|
||||
// Handle prefault schemas by unwrapping and re-applying prefault
|
||||
if (schema instanceof zod_1.z.ZodPrefault) {
|
||||
const innerResult = zodDeepPartialInternal(schema.def.innerType, false);
|
||||
return innerResult.prefault(schema.def.defaultValue);
|
||||
}
|
||||
// Handle nonoptional schemas by unwrapping and re-applying nonoptional
|
||||
if (schema instanceof zod_1.z.ZodNonOptional) {
|
||||
const innerResult = zodDeepPartialInternal(schema.def.innerType, false);
|
||||
return innerResult.nonoptional();
|
||||
}
|
||||
// Handle readonly schemas by unwrapping and re-applying readonly
|
||||
if (schema instanceof zod_1.z.ZodReadonly) {
|
||||
return zodDeepPartialInternal(schema.def.innerType, false).readonly();
|
||||
}
|
||||
// Handle object schemas - the most common case
|
||||
// Recursively process each property and make them optional
|
||||
if (schema instanceof zod_1.z.ZodObject) {
|
||||
const shape = schema.shape;
|
||||
const newShape = {};
|
||||
// Process each property in the object schema
|
||||
for (const key in shape) {
|
||||
newShape[key] = zodDeepPartialInternal(shape[key], false).optional();
|
||||
}
|
||||
// Create new object with processed shape and apply partial()
|
||||
let result = zod_1.z.object(newShape).partial();
|
||||
// Apply strict mode only at top level to prevent unknown properties
|
||||
if (isTopLevel) {
|
||||
result = result.strict();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// Handle array schemas - recursively process element type
|
||||
if (schema instanceof zod_1.z.ZodArray) {
|
||||
return zod_1.z
|
||||
.array(zodDeepPartialInternal(schema.def.element, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle map schemas - recursively process both key and value types
|
||||
if (schema instanceof zod_1.z.ZodMap) {
|
||||
return zod_1.z
|
||||
.map(zodDeepPartialInternal(schema.def.keyType, false), zodDeepPartialInternal(schema.def.valueType, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle set schemas - recursively process value type
|
||||
if (schema instanceof zod_1.z.ZodSet) {
|
||||
return zod_1.z
|
||||
.set(zodDeepPartialInternal(schema.def.valueType, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle promise schemas - recursively process inner type
|
||||
if (schema instanceof zod_1.z.ZodPromise) {
|
||||
return zod_1.z
|
||||
.promise(zodDeepPartialInternal(schema.def.innerType, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle union schemas - recursively process each option
|
||||
if (schema instanceof zod_1.z.ZodUnion) {
|
||||
return zod_1.z
|
||||
.union(schema.options.map((opt) => zodDeepPartialInternal(opt, false)))
|
||||
.optional();
|
||||
}
|
||||
// Handle intersection schemas - recursively process both left and right sides
|
||||
if (schema instanceof zod_1.z.ZodIntersection) {
|
||||
return zod_1.z
|
||||
.intersection(zodDeepPartialInternal(schema.def.left, false), zodDeepPartialInternal(schema.def.right, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle record schemas - recursively process value type (keys remain unchanged)
|
||||
if (schema instanceof zod_1.z.ZodRecord) {
|
||||
return zod_1.z
|
||||
.record(zodDeepPartialInternal(schema.def.keyType, false), zodDeepPartialInternal(schema.def.valueType, false))
|
||||
.optional();
|
||||
}
|
||||
// Handle tuple schemas - recursively process each item in the tuple
|
||||
if (schema instanceof zod_1.z.ZodTuple) {
|
||||
return zod_1.z
|
||||
.tuple(schema.def.items.map((item) => zodDeepPartialInternal(item, false)))
|
||||
.optional();
|
||||
}
|
||||
// Handle lazy schemas - recursively process the lazy getter function
|
||||
if (schema instanceof zod_1.z.ZodLazy) {
|
||||
return zod_1.z
|
||||
.lazy(() => zodDeepPartialInternal(schema.def.getter(), false))
|
||||
.optional();
|
||||
}
|
||||
// Handle discriminated unions - special case that preserves the discriminator field as required
|
||||
// This ensures the union can still be properly discriminated even when other fields are optional
|
||||
if (schema instanceof zod_1.z.ZodDiscriminatedUnion) {
|
||||
const options = schema.options.map((option) => {
|
||||
if (option instanceof zod_1.z.ZodObject) {
|
||||
const shape = option.shape;
|
||||
const newShape = {};
|
||||
// Process each field in the discriminated union option
|
||||
for (const key in shape) {
|
||||
if (key === schema.def.discriminator) {
|
||||
// Keep discriminator field required to maintain discriminator functionality
|
||||
newShape[key] = shape[key];
|
||||
}
|
||||
else {
|
||||
// Make all other fields optional
|
||||
newShape[key] = zodDeepPartialInternal(shape[key], false).optional();
|
||||
}
|
||||
}
|
||||
return zod_1.z.object(newShape);
|
||||
}
|
||||
return zodDeepPartialInternal(option, false);
|
||||
});
|
||||
return zod_1.z.discriminatedUnion(schema.def.discriminator, options);
|
||||
}
|
||||
// Handle pipe schemas - preserve both input and output types as-is
|
||||
// Note: We don't deep-partial the input of pipes because transforms/refinements
|
||||
// have specific input requirements. The parent object's .optional() handles optionality.
|
||||
if (schema instanceof zod_1.z.ZodPipe) {
|
||||
return schema;
|
||||
}
|
||||
// Fallback for any other schema types - simply make them optional
|
||||
return schema.optional();
|
||||
}
|
||||
/**
|
||||
* Recursively makes all properties in a Zod schema optional at all levels.
|
||||
*
|
||||
* This is the main entry point for creating deeply partial Zod schemas. It transforms
|
||||
* a Zod schema so that every property, at every level of nesting, becomes optional.
|
||||
* This is useful for:
|
||||
* - Creating partial update schemas
|
||||
* - Handling incomplete data structures
|
||||
* - Building flexible API request/response validators
|
||||
* - Implementing patch operations
|
||||
*
|
||||
* The function preserves type safety and works with all Zod schema types including:
|
||||
* - Objects (with nested objects)
|
||||
* - Arrays
|
||||
* - Unions
|
||||
* - Intersections
|
||||
* - Records
|
||||
* - Tuples
|
||||
* - Lazy/recursive schemas
|
||||
* - Discriminated unions (preserves discriminator as required)
|
||||
*
|
||||
* @template T - The Zod schema type
|
||||
* @param schema - The Zod schema to make deeply partial
|
||||
* @returns A new Zod schema where all properties are optional at all levels
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { z } from "zod";
|
||||
* import { zodDeepPartial } from "zod-deep-partial";
|
||||
*
|
||||
* const userSchema = z.object({
|
||||
* name: z.string(),
|
||||
* email: z.email(),
|
||||
* profile: z.object({
|
||||
* bio: z.string(),
|
||||
* avatar: z.url(),
|
||||
* }),
|
||||
* });
|
||||
*
|
||||
* const partialUserSchema = zodDeepPartial(userSchema);
|
||||
*
|
||||
* // All of these are now valid:
|
||||
* partialUserSchema.parse({});
|
||||
* partialUserSchema.parse({ name: "John" });
|
||||
* partialUserSchema.parse({ profile: { bio: "Developer" } });
|
||||
* ```
|
||||
*/
|
||||
function zodDeepPartial(schema) {
|
||||
return zodDeepPartialInternal(schema, true);
|
||||
}
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AA0PA,wCAIC;AA9PD,6BAAwB;AAGxB;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAS,sBAAsB,CAC7B,MAAS,EACT,aAAsB,KAAK;IAE3B,iEAAiE;IACjE,IAAI,MAAM,YAAY,OAAC,CAAC,WAAW,EAAE,CAAC;QACpC,OAAO,sBAAsB,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;IACnE,CAAC;IAED,iEAAiE;IACjE,IAAI,MAAM,YAAY,OAAC,CAAC,WAAW,EAAE,CAAC;QACpC,OAAO,sBAAsB,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,CAAC,QAAQ,EAAE,CAAC;IACnE,CAAC;IAED,+DAA+D;IAC/D,IAAI,MAAM,YAAY,OAAC,CAAC,UAAU,EAAE,CAAC;QACnC,MAAM,WAAW,GAAG,sBAAsB,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,CAAC,CAAC;Q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E;IAClE,OAAQ,MAAc,CAAC,QAAQ,EAAE,CAAC;AACpC,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,SAAgB,cAAc,CAC5B,MAAS;IAET,OAAO,sBAAsB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC"}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
import { z, ZodObject, ZodArray, ZodUnion, ZodIntersection, ZodRecord, ZodTuple, ZodLazy, ZodOptional, ZodNullable, ZodDefault, ZodMap, ZodSet, ZodPromise, ZodReadonly, ZodCatch, ZodPipe, ZodNonOptional, ZodPrefault } from "zod";
|
||||
/**
|
||||
* A type utility that recursively makes all properties in a Zod schema optional.
|
||||
*
|
||||
* This type is the TypeScript counterpart to the `zodDeepPartial` function. It provides
|
||||
* proper type inference for deeply partial schemas, ensuring that:
|
||||
*
|
||||
* 1. All object properties become optional at all nesting levels
|
||||
* 2. Array element types are recursively made partial
|
||||
* 3. Union and intersection types are properly handled
|
||||
* 4. Record value types are made partial
|
||||
* 5. Tuple element types are made partial
|
||||
* 6. Lazy/recursive schemas maintain their recursive structure
|
||||
* 7. Optional and nullable wrappers are preserved
|
||||
* 8. Default values are preserved
|
||||
* 9. Map value types are made partial
|
||||
* 10. Set element types are made partial
|
||||
* 11. Promise inner types are made partial
|
||||
* 12. Readonly wrappers are preserved
|
||||
* 13. Catch wrappers are preserved
|
||||
* 14. Pipe schemas are preserved as-is (transforms have specific input requirements)
|
||||
* 15. NonOptional wrappers are preserved
|
||||
* 16. Prefault wrappers are preserved
|
||||
*
|
||||
* The type handles all Zod schema variants through a series of conditional type checks,
|
||||
* ensuring complete type safety when working with deeply partial schemas.
|
||||
*
|
||||
* @template T - The Zod schema type to make deeply partial
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* import { z } from "zod";
|
||||
* import type { DeepPartial } from "zod-deep-partial";
|
||||
*
|
||||
* const schema = z.object({
|
||||
* name: z.string(),
|
||||
* profile: z.object({
|
||||
* bio: z.string(),
|
||||
* age: z.number(),
|
||||
* }),
|
||||
* });
|
||||
*
|
||||
* type PartialSchema = DeepPartial<typeof schema>;
|
||||
* // Results in a type where all properties are optional at all levels
|
||||
* ```
|
||||
*/
|
||||
export type DeepPartial<T extends z.core.SomeType> = T extends ZodOptional<infer Inner> ? ZodOptional<DeepPartial<Inner>> : T extends ZodNullable<infer Inner> ? ZodNullable<DeepPartial<Inner>> : T extends ZodDefault<infer Inner> ? ZodDefault<DeepPartial<Inner>> : T extends ZodCatch<infer Inner> ? ZodCatch<DeepPartial<Inner>> : T extends ZodPrefault<infer Inner> ? ZodPrefault<DeepPartial<Inner>> : T extends ZodNonOptional<infer Inner> ? ZodNonOptional<DeepPartial<Inner>> : T extends ZodReadonly<infer Inner> ? ZodReadonly<DeepPartial<Inner>> : T extends ZodObject<infer Shape> ? ZodObject<{
|
||||
[K in keyof Shape]: ZodOptional<DeepPartial<Shape[K]>>;
|
||||
}> : T extends ZodArray<infer Type> ? ZodArray<DeepPartial<Type>> : T extends ZodMap<infer Key, infer Value> ? ZodMap<Key, DeepPartial<Value>> : T extends ZodSet<infer Type> ? ZodSet<DeepPartial<Type>> : T extends ZodPromise<infer Type> ? ZodPromise<DeepPartial<Type>> : T extends ZodUnion<infer Options> ? ZodUnion<{
|
||||
[K in keyof Options]: DeepPartial<Options[K]>;
|
||||
}> : T extends ZodIntersection<infer Left, infer Right> ? ZodIntersection<DeepPartial<Left>, DeepPartial<Right>> : T extends ZodRecord<infer Key, infer Value> ? ZodRecord<Key, DeepPartial<Value>> : T extends ZodTuple<infer Items> ? ZodTuple<{
|
||||
[K in keyof Items]: DeepPartial<Items[K]>;
|
||||
} extends infer U ? U extends any[] ? U : never : never> : T extends ZodPipe<infer In, infer Out> ? ZodPipe<In, Out> : T extends ZodLazy<infer Type> ? ZodLazy<DeepPartial<Type>> : ZodOptional<T>;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
//# sourceMappingURL=types.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "zod-deep-partial",
|
||||
"version": "1.4.4",
|
||||
"description": "A utility to recursively make all properties in a Zod schema optional.",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "vitest run",
|
||||
"prepublishOnly": "pnpm build && pnpm test"
|
||||
},
|
||||
"keywords": [
|
||||
"zod",
|
||||
"deep",
|
||||
"partial",
|
||||
"nested",
|
||||
"object"
|
||||
],
|
||||
"author": "Amir Farzamnia <amirfarzamnia@proton.me>",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"typescript": "^5.9.3",
|
||||
"vitest": "^4.0.15",
|
||||
"zod": "^4.1.13"
|
||||
},
|
||||
"files": [
|
||||
"dist/**/*"
|
||||
],
|
||||
"homepage": "https://github.com/amirfarzamnia/zod-deep-partial",
|
||||
"bugs": {
|
||||
"url": "https://github.com/amirfarzamnia/zod-deep-partial/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/amirfarzamnia/zod-deep-partial.git"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^4.1.13"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user