Socket
Socket
Sign inDemoInstall

zod

Package Overview
Dependencies
Maintainers
2
Versions
361
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod - npm Package Compare versions

Comparing version 2.0.0-beta.1 to 2.0.0-beta.2

20

lib/src/crazySchema.d.ts

@@ -9,3 +9,3 @@ import * as z from './index';

k2: z.ZodNumber;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;
union: import("./types/array").ZodNonEmptyArray<z.ZodUnion<[z.ZodLiteral<"asdf">, z.ZodLiteral<12>]>>;

@@ -17,12 +17,12 @@ array: z.ZodArray<z.ZodNumber>;

p1: z.ZodOptional<z.ZodString>;
}, "allow", z.ZodTypeAny>, z.ZodObject<{
}, "passthrough", z.ZodTypeAny>, z.ZodObject<{
p1: z.ZodOptional<z.ZodNumber>;
}, "allow", z.ZodTypeAny>>;
}, "passthrough", z.ZodTypeAny>>;
enum: z.ZodIntersection<z.ZodEnum<["zero", "one"]>, z.ZodEnum<["one", "two"]>>;
nonstrict: z.ZodObject<{
points: z.ZodNumber;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;
numProm: z.ZodPromise<z.ZodNumber>;
lenfun: z.ZodFunction<z.ZodTuple<[z.ZodString]>, z.ZodBoolean>;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;
export declare const asyncCrazySchema: z.ZodObject<{

@@ -32,3 +32,3 @@ array: z.ZodArray<z.ZodNumber>;

points: z.ZodNumber;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;
tuple: z.ZodTuple<[z.ZodOptional<z.ZodNullable<z.ZodString>>, z.ZodOptional<z.ZodNullable<z.ZodNumber>>, z.ZodOptional<z.ZodNullable<z.ZodBoolean>>, z.ZodOptional<z.ZodNullable<z.ZodNull>>, z.ZodOptional<z.ZodNullable<z.ZodUndefined>>, z.ZodOptional<z.ZodNullable<z.ZodLiteral<"1234">>>]>;

@@ -40,3 +40,3 @@ merged: z.ZodObject<{

k2: z.ZodNumber;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;
union: import("./types/array").ZodNonEmptyArray<z.ZodUnion<[z.ZodLiteral<"asdf">, z.ZodLiteral<12>]>>;

@@ -47,5 +47,5 @@ sumTransformer: z.ZodTransformer<z.ZodArray<z.ZodNumber>, z.ZodNumber>;

p1: z.ZodOptional<z.ZodString>;
}, "allow", z.ZodTypeAny>, z.ZodObject<{
}, "passthrough", z.ZodTypeAny>, z.ZodObject<{
p1: z.ZodOptional<z.ZodNumber>;
}, "allow", z.ZodTypeAny>>;
}, "passthrough", z.ZodTypeAny>>;
enum: z.ZodIntersection<z.ZodEnum<["zero", "one"]>, z.ZodEnum<["one", "two"]>>;

@@ -57,2 +57,2 @@ numProm: z.ZodPromise<z.ZodNumber>;

async_refine: z.ZodArray<z.ZodNumber>;
}, "allow", z.ZodTypeAny>;
}, "passthrough", z.ZodTypeAny>;

@@ -45,3 +45,3 @@ import { ZodString, ZodStringDef } from './types/string';

declare const arrayType: <T extends ZodTypeAny>(schema: T) => ZodArray<T>;
declare const objectType: <T extends import("./types/base").ZodRawShape>(shape: T) => ZodObject<T, "allow", ZodTypeAny>;
declare const objectType: <T extends import("./types/base").ZodRawShape>(shape: T) => ZodObject<T, "passthrough", ZodTypeAny>;
declare const unionType: <T extends [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]>(types: T) => ZodUnion<T>;

@@ -71,3 +71,3 @@ declare const intersectionType: <T extends ZodTypeAny, U extends ZodTypeAny>(left: T, right: U) => ZodIntersection<T, U>;

export declare const late: {
object: <T extends import("./types/base").ZodRawShape>(shape: () => T) => ZodObject<T, "allow", ZodTypeAny>;
object: <T extends import("./types/base").ZodRawShape>(shape: () => T) => ZodObject<T, "passthrough", ZodTypeAny>;
};

@@ -74,0 +74,0 @@ export { ZodString, ZodNumber, ZodBigInt, ZodBoolean, ZodDate, ZodUndefined, ZodNull, ZodAny, ZodUnknown, ZodNever, ZodVoid, ZodArray, ZodObject, ZodUnion, ZodIntersection, ZodTuple, ZodRecord, ZodFunction, ZodLazy, ZodLiteral, ZodEnum, ZodNativeEnum, ZodPromise, ZodTransformer, ZodOptional, ZodNullable, ZodType, ZodType as Schema, ZodType as ZodSchema, ZodTypeAny, ZodDef, ZodErrorMap, ZodParsedType, ZodCodeGenerator, };

@@ -376,3 +376,3 @@ "use strict";

