swc/crates/swc_bundler/tests/.cache/untrusted/bf25849c02d079a33c668965b2f9202f925a6666.ts
2021-11-09 20:42:49 +09:00

72 lines
1.7 KiB
TypeScript

// Loaded from https://raw.githubusercontent.com/colinhacks/zod/654680afc2ede388e71e09104eac5a0088fe3207/deno/lib/types/enum.ts
import { ZodTypes } from "../ZodTypes.ts";
import { ZodType, ZodTypeDef } from "./base.ts";
// import { ZodUndefined } from './undefined';
// import { ZodNull } from './null';
// import { ZodUnion } from './union';
export type ArrayKeys = keyof any[];
export type Indices<T> = Exclude<keyof T, ArrayKeys>;
type EnumValues = [string, ...string[]];
type Values<T extends EnumValues> = {
[k in T[number]]: k;
};
export interface ZodEnumDef<T extends EnumValues = EnumValues>
extends ZodTypeDef {
t: ZodTypes.enum;
values: T;
}
export class ZodEnum<T extends [string, ...string[]]> extends ZodType<
T[number],
ZodEnumDef<T>
> {
// opt optional: () => ZodUnion<[this, ZodUndefined]> = () => ZodUnion.create([this, ZodUndefined.create()]);
// null nullable: () => ZodUnion<[this, ZodNull]> = () => ZodUnion.create([this, ZodNull.create()]);
toJSON = () => this._def;
get options() {
return this._def.values;
}
get enum(): Values<T> {
const enumValues: any = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues as any;
}
get Values(): Values<T> {
const enumValues: any = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues as any;
}
get Enum(): Values<T> {
const enumValues: any = {};
for (const val of this._def.values) {
enumValues[val] = val;
}
return enumValues as any;
}
static create = <U extends string, T extends [U, ...U[]]>(
values: T
): ZodEnum<T> => {
return new ZodEnum({
t: ZodTypes.enum,
values: values,
}) as any;
};
}