@decs/typeschema
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { Type } from 'arktype'; | ||
export type AdapterSchema<T> = Type<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { Problems, Type } from 'arktype'; | ||
interface ArkTypeResolver extends TypeSchemaResolver { | ||
base: Type<this['type']>; | ||
input: this['schema'] extends Type ? this['schema']['inferIn'] : never; | ||
output: this['schema'] extends Type ? this['schema']['infer'] : never; | ||
error: Problems; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
arktype: ArkTypeResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,24 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const ArkType = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('arktype')))); | ||
if (ArkType == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const ArkType = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('arktype')))); | ||
if (ArkType == null) { | ||
return null; | ||
} | ||
if (!('infer' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), (schema) => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = schema(data); | ||
if (result.problems == null) { | ||
return { data: result.data }; | ||
} | ||
if (!('infer' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.assert(data); }), | ||
issues: [...result.problems].map(({ message, path }) => new schema_1.ValidationIssue(message, path)), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
}), | ||
})); |
@@ -1,3 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
export type AdapterSchema<T> = (data: unknown) => T; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
type FunctionSchema<T = unknown> = (data: unknown) => T; | ||
interface FunctionResolver extends TypeSchemaResolver { | ||
base: FunctionSchema<this['type']>; | ||
input: this['schema'] extends FunctionSchema ? ReturnType<this['schema']> : never; | ||
output: this['schema'] extends FunctionSchema ? ReturnType<this['schema']> : never; | ||
error: unknown; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
function: FunctionResolver; | ||
} | ||
} | ||
export {}; |
@@ -12,14 +12,21 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof schema !== 'function' || 'assert' in schema) { | ||
return null; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (typeof schema !== 'function' || 'assert' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
return { data: schema(data) }; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema(data); }), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
catch (error) { | ||
if (error instanceof Error) { | ||
return { issues: [new schema_1.ValidationIssue(error.message)] }; | ||
} | ||
throw error; | ||
} | ||
}), | ||
})); |
@@ -1,14 +0,8 @@ | ||
import type { IfDefined } from '../utils'; | ||
import * as ArkTypeAdapter from './arktype'; | ||
import * as FunctionAdapter from './function'; | ||
import * as JoiAdapter from './joi'; | ||
import * as RuntypesAdapter from './runtypes'; | ||
import * as SuperstructAdapter from './superstruct'; | ||
import * as TypeboxAdapter from './typebox'; | ||
import * as YupAdapter from './yup'; | ||
import * as ZodAdapter from './zod'; | ||
export type Schema<T> = IfDefined<ArkTypeAdapter.AdapterSchema<T>> | IfDefined<FunctionAdapter.AdapterSchema<T>> | IfDefined<JoiAdapter.AdapterSchema<T>> | IfDefined<RuntypesAdapter.AdapterSchema<T>> | IfDefined<SuperstructAdapter.AdapterSchema<T>> | IfDefined<TypeboxAdapter.AdapterSchema<T>> | IfDefined<YupAdapter.AdapterSchema<T>> | IfDefined<ZodAdapter.AdapterSchema<T>>; | ||
export type WrappedSchema<T> = { | ||
assert(data: unknown): Promise<T>; | ||
}; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T>>; | ||
import './arktype'; | ||
import './function'; | ||
import './joi'; | ||
import './runtypes'; | ||
import './superstruct'; | ||
import './typebox'; | ||
import './yup'; | ||
import './zod'; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const ArkTypeAdapter = __importStar(require("./arktype")); | ||
const FunctionAdapter = __importStar(require("./function")); | ||
const JoiAdapter = __importStar(require("./joi")); | ||
const RuntypesAdapter = __importStar(require("./runtypes")); | ||
const SuperstructAdapter = __importStar(require("./superstruct")); | ||
const TypeboxAdapter = __importStar(require("./typebox")); | ||
const YupAdapter = __importStar(require("./yup")); | ||
const ZodAdapter = __importStar(require("./zod")); | ||
const adapters = [ | ||
ArkTypeAdapter, | ||
FunctionAdapter, | ||
JoiAdapter, | ||
RuntypesAdapter, | ||
SuperstructAdapter, | ||
TypeboxAdapter, | ||
YupAdapter, | ||
ZodAdapter, | ||
]; | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const results = (yield Promise.all(adapters.map((adapter) => __awaiter(this, void 0, void 0, function* () { return adapter.wrap(schema); })))).filter(Boolean); | ||
if (results.length === 0) { | ||
throw new Error('Missing adapters for schema: ' + schema); | ||
} | ||
if (results.length > 1) { | ||
throw new Error('Conflicting adapters for schema: ' + schema); | ||
} | ||
return results[0]; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
require("./arktype"); | ||
require("./function"); | ||
require("./joi"); | ||
require("./runtypes"); | ||
require("./superstruct"); | ||
require("./typebox"); | ||
require("./yup"); | ||
require("./zod"); |
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { AnySchema } from 'joi'; | ||
export type AdapterSchema<T> = AnySchema<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { AnySchema, ValidationError } from 'joi'; | ||
interface JoiResolver extends TypeSchemaResolver { | ||
base: AnySchema<this['type']>; | ||
input: this['schema'] extends AnySchema<infer T> ? T : never; | ||
output: this['schema'] extends AnySchema<infer T> ? T : never; | ||
error: ValidationError; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
joi: JoiResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,24 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Joi = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('joi')))); | ||
if (Joi == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const Joi = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('joi')))); | ||
if (Joi == null) { | ||
return null; | ||
} | ||
if (!('_flags' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = schema.validate(data); | ||
if (result.error == null) { | ||
return { data: result.value }; | ||
} | ||
if (!('_flags' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.validateAsync(data); }), | ||
issues: result.error.details.map(({ message, path }) => new schema_1.ValidationIssue(message, path)), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
}), | ||
})); |
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { Runtype } from 'runtypes'; | ||
export type AdapterSchema<T> = Runtype<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { Failure, Runtype, Static } from 'runtypes'; | ||
interface RuntypesResolver extends TypeSchemaResolver { | ||
base: Runtype<this['type']>; | ||
input: this['schema'] extends Runtype ? Static<this['schema']> : never; | ||
output: this['schema'] extends Runtype ? Static<this['schema']> : never; | ||
error: Failure; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
runtypes: RuntypesResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,22 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Runtypes = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('runtypes')))); | ||
if (Runtypes == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const Runtypes = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('runtypes')))); | ||
if (Runtypes == null) { | ||
return null; | ||
} | ||
if (!('reflect' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = schema.validate(data); | ||
if (result.success) { | ||
return { data: result.value }; | ||
} | ||
if (!('reflect' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.check(data); }), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
return { issues: [new schema_1.ValidationIssue(result.message)] }; | ||
}), | ||
})); |
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { Struct } from 'superstruct'; | ||
export type AdapterSchema<T> = Struct<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { Infer, Struct, StructError } from 'superstruct'; | ||
interface SuperstructResolver extends TypeSchemaResolver { | ||
base: Struct<this['type']>; | ||
input: this['schema'] extends Struct ? Infer<this['schema']> : never; | ||
output: this['schema'] extends Struct ? Infer<this['schema']> : never; | ||
error: StructError; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
superstruct: SuperstructResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,23 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Superstruct = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('superstruct')))); | ||
if (Superstruct == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const Superstruct = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('superstruct')))); | ||
if (Superstruct == null) { | ||
return null; | ||
} | ||
if (!('refiner' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = schema.validate(data); | ||
if (result[0] == null) { | ||
return { data: result[1] }; | ||
} | ||
if (!('refiner' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.create(data); }), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
const { message, path } = result[0]; | ||
return { issues: [new schema_1.ValidationIssue(message, path)] }; | ||
}), | ||
})); |
@@ -1,6 +0,18 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { TSchema } from '@sinclair/typebox'; | ||
export type AdapterSchema<T> = TSchema & { | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { Static, TSchema } from '@sinclair/typebox'; | ||
import type { TypeCheck } from '@sinclair/typebox/compiler'; | ||
type TypeBoxSchema<T> = TSchema & { | ||
static: T; | ||
}; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
interface TypeBoxResolver extends TypeSchemaResolver { | ||
base: TypeBoxSchema<this['type']>; | ||
input: this['schema'] extends TSchema ? Static<this['schema']> : never; | ||
output: this['schema'] extends TSchema ? Static<this['schema']> : never; | ||
error: ReturnType<TypeCheck<TSchema>['Errors']>; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
typebox: TypeBoxResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,26 +35,25 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Typebox = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('@sinclair/typebox')))); | ||
if (Typebox == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const TypeBox = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('@sinclair/typebox')))); | ||
if (TypeBox == null) { | ||
return null; | ||
} | ||
if (!(TypeBox.Kind in schema)) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const { TypeCompiler } = yield Promise.resolve().then(() => __importStar(require('@sinclair/typebox/compiler'))); | ||
const result = TypeCompiler.Compile(schema); | ||
if (result.Check(data)) { | ||
return { data }; | ||
} | ||
if (!(Typebox.Kind in schema)) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { | ||
const { TypeCompiler } = yield Promise.resolve().then(() => __importStar(require('@sinclair/typebox/compiler'))); | ||
const result = TypeCompiler.Compile(schema); | ||
if (result.Check(data)) { | ||
return data; | ||
} | ||
throw new Error(JSON.stringify([...result.Errors(data)])); | ||
}), | ||
issues: [...result.Errors(data)].map(({ message, path }) => new schema_1.ValidationIssue(message, [path])), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
}), | ||
})); |
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { Schema as YupSchema } from 'yup'; | ||
export type AdapterSchema<T> = YupSchema<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { InferType, Schema, ValidationError } from 'yup'; | ||
interface YupResolver extends TypeSchemaResolver { | ||
base: Schema<this['type']>; | ||
input: this['schema'] extends Schema ? InferType<this['schema']> : never; | ||
output: this['schema'] extends Schema ? InferType<this['schema']> : never; | ||
error: ValidationError; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
yup: YupResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,32 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Yup = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('yup')))); | ||
if (Yup == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const Yup = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('yup')))); | ||
if (Yup == null) { | ||
return null; | ||
} | ||
if (!('__isYupSchema__' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
return { data: yield schema.validate(data, { strict: true }) }; | ||
} | ||
if (!('__isYupSchema__' in schema) || 'static' in schema) { | ||
return null; | ||
catch (error) { | ||
const Yup = yield Promise.resolve().then(() => __importStar(require('yup'))); | ||
if (error instanceof Yup.ValidationError) { | ||
const { message, path } = error; | ||
return { | ||
issues: [ | ||
new schema_1.ValidationIssue(message, path != null && path !== '' ? [path] : undefined), | ||
], | ||
}; | ||
} | ||
throw error; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.validate(data, { strict: true }); }), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
}), | ||
})); |
@@ -1,4 +0,14 @@ | ||
import type { Schema, WrappedSchema } from '.'; | ||
import type { ZodSchema } from 'zod'; | ||
export type AdapterSchema<T> = ZodSchema<T>; | ||
export declare function wrap<T>(schema: Schema<T>): Promise<WrappedSchema<T> | null>; | ||
import type { TypeSchemaResolver } from '../resolver'; | ||
import type { input, output, ZodError, ZodSchema } from 'zod'; | ||
interface ZodResolver extends TypeSchemaResolver { | ||
base: ZodSchema<this['type']>; | ||
input: this['schema'] extends ZodSchema ? input<this['schema']> : never; | ||
output: this['schema'] extends ZodSchema ? output<this['schema']> : never; | ||
error: ZodError; | ||
} | ||
declare global { | ||
export interface TypeSchemaRegistry { | ||
zod: ZodResolver; | ||
} | ||
} | ||
export {}; |
@@ -35,19 +35,24 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.wrap = void 0; | ||
const registry_1 = require("../registry"); | ||
const schema_1 = require("../schema"); | ||
const utils_1 = require("../utils"); | ||
function wrap(schema) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const Zod = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('zod')))); | ||
if (Zod == null) { | ||
return null; | ||
(0, registry_1.register)((schema) => __awaiter(void 0, void 0, void 0, function* () { | ||
const Zod = yield (0, utils_1.maybe)(() => Promise.resolve().then(() => __importStar(require('zod')))); | ||
if (Zod == null) { | ||
return null; | ||
} | ||
if (!('_def' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
return schema; | ||
}), schema => ({ | ||
validate: (data) => __awaiter(void 0, void 0, void 0, function* () { | ||
const result = yield schema.safeParseAsync(data); | ||
if (result.success) { | ||
return { data: result.data }; | ||
} | ||
if (!('_def' in schema) || 'static' in schema) { | ||
return null; | ||
} | ||
schema; | ||
return { | ||
assert: (data) => __awaiter(this, void 0, void 0, function* () { return schema.parse(data); }), | ||
issues: result.error.issues.map(({ message, path }) => new schema_1.ValidationIssue(message, path)), | ||
}; | ||
}); | ||
} | ||
exports.wrap = wrap; | ||
}), | ||
})); |
@@ -1,4 +0,11 @@ | ||
import type { Schema } from './adapters'; | ||
export type { Schema } from './adapters'; | ||
import type { Schema } from './registry'; | ||
import type { ValidationIssue } from './schema'; | ||
export type { Schema } from './registry'; | ||
export type { ValidationIssue } from './schema'; | ||
export type Infer<TSchema> = TSchema extends Schema<infer T> ? T : never; | ||
export declare function validate<T>(schema: Schema<T>, data: unknown): Promise<{ | ||
data: T; | ||
} | { | ||
issues: Array<ValidationIssue>; | ||
}>; | ||
export declare function assert<T>(schema: Schema<T>, data: unknown): Promise<T>; |
@@ -12,9 +12,19 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assert = void 0; | ||
const adapters_1 = require("./adapters"); | ||
exports.assert = exports.validate = void 0; | ||
const wrap_1 = require("./wrap"); | ||
function validate(schema, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield (0, wrap_1.wrap)(schema)).validate(data); | ||
}); | ||
} | ||
exports.validate = validate; | ||
function assert(schema, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (yield (0, adapters_1.wrap)(schema)).assert(data); | ||
const result = yield validate(schema, data); | ||
if ('issues' in result) { | ||
throw result.issues[0]; | ||
} | ||
return result.data; | ||
}); | ||
} | ||
exports.assert = assert; |
{ | ||
"name": "@decs/typeschema", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Universal adapter for schema validation", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -49,3 +49,3 @@ <div id="header"> | ||
import type {Infer, Schema} from '@decs/typeschema'; | ||
import {assert} from '@decs/typeschema'; | ||
import {assert, validate} from '@decs/typeschema'; | ||
@@ -60,5 +60,9 @@ // Use your favorite validation library, e.g. `zod`, `arktype`, `typia` | ||
// Returns the validated data or throws an exception | ||
// Returns the validated data or throws a `ValidationIssue` | ||
await assert(schema, '123'); // '123' | ||
await assert(schema, 123); // `Error` | ||
await assert(schema, 123); // Throws `ValidationIssue` | ||
// Returns the validated data or a list of `ValidationIssue`s | ||
await validate(schema, '123'); // {data: '123'} | ||
await validate(schema, 123); // {issues: [`ValidationIssue`]} | ||
``` | ||
@@ -70,8 +74,10 @@ | ||
- `Schema<T>`<br />Generic interface for schemas | ||
- `Infer<T as Schema<unknown>>`<br />Extracts the equivalent TypeScript type of a schema | ||
- `Schema<T>`<br />Generic interface for schemas<br />An union of the schema types of all supported validation libraries | ||
- `ValidationIssue`<br />Generic interface for validation issues<br />Includes a `message: string` and an optional `path?: Array<string | number | symbol>` | ||
- `Infer<TSchema>`<br />Extracts the equivalent TypeScript type of a schema | ||
#### Functions | ||
- `assert<T>(schema: Schema<T>, data: unknown): Promise<T>`<br />Returns the validated data or throws an exception | ||
- `assert<T>(schema: Schema<T>, data: unknown): Promise<T>`<br />Returns the validated data or throws a `ValidationIssue` | ||
- `validate<T>(schema: Schema<T>, data: unknown): Promise<{data: T} | {issues: Array<ValidationIssue>}>`<br />Returns the validated data or a list of `ValidationIssue`s | ||
@@ -103,4 +109,7 @@ ## Coverage | ||
await assert(assertString, '123'); // Returns '123' | ||
await assert(assertString, 123); // Throws an exception | ||
await assert(assertString, '123'); // '123' | ||
await assert(assertString, 123); // Throws `ValidationIssue` | ||
await validate(assertString, '123'); // {data: '123'} | ||
await validate(assertString, 123); // {issues: [`ValidationIssue`]} | ||
``` | ||
@@ -107,0 +116,0 @@ |
41691
33
735
116