if (def.catchall instanceof index_1.ZodNever) {
if (def.unknownKeys === 'allow') {
if (def.unknownKeys === 'passthrough') {
for (var _a = 0, extraKeys_1 = extraKeys; _a < extraKeys_1.length; _a++) {

@@ -379,0 +379,0 @@ var key = extraKeys_1[_a];

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var z = __importStar(require("."));
var run = function () { return __awaiter(void 0, void 0, void 0, function () {
var coercedString;
return __generator(this, function (_a) {
console.log(z
.string()
.transform(function (val) { return val.replace('pretty', 'extremely'); })
.transform(function (val) { return val.toUpperCase(); })
.transform(function (val) { return val.split(' ').join('👏'); })
.parse('zod 2 is pretty cool'));
coercedString = z.unknown().transform(z.string(), function (val) { return "" + val; });
console.log(typeof coercedString.parse(false));
console.log(typeof coercedString.parse(12));
return [2 /*return*/];
});
}); };
run();
//# sourceMappingURL=playground.js.map
import * as z from './base';
import { objectUtil } from '../helpers/objectUtil';
import { partialUtil } from '../helpers/partialUtil';
declare type UnknownKeysParam = 'allow' | 'strict' | 'strip';
declare type UnknownKeysParam = 'passthrough' | 'strict' | 'strip';
export interface ZodObjectDef<T extends z.ZodRawShape = z.ZodRawShape, UnknownKeys extends UnknownKeysParam = UnknownKeysParam, Catchall extends z.ZodTypeAny = z.ZodTypeAny> extends z.ZodTypeDef {

@@ -25,3 +25,3 @@ t: z.ZodTypes.object;

export declare type AnyZodObject = ZodObject<any, any, any>;
export declare class ZodObject<T extends z.ZodRawShape, UnknownKeys extends UnknownKeysParam = 'allow', Catchall extends z.ZodTypeAny = z.ZodTypeAny> extends z.ZodType<objectOutputType<T, Catchall>, ZodObjectDef<T, UnknownKeys, Catchall>, objectInputType<T, Catchall>> {
export declare class ZodObject<T extends z.ZodRawShape, UnknownKeys extends UnknownKeysParam = 'passthrough', Catchall extends z.ZodTypeAny = z.ZodTypeAny> extends z.ZodType<objectOutputType<T, Catchall>, ZodObjectDef<T, UnknownKeys, Catchall>, objectInputType<T, Catchall>> {
readonly _shape: T;

@@ -38,5 +38,5 @@ readonly _unknownKeys: UnknownKeys;

strict: () => ZodObject<T, "strict", Catchall>;
stripUnknown: () => ZodObject<T, "strip", Catchall>;
allowUnknown: () => ZodObject<T, "allow", Catchall>;
nonstrict: () => ZodObject<T, "allow", Catchall>;
strip: () => ZodObject<T, "strip", Catchall>;
passthrough: () => ZodObject<T, "passthrough", Catchall>;
nonstrict: () => ZodObject<T, "passthrough", Catchall>;
augment: <Augmentation extends z.ZodRawShape>(augmentation: Augmentation) => ZodObject<{ [k in Exclude<keyof T, keyof Augmentation>]: T[k]; } & { [k_1 in keyof Augmentation]: Augmentation[k_1]; }, UnknownKeys, Catchall>;

@@ -57,5 +57,5 @@ extend: <Augmentation extends z.ZodRawShape>(augmentation: Augmentation) => ZodObject<{ [k in Exclude<keyof T, keyof Augmentation>]: T[k]; } & { [k_1 in keyof Augmentation]: Augmentation[k_1]; }, UnknownKeys, Catchall>;

deepPartial: () => partialUtil.RootDeepPartial<this>;
static create: <T_1 extends z.ZodRawShape>(shape: T_1) => ZodObject<T_1, "allow", z.ZodTypeAny>;
static lazycreate: <T_1 extends z.ZodRawShape>(shape: () => T_1) => ZodObject<T_1, "allow", z.ZodTypeAny>;
static create: <T_1 extends z.ZodRawShape>(shape: T_1) => ZodObject<T_1, "passthrough", z.ZodTypeAny>;
static lazycreate: <T_1 extends z.ZodRawShape>(shape: () => T_1) => ZodObject<T_1, "passthrough", z.ZodTypeAny>;
}
export {};

@@ -72,9 +72,9 @@ "use strict";

};
_this.stripUnknown = function () {
_this.strip = function () {
return new ZodObject(__assign(__assign({}, _this._def), { unknownKeys: 'strip' }));
};
_this.allowUnknown = function () {
return new ZodObject(__assign(__assign({}, _this._def), { unknownKeys: 'allow' }));
_this.passthrough = function () {
return new ZodObject(__assign(__assign({}, _this._def), { unknownKeys: 'passthrough' }));
};
_this.nonstrict = _this.allowUnknown;
_this.nonstrict = _this.passthrough;
// opt optional: () => ZodUnion<[this, ZodUndefined]> = () => ZodUnion.create([this, ZodUndefined.create()]);

@@ -197,3 +197,3 @@ // nullable: () => ZodUnion<[this, ZodNull]> = () => ZodUnion.create([this, ZodNull.create()]);

shape: function () { return shape; },
unknownKeys: 'allow',
unknownKeys: 'strip',
catchall: __1.ZodNever.create(),

@@ -206,3 +206,3 @@ });

shape: shape,
unknownKeys: 'allow',
unknownKeys: 'strip',
catchall: __1.ZodNever.create(),

@@ -209,0 +209,0 @@ });

{
"name": "zod",
"version": "2.0.0-beta.1",
"version": "2.0.0-beta.2",
"description": "TypeScript-first schema declaration and validation library with static type inference",

@@ -59,2 +59,2 @@ "main": "./lib/src/index.js",

}
}
}

@@ -34,3 +34,3 @@ <p align="center">

- Asynchronous refinements and new `.parseAsync` and `.safeParseAsync` methods. Read more here: [Refinements](#refinements)
- New object methods: `.stripUnknown()`, `.strict()`, and `.catchall()`. Read more here: [Objects](#objects)
- New object methods: `.passthrough()`, `.strict()`, and `.catchall()`. Read more here: [Objects](#objects)

@@ -41,3 +41,3 @@ In almost all cases, you'll be able to upgrade to Zod 2 without changing any code. Here are some of the (very minor) breaking changes:

- Relatedly, Zod _no longer_ supports cyclical _data_. Recursive schemas are still supported, but Zod can't properly parse nested objects that contain cycles.
- Object schemas now _allow_ unknown keys by default, instead of throwing an error
- Object schemas now strip unknown keys by default, instead of throwing an error
- Optional and nullable schemas are now represented with the dedicated ZodOptional and ZodNullable classes, instead of using ZodUnion.

@@ -121,3 +121,3 @@

- [.partial/.deepPartial](#partials)
- [.stripUnknown](#strip-unknown-keys)
- [.strip](#strip-unknown-keys)
- [.strict](#disallow-unknown-keys)

@@ -692,5 +692,5 @@ - [.primitives/.nonprimitives](#primitives-and-nonprimitives)

Zod object schemas allow unknown keys.
By default Zod object schema strip unknown keys from the output.
> ⚠️ Before Zod 2 object schema did NOT allow unknown keys by default.
> ⚠️ Before version 2, Zod did NOT allow unknown keys by default.

@@ -708,9 +708,11 @@ Zod will return

});
// => { name: "bob dylan", extraKey: 61 }
// => { name: "bob dylan" }
```
#### Strip unknown keys
#### Pass through unknown keys
If you want to strip out unknown keys, use `.stripUnknown`:
If you want to pass through unknown keys, use `.passthrough()`.
> For backwards compatibility, you can also use `.nonstrict()` which behaves identically.
```ts

@@ -721,3 +723,3 @@ const person = z

})
.stripUnknown();
.passthrough();

@@ -728,3 +730,3 @@ person.parse({

});
// => { name: "bob dylan" }
// => { name: "bob dylan", extraKey: 61 }
```

@@ -734,3 +736,3 @@

You can _disallow_ unknown keys with `.strict()`
You can _disallow_ unknown keys with `.strict()`. If there are any unknown keys in the input, Zod will throw an error.

@@ -805,3 +807,3 @@ ```ts

> Using `.catchall()` overrides `.stripUnknown()` or `.strict()`. All keys are now considered "known".
> Using `.catchall()` overrides `.passsthrough()`, `.strip()`, or `.strict()`. All keys are now considered "known".

@@ -1168,6 +1170,6 @@ ## Records

const b = z.union([z.number(), z.boolean()]);
const c = z.intersection(a, b);
type c = z.infer<typeof C>; // => number
type c = z.infer<typeof c>; // => number
const stringAndNumber = z.intersection(z.string(), z.number());

@@ -1177,5 +1179,5 @@ type Never = z.infer<typeof stringAndNumber>; // => never

Intersections in Zod are not smart. Whatever data you pass into `.parse()` gets passed into the two intersected schemas. Because Zod object schemas don't allow any unknown keys by default, there are some unintuitive behavior surrounding intersections of object schemas.
<!-- Intersections in Zod are not smart. Whatever data you pass into `.parse()` gets passed into the two intersected schemas. Because Zod object schemas don't allow any unknown keys by default, there are some unintuitive behavior surrounding intersections of object schemas. -->
````ts
<!-- ```ts
const A = z.object({

@@ -1193,3 +1195,3 @@ a: z.string(),

// { id:string; name:string };
``` -->
``` -->

@@ -1212,3 +1214,3 @@ ## Tuples

// type Athlete = [string, number, { pointsScored: number }]
````
```

@@ -1681,3 +1683,3 @@ ## Recursive types

- Missing object methods: (pick, omit, partial, deepPartial, merge, extend)
<!-- - Missing object methods: (pick, omit, partial, deepPartial, merge, extend)
- Missing nonempty arrays with proper typing (`[T, ...T[]]`)

@@ -1689,3 +1691,3 @@ - Missing lazy/recursive types

- Missing support for parsing cyclical data (maybe)
- Missing error customization
- Missing error customization -->

@@ -1692,0 +1694,0 @@ #### Joi

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc