Comparing version 1.6.1 to 1.7.0
@@ -156,2 +156,3 @@ declare const typeErrSym: unique symbol; | ||
parse(value: unknown, opts?: any): Infer<T> | undefined; | ||
required(): T; | ||
and<K extends AnyType>(schema: K): IntersectionType<this, K>; | ||
@@ -161,3 +162,3 @@ } | ||
export declare class NullableType<T extends AnyType> extends Type<Infer<T> | null> implements Defaultable<Infer<T> | null> { | ||
private readonly schema; | ||
readonly schema: T; | ||
private readonly defaultValue?; | ||
@@ -167,2 +168,3 @@ constructor(schema: T); | ||
and<K extends AnyType>(schema: K): IntersectionType<this, K>; | ||
required(): T; | ||
default(value: Nullable<Infer<T>> | (() => Nullable<Infer<T>>)): any; | ||
@@ -169,0 +171,0 @@ } |
@@ -439,2 +439,5 @@ "use strict"; | ||
} | ||
required() { | ||
return clone(this.schema); | ||
} | ||
and(schema) { | ||
@@ -464,2 +467,5 @@ return new IntersectionType(this, schema); | ||
} | ||
required() { | ||
return clone(this.schema); | ||
} | ||
default(value) { | ||
@@ -466,0 +472,0 @@ return withDefault(this, value); |
{ | ||
"name": "myzod", | ||
"version": "1.6.1", | ||
"version": "1.7.0", | ||
"description": "", | ||
@@ -29,10 +29,10 @@ "main": "./libs/index.js", | ||
"devDependencies": { | ||
"@types/mocha": "^8.2.1", | ||
"@types/node": "^14.14.33", | ||
"@types/mocha": "^8.2.2", | ||
"@types/node": "^15.0.2", | ||
"benchmonkey": "^0.0.8", | ||
"mocha": "^8.3.1", | ||
"mocha": "^8.4.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.2.1", | ||
"prettier": "^2.3.0", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.2.3" | ||
"typescript": "^4.2.4" | ||
}, | ||
@@ -39,0 +39,0 @@ "nyc": { |
@@ -162,2 +162,9 @@ # myzod | ||
It is possible to unwrap an optional schema via a call to require: | ||
```typescript | ||
const optionalSchema = myzod.string().optional(); | ||
const schema = optionalSchema.require(); | ||
``` | ||
##### Type.nullable | ||
@@ -173,10 +180,18 @@ | ||
It is possible to unwrap an optional schema via a call to require: | ||
```typescript | ||
const optionalSchema = myzod.string().nullable(); | ||
const schema = optionalSchema.require(); | ||
``` | ||
##### Type.map | ||
Returns a new generic schema to the mapped type. Useful for transforming validated input into a new type on parse. | ||
```typescript | ||
const ObjectIDSchema = myzod | ||
.string() | ||
.withPredicate(ObjectId.isValid, 'must be an object ID') | ||
.map(value => new ObjectId(value)); | ||
.string() | ||
.withPredicate(ObjectId.isValid, 'must be an object ID') | ||
.map(value => new ObjectId(value)); | ||
@@ -454,7 +469,7 @@ // Infer<ObjectIDSchema> === ObjectId | ||
A new schema can be build via fluent syntax to allow for unknown keys | ||
```typescript | ||
const schema = z.object({ name: z.string(), age: z.number() }).allowUnknownKeys() | ||
const schema = z.object({ name: z.string(), age: z.number() }).allowUnknownKeys(); | ||
const value = schema.try({ name: 'myzod', age: 1, cool: true }); // value is { name: 'myzod', age: 1 } | ||
``` | ||
@@ -545,7 +560,5 @@ | ||
```typescript | ||
const personSchema = myzod | ||
.object({ name: myzod.string(), lastName: myzod.string() }) | ||
.collectErrors(); | ||
const personSchema = myzod.object({ name: myzod.string(), lastName: myzod.string() }).collectErrors(); | ||
personSchema.parse({ name: 1, lastName: 2 }) | ||
personSchema.parse({ name: 1, lastName: 2 }); | ||
@@ -556,3 +569,3 @@ // throws an ValidationError with message: | ||
error parsing object at path: "lastName" - expected type to be string but got number | ||
` | ||
`; | ||
@@ -562,3 +575,3 @@ err.collectedErrors = { | ||
lastName: ValidationError, | ||
} | ||
}; | ||
``` | ||
@@ -565,0 +578,0 @@ |
106701
1864
909