Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to enable type printing #1209

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class Struct<T = unknown, S = unknown> {
readonly TYPE!: T
type: string
schema: S
extend?: any
coercer: (value: unknown, context: Context) => unknown
validator: (value: unknown, context: Context) => Iterable<Failure>
refiner: (value: T, context: Context) => Iterable<Failure>
Expand All @@ -22,6 +23,7 @@ export class Struct<T = unknown, S = unknown> {
constructor(props: {
type: string
schema: S
extend?: any
coercer?: Coercer
validator?: Validator
refiner?: Refiner<T>
Expand All @@ -31,12 +33,14 @@ export class Struct<T = unknown, S = unknown> {
type,
schema,
validator,
extend,
refiner,
coercer = (value: unknown) => value,
entries = function* () {},
} = props

this.type = type
this.extend = extend
this.schema = schema
this.entries = entries
this.coercer = coercer
Expand Down
6 changes: 5 additions & 1 deletion src/structs/coercions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function defaulted<T, S>(
strict?: boolean
} = {}
): Struct<T, S> {
return coerce(struct, unknown(), (x) => {
const result = coerce(struct, unknown(), (x) => {
const f = typeof fallback === 'function' ? fallback() : fallback

if (x === undefined) {
Expand All @@ -67,6 +67,10 @@ export function defaulted<T, S>(

return x
})

result.extend = ["def", fallback];

return result;
}

/**
Expand Down
44 changes: 25 additions & 19 deletions src/structs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,16 @@ export function func(): Struct<Function, null> {

export function instance<T extends { new (...args: any): any }>(
Class: T
): Struct<InstanceType<T>, null> {
return define('instance', (value) => {
return (
value instanceof Class ||
`Expected a \`${Class.name}\` instance, but received: ${print(value)}`
)
): Struct<InstanceType<T>, T> {
return new Struct({
type: 'instance',
schema: Class,
validator: (value) => {
return (
value instanceof Class ||
`Expected a \`${Class.name}\` instance, but received: ${print(value)}`
)
}
})
}

Expand All @@ -170,10 +174,10 @@ export function integer(): Struct<number, null> {

export function intersection<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, null> {
): Struct<Infer<A> & UnionToIntersection<InferStructTuple<B>[number]>, [A, ...B]> {
return new Struct({
type: 'intersection',
schema: null,
schema: Structs,
*entries(value, ctx) {
for (const S of Structs) {
yield* S.entries(value, ctx)
Expand Down Expand Up @@ -225,11 +229,11 @@ export function map(): Struct<Map<unknown, unknown>, null>
export function map<K, V>(
Key: Struct<K>,
Value: Struct<V>
): Struct<Map<K, V>, null>
): Struct<Map<K, V>, [Struct<K>, Struct<V>]>
export function map<K, V>(Key?: Struct<K>, Value?: Struct<V>): any {
return new Struct({
type: 'map',
schema: null,
schema: [Key, Value],
*entries(value) {
if (Key && Value && value instanceof Map) {
for (const [k, v] of value.entries()) {
Expand Down Expand Up @@ -265,6 +269,7 @@ export function never(): Struct<never, null> {
export function nullable<T, S>(struct: Struct<T, S>): Struct<T | null, S> {
return new Struct({
...struct,
extend: ["null"],
validator: (value, ctx) => value === null || struct.validator(value, ctx),
refiner: (value, ctx) => value === null || struct.refiner(value, ctx),
})
Expand Down Expand Up @@ -332,6 +337,7 @@ export function object<S extends ObjectSchema>(schema?: S): any {
export function optional<T, S>(struct: Struct<T, S>): Struct<T | undefined, S> {
return new Struct({
...struct,
extend: ["?"],
validator: (value, ctx) =>
value === undefined || struct.validator(value, ctx),
refiner: (value, ctx) => value === undefined || struct.refiner(value, ctx),
Expand All @@ -348,10 +354,10 @@ export function optional<T, S>(struct: Struct<T, S>): Struct<T | undefined, S> {
export function record<K extends string, V>(
Key: Struct<K>,
Value: Struct<V>
): Struct<Record<K, V>, null> {
): Struct<Record<K, V>, [Struct<K>, Struct<V>]> {
return new Struct({
type: 'record',
schema: null,
schema: [Key, Value],
*entries(value) {
if (isObject(value)) {
for (const k in value) {
Expand Down Expand Up @@ -388,11 +394,11 @@ export function regexp(): Struct<RegExp, null> {
*/

export function set(): Struct<Set<unknown>, null>
export function set<T>(Element: Struct<T>): Struct<Set<T>, null>
export function set<T>(Element: Struct<T>): Struct<Set<T>, Struct<T>>
export function set<T>(Element?: Struct<T>): any {
return new Struct({
type: 'set',
schema: null,
schema: Element,
*entries(value) {
if (Element && value instanceof Set) {
for (const v of value) {
Expand Down Expand Up @@ -432,12 +438,12 @@ export function string(): Struct<string, null> {

export function tuple<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<[Infer<A>, ...InferStructTuple<B>], null> {
): Struct<[Infer<A>, ...InferStructTuple<B>], [A, ...B]> {
const Never = never()

return new Struct({
type: 'tuple',
schema: null,
schema: Structs,
*entries(value) {
if (Array.isArray(value)) {
const length = Math.max(Structs.length, value.length)
Expand Down Expand Up @@ -494,11 +500,11 @@ export function type<S extends ObjectSchema>(

export function union<A extends AnyStruct, B extends AnyStruct[]>(
Structs: [A, ...B]
): Struct<Infer<A> | InferStructTuple<B>[number], null> {
): Struct<Infer<A> | InferStructTuple<B>[number], [A, ...B]> {
const description = Structs.map((s) => s.type).join(' | ')
return new Struct({
type: 'union',
schema: null,
schema: Structs,
coercer(value) {
for (const S of Structs) {
const [error, coerced] = S.validate(value, { coerce: true })
Expand Down Expand Up @@ -543,4 +549,4 @@ export function union<A extends AnyStruct, B extends AnyStruct[]>(

export function unknown(): Struct<unknown, null> {
return define('unknown', () => true)
}
}
14 changes: 8 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export type If<B extends Boolean, Then, Else> = B extends true ? Then : Else

export type StructSchema<T> = [T] extends [string | undefined | null]
? [T] extends [IsMatch<T, string | undefined | null>]
? null
? any
: [T] extends [IsUnion<T>]
? EnumSchema<T>
: T
Expand All @@ -354,6 +354,10 @@ export type StructSchema<T> = [T] extends [string | undefined | null]
? [T] extends [IsExactMatch<T, boolean>]
? null
: T
: T extends Map<infer K, infer V>
? [Struct<K>, Struct<V>]
: T extends Set<infer V>
? Struct<V>
: T extends
| bigint
| symbol
Expand All @@ -363,21 +367,19 @@ export type StructSchema<T> = [T] extends [string | undefined | null]
| Date
| Error
| RegExp
| Map<any, any>
| WeakMap<any, any>
| Set<any>
| WeakSet<any>
| Promise<any>
? null
: T extends Array<infer E>
? T extends IsTuple<T>
? null
? any
: Struct<E>
: T extends object
? T extends IsRecord<T>
? null
? [any, any]
: { [K in keyof T]: Describe<T[K]> }
: null
: any

/**
* A schema for tuple structs.
Expand Down
Loading