Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
@@ -1,2 +0,2 @@ | ||
import { ValidationError, Type, StringType, NumberType, LiteralType, ObjectType, ArrayType, UnionType, PartialType, TupleType, DateType, LazyType, UndefinedType, NullType, EnumType, BooleanType, UnknownType, Literal, ObjectShape, ObjectOptions, AnyType, UnionOptions, PartialOpts, IntersectionResult, DeepPartialShape, PartialShape, Eval, ToUnion, keySignature, StringTypes, OptionalType } from './types'; | ||
import { ValidationError, Type, StringType, NumberType, LiteralType, ObjectType, ArrayType, UnionType, PartialType, TupleType, DateType, LazyType, UndefinedType, NullType, EnumType, BooleanType, UnknownType, Literal, ObjectShape, ObjectOptions, AnyType, UnionOptions, PartialOpts, IntersectionResult, DeepPartialShape, PartialShape, Eval, ToUnion, keySignature, StringTypes, OptionalType, BigIntOptions, BigIntType } from './types'; | ||
export { ValidationError, Type, Infer, keySignature } from './types'; | ||
@@ -17,2 +17,3 @@ export declare const string: (opts?: Partial<{ | ||
}> | undefined) => NumberType; | ||
export declare const bigint: (opts?: BigIntOptions | undefined) => BigIntType; | ||
export declare const unknown: () => UnknownType; | ||
@@ -19,0 +20,0 @@ export declare const literal: <T extends Literal>(literal: T) => LiteralType<T>; |
@@ -11,2 +11,3 @@ "use strict"; | ||
exports.number = (opts) => new types_1.NumberType(opts); | ||
exports.bigint = (opts) => new types_1.BigIntType(opts); | ||
exports.unknown = () => new types_1.UnknownType(); | ||
@@ -13,0 +14,0 @@ exports.literal = (literal) => new types_1.LiteralType(literal); |
@@ -58,2 +58,14 @@ export declare abstract class Type<T> { | ||
} | ||
export declare type BigIntOptions = { | ||
min?: number | bigint; | ||
max?: number | bigint; | ||
}; | ||
export declare class BigIntType extends Type<bigint> { | ||
private opts; | ||
constructor(opts?: BigIntOptions); | ||
parse(value: unknown): bigint; | ||
and<K extends AnyType>(schema: K): IntersectionType<this, K>; | ||
min(x: number | bigint): BigIntType; | ||
max(x: number | bigint): BigIntType; | ||
} | ||
export declare class UndefinedType extends Type<undefined> { | ||
@@ -60,0 +72,0 @@ parse(value: unknown): undefined; |
@@ -130,3 +130,3 @@ "use strict"; | ||
this.opts = opts; | ||
this[coercionTypeSymbol] = false; | ||
this[coercionTypeSymbol] = !!opts.coerce; | ||
} | ||
@@ -166,2 +166,37 @@ parse(value) { | ||
exports.NumberType = NumberType; | ||
class BigIntType extends Type { | ||
constructor(opts = {}) { | ||
super(); | ||
this.opts = opts; | ||
this[coercionTypeSymbol] = true; | ||
} | ||
parse(value) { | ||
try { | ||
const int = BigInt(value); | ||
if (this.opts.min !== undefined && int < this.opts.min) { | ||
throw new ValidationError(`expected bigint to be greater than or equal to ${this.opts.min} but got ${int}`); | ||
} | ||
if (this.opts.max !== undefined && int > this.opts.max) { | ||
throw new ValidationError(`expected bigint to be less than or equal to ${this.opts.max} but got ${int}`); | ||
} | ||
return int; | ||
} | ||
catch (err) { | ||
if (err instanceof ValidationError) { | ||
throw err; | ||
} | ||
throw new ValidationError('expected type to be bigint interpretable - ' + err.message.toLowerCase()); | ||
} | ||
} | ||
and(schema) { | ||
return new IntersectionType(this, schema); | ||
} | ||
min(x) { | ||
return new BigIntType(Object.assign(Object.assign({}, this.opts), { min: x })); | ||
} | ||
max(x) { | ||
return new BigIntType(Object.assign(Object.assign({}, this.opts), { max: x })); | ||
} | ||
} | ||
exports.BigIntType = BigIntType; | ||
class UndefinedType extends Type { | ||
@@ -168,0 +203,0 @@ parse(value) { |
{ | ||
"name": "myzod", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./libs/index.js", |
@@ -62,9 +62,9 @@ # myzod | ||
- [Type<T>](#type<T>) | ||
- [parse](#type<T>.parse) | ||
- [try](#type<T>.try) | ||
- [and](#type<T>.and) | ||
- [or](#type<T>.or) | ||
- [optional](#type<T>.optional) | ||
- [nullable](#type<T>.nullable) | ||
- [Type<T>](#type) | ||
- [parse](#type.parse) | ||
- [try](#type.try) | ||
- [and](#type.and) | ||
- [or](#type.or) | ||
- [optional](#type.optional) | ||
- [nullable](#type.nullable) | ||
@@ -75,2 +75,3 @@ Primitive Types | ||
- [number](#number) | ||
- [bigint](#bigint) | ||
- [boolean](#boolean) | ||
@@ -104,7 +105,7 @@ - [undefined](#undefined) | ||
### Type<T> | ||
### Type. | ||
All myzod schemas extend the generic myzod.Type class, and as such inherit these methods: | ||
#### Type<T>.parse | ||
#### Type.parse | ||
@@ -117,3 +118,3 @@ Takes an unknown value, and returns it typed if passed validation. Otherwise throws a myzod.ValidationError | ||
#### Type<T>.try | ||
#### Type.try | ||
@@ -132,3 +133,3 @@ Takes an unknown value and returns a result which will either be the parsed value or an instance of ValidationError. | ||
##### Type<T>.and | ||
##### Type.and | ||
@@ -146,3 +147,3 @@ Shorthand for creating intersection types of two schemas. | ||
##### Type<T>.or | ||
##### Type.or | ||
@@ -157,3 +158,3 @@ Shorthand for creating union types of two schemas. | ||
##### Type<T>.optional | ||
##### Type.optional | ||
@@ -168,3 +169,3 @@ Returns a new schema which is a wrapped OptionalType of the current schema. | ||
##### Type<T>.nullable | ||
##### Type.nullable | ||
@@ -246,6 +247,34 @@ Returns a new schema which is a wrapped NullableType of the current schema. | ||
assert.equal(typeof value === 'number'); // succeeds | ||
assert.ok(typeof value === 'number'); // succeeds | ||
assert.equal(value, 42); // succeeds | ||
``` | ||
#### BigInt | ||
options: | ||
- min: `number` - min value for number | ||
- max: `number` - max value for number | ||
options can be passed as an option object or chained from schema. | ||
```typescript | ||
myzod.bigint({ min: 0, max: 10 }); | ||
// Same as: | ||
myzod.bigint().min(0).max(10); | ||
const integer = myzod.bigint(); | ||
type Integer = myzod.Infer<typeof integer>; // => bigint | ||
``` | ||
The bigint schema automatically coerces bigint interpretable numbers and strings into bigint values. | ||
```typescript | ||
const schema = myzod.bigint(); | ||
const value = schema.parse('42'); | ||
assert.ok(typeof value === 'bigint'); // succeeds | ||
assert.equal(value, 42n); // succeeds | ||
``` | ||
#### Boolean | ||
@@ -252,0 +281,0 @@ |
69085
1208
670