Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@esmj/schema

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@esmj/schema - npm Package Compare versions

Comparing version
0.5.1
to
0.6.0
+13
dist/array.d.mts
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
declare module './index.ts' {
interface ArraySchemaInterface<T extends SchemaType> {
min(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
max(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
length(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
nonEmpty(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
unique(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
sort(): ArraySchemaInterface<T>;
reverse(): ArraySchemaInterface<T>;
}
}
import './string.mjs';
import './number.mjs';
import './array.mjs';
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
type ErrorStructure = {
message: string;
cause?: {
key?: string;
};
};
type Valid<Output> = {
success: true;
data: Output;
};
type Invalid = {
success: false;
error: ErrorStructure;
errors?: ErrorStructure[];
};
type InternalParseOutput<Output> = Valid<Output> | Invalid;
type ValidationMethod<Input, Output> = (value: Input | Partial<Input>) => boolean | InternalParseOutput<Output>;
interface ParseOptions {
abortEarly?: boolean;
}
interface SchemaInterface<Input, Output> {
_getName(): string;
_getType(): string;
_getDescription(): string;
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
parse(value: Input | Partial<Input>, options?: ParseOptions): Output;
safeParse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
nullable(): SchemaInterface<Input, Output | null>;
nullish(): SchemaInterface<Input, Output | undefined | null>;
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
}
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
}
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
}
interface StringSchemaInterface extends SchemaInterface<string, string> {
}
interface NumberSchemaInterface extends SchemaInterface<number, number> {
}
interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
}
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
}
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
}
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
[Property in keyof T]: ReturnType<T[Property]['parse']>;
}, {
[Property in keyof T]: ReturnType<T[Property]['parse']>;
}> {
}
type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
type ErrorMessage = string | ((value: unknown) => string);
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
message: ErrorMessage;
type: string;
}) => SchemaType;
interface CreateSchemaInterfaceOptions {
name?: string;
type?: string;
message?: ErrorMessage;
}
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
declare const s: {
/**
* Creates an object schema with validated fields.
*
* @param definition - Object containing field schemas
* @param options - Optional configuration (name, message)
* @returns Object schema interface
*
* @example
* ```typescript
* const userSchema = s.object({
* name: s.string(),
* age: s.number()
* });
* ```
*/
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
/**
* Creates a string schema.
*
* @param options - Optional configuration (name, message)
* @returns String schema interface
*
* @example
* ```typescript
* const nameSchema = s.string();
* const result = nameSchema.parse('John'); // 'John'
* ```
*/
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
/**
* Creates a number schema.
*
* @param options - Optional configuration (name, message)
* @returns Number schema interface
*
* @example
* ```typescript
* const ageSchema = s.number();
* const result = ageSchema.parse(25); // 25
* ```
*/
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
/**
* Creates a boolean schema.
*
* @param options - Optional configuration (name, message)
* @returns Boolean schema interface
*
* @example
* ```typescript
* const isActiveSchema = s.boolean();
* const result = isActiveSchema.parse(true); // true
* ```
*/
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
/**
* Creates a date schema.
*
* @param options - Optional configuration (name, message)
* @returns Date schema interface
*
* @example
* ```typescript
* const birthdateSchema = s.date();
* const result = birthdateSchema.parse(new Date()); // Date object
* ```
*/
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
/**
* Creates an enum schema with predefined values.
*
* @param definition - Array of allowed string values
* @param options - Optional configuration (name, message)
* @returns Enum schema interface
*
* @example
* ```typescript
* const roleSchema = s.enum(['admin', 'user', 'guest']);
* const result = roleSchema.parse('admin'); // 'admin'
* ```
*/
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
/**
* Creates an array schema with element validation.
*
* @param definition - Schema for array elements
* @param options - Optional configuration (name, message)
* @returns Array schema interface
*
* @example
* ```typescript
* const tagsSchema = s.array(s.string());
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
* ```
*/
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
/**
* Creates a schema that accepts any value without validation.
*
* @returns Schema interface that accepts any value
*
* @example
* ```typescript
* const anySchema = s.any();
* const result = anySchema.parse({ anything: true }); // { anything: true }
* ```
*/
any(): any;
/**
* Preprocesses a value before passing it to a schema for validation.
*
* @param callback - Function to transform the value before validation
* @param schema - Schema to validate the transformed value
* @returns Modified schema with preprocessing
*
* @example
* ```typescript
* const schema = s.preprocess(
* (val) => String(val).trim(),
* s.string().min(3)
* );
* const result = schema.parse(' hello '); // 'hello'
* ```
*/
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
/**
* Creates a union schema that validates against multiple schemas.
* The value must match at least one of the provided schemas.
*
* @param definitions - Array of schemas to validate against
* @param options - Optional configuration (name, message)
* @returns Union schema interface
*
* @example
* ```typescript
* const idSchema = s.union([s.string(), s.number()]);
* const result1 = idSchema.parse('abc'); // 'abc'
* const result2 = idSchema.parse(123); // 123
* ```
*/
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
};
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: string, action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
/**
* Extends the schema system with custom validation methods.
* Used to add new methods to schema interfaces like StringSchemaInterface, NumberSchemaInterface, etc.
*
* @param callback - Function that receives schema, validation, and options, and can add new methods
*
* @example
* ```typescript
* import { extend, type StringSchemaInterface } from '@esmj/schema';
*
* // Extend StringSchemaInterface with email validation
* declare module '@esmj/schema' {
* interface StringSchemaInterface {
* email(): StringSchemaInterface;
* }
* }
*
* extend((schema, _, options) => {
* if (options?.type === 'string') {
* schema.email = function() {
* return this.refine(
* (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val),
* { message: 'Invalid email address' }
* );
* };
* }
* return schema;
* });
*
* // Now you can use the email() method
* const emailSchema = s.string().email();
* ```
*/
declare function extend(callback: ExtenderType): void;
/**
* Type helper to infer the output type of a schema.
* Extracts the TypeScript type that a schema will produce after parsing.
*
* @template T - The schema type to infer from
*
* @example
* ```typescript
* const userSchema = s.object({
* name: s.string(),
* age: s.number(),
* email: s.string().optional()
* });
*
* type User = Infer<typeof userSchema>;
* // type User = {
* // name: string;
* // age: number;
* // email?: string;
* // }
* ```
*/
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
declare module './index.ts' {
interface NumberSchemaInterface {
min(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
max(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
positive(options?: SchemaInterfaceOptions): NumberSchemaInterface;
negative(options?: SchemaInterfaceOptions): NumberSchemaInterface;
int(options?: SchemaInterfaceOptions): NumberSchemaInterface;
float(options?: SchemaInterfaceOptions): NumberSchemaInterface;
multipleOf(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
finite(options?: SchemaInterfaceOptions): NumberSchemaInterface;
}
}
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
declare module './index.ts' {
interface StringSchemaInterface {
min(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
max(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
length(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
nonEmpty(options?: SchemaInterfaceOptions): StringSchemaInterface;
startsWith(prefix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
endsWith(suffix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
includes(substring: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
trim(): StringSchemaInterface;
toLowerCase(): StringSchemaInterface;
toUpperCase(): StringSchemaInterface;
padStart(length: number, fillChar?: string): StringSchemaInterface;
padEnd(length: number, fillChar?: string): StringSchemaInterface;
replace(search: string | RegExp, replace: string): StringSchemaInterface;
}
}
+1
-2
{
"name": "@esmj/schema",
"version": "0.5.1",
"version": "0.6.0",
"description": "Tiny extendable package for schema validation.",
"type": "module",
"keywords": [

@@ -7,0 +6,0 @@ "schema",

@@ -120,3 +120,3 @@ # Schema

|-------------------|---------------------------------|
| `@esmj/schema` | `~1,4 KB` |
| `@esmj/schema` | `~1.5 KB` |
| Superstruct | ~3.2 KB |

@@ -126,6 +126,6 @@ | @sinclair/typebox | ~11.7 KB |

| Zod@3 | ~13 KB |
| @zod/mini | ~20,5 KB |
| Joi | ~40,4 KB |
| Zod@4 | ~40,8 KB |
| ArkType | ~41,8 KB |
| @zod/mini | ~20.5 KB |
| Joi | ~40.4 KB |
| Zod@4 | ~40.8 KB |
| ArkType | ~41.8 KB |
| Effect/Schema | ~115.5 KB |

@@ -234,3 +234,3 @@

```typescript
// Minimal version (core only, ~1.4 KB)
// Minimal version (core only, ~1.5 KB)
import { s } from '@esmj/schema';

@@ -258,3 +258,3 @@

- **Core only** (`@esmj/schema`): ~1.4 KB gzipped
- **Core only** (`@esmj/schema`): ~1.5 KB gzipped
- **String extensions** (`@esmj/schema/string`): +~0.8 KB

@@ -261,0 +261,0 @@ - **Number extensions** (`@esmj/schema/number`): +~0.6 KB

export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
declare module './index.ts' {
interface ArraySchemaInterface<T extends SchemaType> {
min(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
max(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
length(length: number, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
nonEmpty(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
unique(options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
sort(): ArraySchemaInterface<T>;
reverse(): ArraySchemaInterface<T>;
}
}
import './string.cjs';
import './number.cjs';
import './array.cjs';
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
type ErrorStructure = {
message: string;
cause?: {
key?: string;
};
};
type Valid<Output> = {
success: true;
data: Output;
};
type Invalid = {
success: false;
error: ErrorStructure;
errors?: ErrorStructure[];
};
type InternalParseOutput<Output> = Valid<Output> | Invalid;
type ValidationMethod<Input, Output> = (value: Input | Partial<Input>) => boolean | InternalParseOutput<Output>;
interface ParseOptions {
abortEarly?: boolean;
}
interface SchemaInterface<Input, Output> {
_getName(): string;
_getType(): string;
_getDescription(): string;
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
parse(value: Input | Partial<Input>, options?: ParseOptions): Output;
safeParse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
nullable(): SchemaInterface<Input, Output | null>;
nullish(): SchemaInterface<Input, Output | undefined | null>;
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
refine(validation: ValidationMethod<Input, Output>, options?: CreateSchemaInterfaceOptions): SchemaInterface<Input, Output>;
}
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
}
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
}
interface StringSchemaInterface extends SchemaInterface<string, string> {
}
interface NumberSchemaInterface extends SchemaInterface<number, number> {
}
interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
}
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
}
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
}
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
[Property in keyof T]: ReturnType<T[Property]['parse']>;
}, {
[Property in keyof T]: ReturnType<T[Property]['parse']>;
}> {
}
type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
type ErrorMessage = string | ((value: unknown) => string);
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
message: ErrorMessage;
type: string;
}) => SchemaType;
interface CreateSchemaInterfaceOptions {
name?: string;
type?: string;
message?: ErrorMessage;
}
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
declare const s: {
/**
* Creates an object schema with validated fields.
*
* @param definition - Object containing field schemas
* @param options - Optional configuration (name, message)
* @returns Object schema interface
*
* @example
* ```typescript
* const userSchema = s.object({
* name: s.string(),
* age: s.number()
* });
* ```
*/
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
/**
* Creates a string schema.
*
* @param options - Optional configuration (name, message)
* @returns String schema interface
*
* @example
* ```typescript
* const nameSchema = s.string();
* const result = nameSchema.parse('John'); // 'John'
* ```
*/
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
/**
* Creates a number schema.
*
* @param options - Optional configuration (name, message)
* @returns Number schema interface
*
* @example
* ```typescript
* const ageSchema = s.number();
* const result = ageSchema.parse(25); // 25
* ```
*/
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
/**
* Creates a boolean schema.
*
* @param options - Optional configuration (name, message)
* @returns Boolean schema interface
*
* @example
* ```typescript
* const isActiveSchema = s.boolean();
* const result = isActiveSchema.parse(true); // true
* ```
*/
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
/**
* Creates a date schema.
*
* @param options - Optional configuration (name, message)
* @returns Date schema interface
*
* @example
* ```typescript
* const birthdateSchema = s.date();
* const result = birthdateSchema.parse(new Date()); // Date object
* ```
*/
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
/**
* Creates an enum schema with predefined values.
*
* @param definition - Array of allowed string values
* @param options - Optional configuration (name, message)
* @returns Enum schema interface
*
* @example
* ```typescript
* const roleSchema = s.enum(['admin', 'user', 'guest']);
* const result = roleSchema.parse('admin'); // 'admin'
* ```
*/
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
/**
* Creates an array schema with element validation.
*
* @param definition - Schema for array elements
* @param options - Optional configuration (name, message)
* @returns Array schema interface
*
* @example
* ```typescript
* const tagsSchema = s.array(s.string());
* const result = tagsSchema.parse(['tag1', 'tag2']); // ['tag1', 'tag2']
* ```
*/
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
/**
* Creates a schema that accepts any value without validation.
*
* @returns Schema interface that accepts any value
*
* @example
* ```typescript
* const anySchema = s.any();
* const result = anySchema.parse({ anything: true }); // { anything: true }
* ```
*/
any(): any;
/**
* Preprocesses a value before passing it to a schema for validation.
*
* @param callback - Function to transform the value before validation
* @param schema - Schema to validate the transformed value
* @returns Modified schema with preprocessing
*
* @example
* ```typescript
* const schema = s.preprocess(
* (val) => String(val).trim(),
* s.string().min(3)
* );
* const result = schema.parse(' hello '); // 'hello'
* ```
*/
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
/**
* Creates a union schema that validates against multiple schemas.
* The value must match at least one of the provided schemas.
*
* @param definitions - Array of schemas to validate against
* @param options - Optional configuration (name, message)
* @returns Union schema interface
*
* @example
* ```typescript
* const idSchema = s.union([s.string(), s.number()]);
* const result1 = idSchema.parse('abc'); // 'abc'
* const result2 = idSchema.parse(123); // 123
* ```
*/
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
};
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: string, action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
/**
* Extends the schema system with custom validation methods.
* Used to add new methods to schema interfaces like StringSchemaInterface, NumberSchemaInterface, etc.
*
* @param callback - Function that receives schema, validation, and options, and can add new methods
*
* @example
* ```typescript
* import { extend, type StringSchemaInterface } from '@esmj/schema';
*
* // Extend StringSchemaInterface with email validation
* declare module '@esmj/schema' {
* interface StringSchemaInterface {
* email(): StringSchemaInterface;
* }
* }
*
* extend((schema, _, options) => {
* if (options?.type === 'string') {
* schema.email = function() {
* return this.refine(
* (val) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val),
* { message: 'Invalid email address' }
* );
* };
* }
* return schema;
* });
*
* // Now you can use the email() method
* const emailSchema = s.string().email();
* ```
*/
declare function extend(callback: ExtenderType): void;
/**
* Type helper to infer the output type of a schema.
* Extracts the TypeScript type that a schema will produce after parsing.
*
* @template T - The schema type to infer from
*
* @example
* ```typescript
* const userSchema = s.object({
* name: s.string(),
* age: s.number(),
* email: s.string().optional()
* });
*
* type User = Infer<typeof userSchema>;
* // type User = {
* // name: string;
* // age: number;
* // email?: string;
* // }
* ```
*/
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
declare module './index.ts' {
interface NumberSchemaInterface {
min(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
max(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
positive(options?: SchemaInterfaceOptions): NumberSchemaInterface;
negative(options?: SchemaInterfaceOptions): NumberSchemaInterface;
int(options?: SchemaInterfaceOptions): NumberSchemaInterface;
float(options?: SchemaInterfaceOptions): NumberSchemaInterface;
multipleOf(value: number, options?: SchemaInterfaceOptions): NumberSchemaInterface;
finite(options?: SchemaInterfaceOptions): NumberSchemaInterface;
}
}
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.cjs';
declare module './index.ts' {
interface StringSchemaInterface {
min(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
max(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
length(length: number, options?: SchemaInterfaceOptions): StringSchemaInterface;
nonEmpty(options?: SchemaInterfaceOptions): StringSchemaInterface;
startsWith(prefix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
endsWith(suffix: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
includes(substring: string, options?: SchemaInterfaceOptions): StringSchemaInterface;
trim(): StringSchemaInterface;
toLowerCase(): StringSchemaInterface;
toUpperCase(): StringSchemaInterface;
padStart(length: number, fillChar?: string): StringSchemaInterface;
padEnd(length: number, fillChar?: string): StringSchemaInterface;
replace(search: string | RegExp, replace: string): StringSchemaInterface;
}
}