Comparing version 1.0.9 to 1.0.10
@@ -14,2 +14,4 @@ "use strict"; | ||
} | ||
// export class ZodError extends Error{ | ||
// } | ||
exports.ZodParser = function (schemaDef) { return function (obj) { | ||
@@ -16,0 +18,0 @@ var def = schemaDef; |
@@ -26,2 +26,3 @@ import { ZodTuple } from './tuple'; | ||
export declare type TypeOf<T extends ZodAny> = T['_type']; | ||
export declare type Infer<T extends ZodAny> = T['_type']; | ||
export declare type TypeOfTuple<T extends [ZodAny, ...ZodAny[]] | []> = { | ||
@@ -35,3 +36,4 @@ [k in keyof T]: T[k] extends ZodType<infer U> ? U : never; | ||
parse: (x: unknown) => Type; | ||
is(u: any): u is Type; | ||
is(u: unknown): u is Type; | ||
check(u: unknown): u is Type; | ||
constructor(def: Def); | ||
@@ -38,0 +40,0 @@ abstract toJSON: () => object; |
@@ -47,2 +47,11 @@ "use strict"; | ||
}; | ||
ZodType.prototype.check = function (u) { | ||
try { | ||
this.parse(u); | ||
return true; | ||
} | ||
catch (err) { | ||
return false; | ||
} | ||
}; | ||
return ZodType; | ||
@@ -49,0 +58,0 @@ }()); |
{ | ||
"name": "zod", | ||
"version": "1.0.9", | ||
"description": "Typescript-first schema declaration and validation library with static type inference", | ||
"version": "1.0.10", | ||
"description": "TypeScript-first schema declaration and validation library with static type inference", | ||
"main": "./lib/index.js", | ||
@@ -6,0 +6,0 @@ "types": "./lib/index.d.ts", |
@@ -10,9 +10,13 @@ # Zod | ||
- [Primitives](#primitives) | ||
- [Literals](#literals) | ||
- [Parsing](#parsing) | ||
- [Type inference](#type-inference) | ||
- [Object types](#objects) | ||
- [Array types](#arrays) | ||
- [Union types](#unions) | ||
- [Tuple types](#tuples) | ||
- [Intersection types](#intersections) | ||
- [Objects](#objects) | ||
- [Arrays](#arrays) | ||
- [Unions](#unions) | ||
- [Optional types](#optional-types) | ||
- [Nullable types](#nullable-types) | ||
- [Enums](#enums) | ||
- [Tuples](#tuples) | ||
- [Intersections](#intersections) | ||
- [Recursive types](#recursive-types) | ||
@@ -22,2 +26,7 @@ - [Function schemas](#function-schemas) | ||
- [Joi](#joi) | ||
- [Yup](#yup) | ||
- [io-ts](#io-ts) | ||
- [Runtypes](#runtypes) | ||
# Installation | ||
@@ -35,11 +44,11 @@ | ||
### Typescript versions | ||
### TypeScript versions | ||
Zod 1.0.x is compatible with Typescript 3.0+. | ||
Zod 1.0.x is compatible with TypeScript 3.0+. | ||
# Usage | ||
Zod is a validation library designed for optimal developer experience. It's a Typescript-first schema declaration library with rigorous (and correct!) inferred types, incredible developer experience, and a few killer features missing from the existing libraries. | ||
Zod is a validation library designed for optimal developer experience. It's a TypeScript-first schema declaration library with rigorous (and correct!) inferred types, incredible developer experience, and a few killer features missing from the existing libraries. | ||
- It takes advantage of Typescript generic inference to statically infer the types of your schemas, eliminating the need to define static types and runtime validators separately. | ||
- It takes advantage of TypeScript generic inference to statically infer the types of your schemas, eliminating the need to define static types and runtime validators separately. | ||
- Eliminates the need to keep static types and runtime validators in sync by hand | ||
@@ -51,3 +60,3 @@ - It has a composable, declarative API that makes it easy to define complex types concisely. | ||
- All fields are required unless explicitly marked as optional (just like Typescript!) | ||
- All fields are required unless explicitly marked as optional (just like TypeScript!) | ||
- Schemas are immutable; methods (i.e. `.optional()` return a new instance. | ||
@@ -86,3 +95,3 @@ - Zod schemas operate on a ["Parse, don't validate!"](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/) basis! | ||
You can also use a Zod schema as a type guard using the schema's `.is()` method, like so: | ||
You can also use a Zod schema as a type guard using the schema's `.check()` method, like so: | ||
@@ -104,3 +113,3 @@ ```ts | ||
const process = (blob: any) => { | ||
if (!stringSchema.is(blob)) { | ||
if (!stringSchema.check(blob)) { | ||
throw new Error('Not a string'); | ||
@@ -116,3 +125,3 @@ } | ||
You can extract the Typescript type of any schema with `z.TypeOf<>`. | ||
You can extract the TypeScript type of any schema with `z.TypeOf<>`. | ||
@@ -244,3 +253,3 @@ ```ts | ||
### Enums | ||
## Enums | ||
@@ -345,3 +354,3 @@ You can combine unions and string literals to create an enum schemas. | ||
You can define a recursive schema in Zod, but because of a limitation of Typescript, their type can't be statically inferred. If you need a recursive Zod schema you'll need to define the type definition manually, and provide it to Zod as a "type hint". | ||
You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. If you need a recursive Zod schema you'll need to define the type definition manually, and provide it to Zod as a "type hint". | ||
@@ -412,3 +421,3 @@ ```ts | ||
// Typescript statically verifies that value returned by | ||
// TypeScript statically verifies that value returned by | ||
// this function is of type { id: string; name: string; }[] | ||
@@ -438,6 +447,14 @@ | ||
### Yup (https://github.com/jquense/yup) | ||
### Joi | ||
Yup is a full-featured library that was implemented first in vanilla JS, with Typescript typings added later. | ||
[https://github.com/hapijs/joi](https://github.com/hapijs/joi) | ||
Doesn't support static type inference. 😕 | ||
### Yup | ||
[https://github.com/jquense/yup](https://github.com/jquense/yup) | ||
Yup is a full-featured library that was implemented first in vanilla JS, with TypeScript typings added later. | ||
Yup supports static type inference, but unfortunately the inferred types aren't actually correct. Currently, the yup package treats all object properties as optional by default: | ||
@@ -473,6 +490,8 @@ | ||
These may sound like nitpicks. But it's not acceptable that an object that's assignable to the inferred Typescript type can fail validation by the validator it was inferred from. | ||
These may sound like nitpicks. But it's not acceptable that an object that's assignable to the inferred TypeScript type can fail validation by the validator it was inferred from. | ||
### io-ts ([https://github.com/gcanti/io-ts](https://github.com/gcanti/io-ts)) | ||
### io-ts | ||
[https://github.com/gcanti/io-ts](https://github.com/gcanti/io-ts) | ||
io-ts is an excellent library by gcanti. The API of io-ts heavily inspired the design of Zod. | ||
@@ -496,8 +515,3 @@ | ||
type C = t.TypeOf<typeof C>; | ||
/* | ||
returns { | ||
foo: string; | ||
bar?: number | undefined | ||
} | ||
*/ | ||
// returns { foo: string; bar?: number | undefined } | ||
``` | ||
@@ -523,4 +537,4 @@ | ||
### Joi ([https://github.com/hapijs/joi](https://github.com/hapijs/joi)) | ||
### Runtypes | ||
Doesn't support static type inference. 😕 | ||
[https://github.com/pelotom/runtypes](https://github.com/pelotom/runtypes) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94361
1376
527