@hedystia/validations 1.10.0 → 1.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.mts +2 -0
- package/dist/index.mjs +2 -0
- package/dist/types.cjs +562 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +244 -0
- package/dist/types.d.mts +244 -0
- package/dist/types.mjs +550 -0
- package/dist/types.mjs.map +1 -0
- package/package.json +27 -7
- package/readme.md +49 -131
- package/dist/index.d.ts +0 -311
- package/dist/index.js +0 -1
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hedystia/validations",
|
|
3
|
-
"version": "1.10.
|
|
3
|
+
"version": "1.10.2",
|
|
4
|
+
"description": "Next-gen TypeScript framework for building type-safe APIs at lightspeed",
|
|
5
|
+
"homepage": "https://docs.hedystia.com/validations/start",
|
|
4
6
|
"devDependencies": {
|
|
5
7
|
"@standard-schema/spec": "^1.0.0",
|
|
6
|
-
"@types/bun": "^1.3.3"
|
|
7
|
-
"tsup": "^8.3.5"
|
|
8
|
+
"@types/bun": "^1.3.3"
|
|
8
9
|
},
|
|
9
10
|
"peerDependencies": {
|
|
10
11
|
"typescript": ">= 5.0.0"
|
|
@@ -15,14 +16,26 @@
|
|
|
15
16
|
}
|
|
16
17
|
},
|
|
17
18
|
"private": false,
|
|
19
|
+
"keywords": [
|
|
20
|
+
"hedystia",
|
|
21
|
+
"framework",
|
|
22
|
+
"typescript",
|
|
23
|
+
"api",
|
|
24
|
+
"type-safe",
|
|
25
|
+
"bun",
|
|
26
|
+
"server",
|
|
27
|
+
"backend",
|
|
28
|
+
"web"
|
|
29
|
+
],
|
|
18
30
|
"license": "MIT",
|
|
19
31
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
32
|
+
"build": "tsdown --config-loader unrun",
|
|
33
|
+
"publish": "npm publish --access public",
|
|
34
|
+
"dev": "tsdown --watch"
|
|
22
35
|
},
|
|
23
36
|
"repository": {
|
|
24
37
|
"type": "git",
|
|
25
|
-
"url": "https://github.com/Hedystia/
|
|
38
|
+
"url": "https://github.com/Hedystia/Hedystia"
|
|
26
39
|
},
|
|
27
40
|
"author": {
|
|
28
41
|
"name": "Zastinian",
|
|
@@ -32,5 +45,12 @@
|
|
|
32
45
|
"type": "commonjs",
|
|
33
46
|
"types": "./dist/index.d.ts",
|
|
34
47
|
"main": "./dist/index.js",
|
|
35
|
-
"module": "./dist/index.
|
|
48
|
+
"module": "./dist/index.mjs",
|
|
49
|
+
"exports": {
|
|
50
|
+
".": {
|
|
51
|
+
"types": "./dist/index.d.ts",
|
|
52
|
+
"import": "./dist/index.mjs",
|
|
53
|
+
"require": "./dist/index.js"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
36
56
|
}
|
package/readme.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
<div align="center">
|
|
2
2
|
<p>
|
|
3
|
-
<strong>🚀 Hedystia
|
|
3
|
+
<strong>🚀 Hedystia Validations</strong>
|
|
4
4
|
</p>
|
|
5
5
|
|
|
6
6
|
<p>
|
|
7
|
-
<strong>Next-gen TypeScript
|
|
7
|
+
<strong>Next-gen TypeScript validation system for building type-safe APIs at lightspeed! ⚡</strong>
|
|
8
8
|
</p>
|
|
9
9
|
|
|
10
10
|
<p>
|
|
11
|
-
<a href="https://docs.hedystia.com"><img src="https://img.shields.io/badge/Docs-blue?style=flat-square" alt="Documentation"></a>
|
|
12
|
-
<a href="https://www.npmjs.com/package/
|
|
13
|
-
<a href="https://www.npmjs.com/package/
|
|
14
|
-
<a href="LICENSE"><img src="https://img.shields.io/github/license/Hedystia/
|
|
11
|
+
<a href="https://docs.hedystia.com/validations/start"><img src="https://img.shields.io/badge/Docs-blue?style=flat-square" alt="Documentation"></a>
|
|
12
|
+
<a href="https://www.npmjs.com/package/@hedystia/validations"><img src="https://img.shields.io/npm/v/@hedystia/validations.svg?style=flat-square" alt="npm version"></a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/@hedystia/validations"><img src="https://img.shields.io/npm/dm/@hedystia/validations.svg?style=flat-square" alt="npm downloads"></a>
|
|
14
|
+
<a href="LICENSE"><img src="https://img.shields.io/github/license/Hedystia/Hedystia.svg?style=flat-square" alt="license"></a>
|
|
15
15
|
<img src="https://img.shields.io/badge/Bun-powered-FFD43B?style=flat-square&logo=bun" alt="Bun powered">
|
|
16
16
|
</p>
|
|
17
17
|
</div>
|
|
@@ -19,154 +19,72 @@
|
|
|
19
19
|
## 🌟 Superpowers
|
|
20
20
|
|
|
21
21
|
- 🌐 **Multi-runtime support** - Bun (default), Deno, Node.js, Vercel, Cloudflare Workers, Fastly Compute, Lambda, etc.
|
|
22
|
-
- 🔒 **End-to-end type safety** -
|
|
23
|
-
- ⚡ **
|
|
24
|
-
-
|
|
25
|
-
- 🛡️ **Validation built-in** - Zod integration for runtime safety
|
|
26
|
-
- 🔌 **Extensible architecture** - Middleware, hooks and macros system
|
|
27
|
-
- 📝 **Standard Schema** - Compatibility with the standard schema so you can use it with Zod, Arktype, etc.
|
|
22
|
+
- 🔒 **End-to-end type safety** - Powerful built-in schema builder `h`.
|
|
23
|
+
- ⚡ **Lightweight & Fast** - Built for maximum performance without overhead.
|
|
24
|
+
- 📝 **Standard Schema** - 100% compatibility with the [Standard Schema](https://standardschema.dev/) specification.
|
|
28
25
|
|
|
29
26
|
## 🚀 Launch in 30 Seconds
|
|
30
27
|
|
|
31
28
|
1. Install with Bun:
|
|
32
29
|
```bash
|
|
33
|
-
bun add hedystia
|
|
30
|
+
bun add @hedystia/validations
|
|
34
31
|
```
|
|
35
32
|
|
|
36
|
-
2. Create your
|
|
33
|
+
2. Create your schemas:
|
|
37
34
|
```typescript
|
|
38
|
-
import {
|
|
39
|
-
|
|
40
|
-
const app = new Hedystia()
|
|
41
|
-
.get("/hello/:name", (ctx) => `Hello ${ctx.params.name}!`, {
|
|
42
|
-
params: h.object({ name: h.string() }),
|
|
43
|
-
response: h.string()
|
|
44
|
-
})
|
|
45
|
-
.listen(3000);
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
3. Generate client and consume API:
|
|
49
|
-
```typescript
|
|
50
|
-
import { createClient } from "@hedystia/client";
|
|
51
|
-
|
|
52
|
-
const client = createClient<typeof app>("http://localhost:3000");
|
|
35
|
+
import { h } from "@hedystia/validations";
|
|
53
36
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
## 💡 Why Developers Love Hedystia
|
|
60
|
-
|
|
61
|
-
### 🔄 Full-stack Type Safety
|
|
62
|
-
```typescript
|
|
63
|
-
// Server-side validation
|
|
64
|
-
.post("/users", (ctx) => {...}, {
|
|
65
|
-
body: h.object({
|
|
66
|
-
email: h.email(),
|
|
67
|
-
age: h.number()
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
|
|
71
|
-
// Client-side types
|
|
72
|
-
await client.users.post({
|
|
73
|
-
body: {
|
|
74
|
-
email: "user@example.com", // Autocompletes!
|
|
75
|
-
age: 25 // Type-checked
|
|
76
|
-
}
|
|
37
|
+
const userSchema = h.object({
|
|
38
|
+
id: h.number(),
|
|
39
|
+
name: h.string(),
|
|
40
|
+
email: h.string().email(),
|
|
41
|
+
tags: h.string().array().optional()
|
|
77
42
|
});
|
|
78
43
|
```
|
|
79
44
|
|
|
80
|
-
|
|
81
|
-
|
|
45
|
+
3. Type inference:
|
|
82
46
|
```typescript
|
|
83
|
-
import {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
]
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
app.use("/swagger", swaggerPlugin.plugin(app));
|
|
95
|
-
|
|
96
|
-
app.listen(3000);
|
|
47
|
+
import { Infer } from "@hedystia/validations";
|
|
48
|
+
|
|
49
|
+
type User = Infer<typeof userSchema>;
|
|
50
|
+
/*
|
|
51
|
+
{
|
|
52
|
+
id: number;
|
|
53
|
+
name: string;
|
|
54
|
+
email: string;
|
|
55
|
+
tags?: string[] | undefined;
|
|
56
|
+
}
|
|
57
|
+
*/
|
|
97
58
|
```
|
|
98
59
|
|
|
99
|
-
|
|
100
|
-
- Bun runtime optimized
|
|
101
|
-
- Faster by default
|
|
102
|
-
- Own type validation system
|
|
103
|
-
- Faster than Express
|
|
104
|
-
- Built-in response compression
|
|
105
|
-
|
|
106
|
-
### 🧩 Modern Feature Set
|
|
107
|
-
```typescript
|
|
108
|
-
// File uploads
|
|
109
|
-
.post("/upload", async (ctx) => {
|
|
110
|
-
const formData = await ctx.body; // FormData type
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
// Binary responses
|
|
114
|
-
.get("/pdf", () => new Blob([...]), {
|
|
115
|
-
response: h.instanceof(Blob)
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
// Nested routing
|
|
119
|
-
.group("/api/v1", (v1) => v1
|
|
120
|
-
.group("/users", (users) => users
|
|
121
|
-
.get("/:id", ...)
|
|
122
|
-
)
|
|
123
|
-
)
|
|
124
|
-
```
|
|
60
|
+
## 💡 Why Developers Love @hedystia/validations
|
|
125
61
|
|
|
126
|
-
|
|
62
|
+
### 🔄 Standard Schema Compatibility
|
|
63
|
+
Since `h` implements the Standard Schema specification, you can use it alongside any other Standard Schema-compatible library.
|
|
127
64
|
|
|
128
|
-
###
|
|
129
|
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
132
|
-
-
|
|
133
|
-
-
|
|
134
|
-
-
|
|
65
|
+
### 🧩 Complete Primitive & Composite Types
|
|
66
|
+
Provides a robust set of types to model any data structure:
|
|
67
|
+
- `h.string()` with built-in formats (email, uuid, phone, domain, date, regex)
|
|
68
|
+
- `h.number()` with `.min()`, `.max()` and coercion
|
|
69
|
+
- `h.boolean()`
|
|
70
|
+
- `h.literal()` and `h.options()` for unions
|
|
71
|
+
- `h.object()` and `.array()`
|
|
135
72
|
|
|
136
|
-
###
|
|
137
|
-
|
|
138
|
-
- ✅ Hooks System (onRequest, onError, etc)
|
|
139
|
-
- ✅ Macro System for Auth/Rate Limiting
|
|
140
|
-
- ✅ OpenAPI - Swagger Integration
|
|
73
|
+
### ⚡ Built-in Coercion
|
|
74
|
+
URL parameters and query strings are always strings. Use `.coerce()` to convert them automatically:
|
|
141
75
|
|
|
142
|
-
## 💼 Production Ready
|
|
143
76
|
```typescript
|
|
144
|
-
//
|
|
145
|
-
.
|
|
146
|
-
ctx.set.status(500);
|
|
147
|
-
return {
|
|
148
|
-
error: err.message
|
|
149
|
-
};
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
// Rate limiting macro
|
|
153
|
-
.macro({
|
|
154
|
-
rateLimit: () => ({
|
|
155
|
-
resolve: async (ctx) => {
|
|
156
|
-
// Implement your logic
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
})
|
|
77
|
+
h.number().coerce() // "42" -> 42
|
|
78
|
+
h.boolean().coerce() // "true" -> true
|
|
160
79
|
```
|
|
161
80
|
|
|
162
81
|
## 📜 License
|
|
163
|
-
MIT License ©
|
|
82
|
+
MIT License © 2026 Hedystia
|
|
164
83
|
|
|
165
|
-
##
|
|
166
|
-
- [
|
|
167
|
-
- [
|
|
168
|
-
- [API Reference](https://docs.hedystia.com/framework/overview)
|
|
84
|
+
## 📖 Documentation
|
|
85
|
+
- [Validations Documentation](https://docs.hedystia.com/validations/start)
|
|
86
|
+
- [Framework API Reference](https://docs.hedystia.com/framework/overview)
|
|
169
87
|
|
|
170
|
-
##
|
|
171
|
-
- [GitHub Issues](https://github.com/Hedystia/
|
|
88
|
+
## 🗣️ Community
|
|
89
|
+
- [GitHub Issues](https://github.com/Hedystia/Hedystia/issues)
|
|
172
90
|
- [Discord Server](https://hedystia.com/support)
|
package/dist/index.d.ts
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
/** The Standard Schema interface. */
|
|
2
|
-
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
3
|
-
/** The Standard Schema properties. */
|
|
4
|
-
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|
5
|
-
}
|
|
6
|
-
declare namespace StandardSchemaV1 {
|
|
7
|
-
/** The Standard Schema properties interface. */
|
|
8
|
-
export interface Props<Input = unknown, Output = Input> {
|
|
9
|
-
/** The version number of the standard. */
|
|
10
|
-
readonly version: 1;
|
|
11
|
-
/** The vendor name of the schema library. */
|
|
12
|
-
readonly vendor: string;
|
|
13
|
-
/** Validates unknown input values. */
|
|
14
|
-
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
|
15
|
-
/** Inferred types associated with the schema. */
|
|
16
|
-
readonly types?: Types<Input, Output> | undefined;
|
|
17
|
-
}
|
|
18
|
-
/** The result interface of the validate function. */
|
|
19
|
-
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|
20
|
-
/** The result interface if validation succeeds. */
|
|
21
|
-
export interface SuccessResult<Output> {
|
|
22
|
-
/** The typed output value. */
|
|
23
|
-
readonly value: Output;
|
|
24
|
-
/** The non-existent issues. */
|
|
25
|
-
readonly issues?: undefined;
|
|
26
|
-
}
|
|
27
|
-
/** The result interface if validation fails. */
|
|
28
|
-
export interface FailureResult {
|
|
29
|
-
/** The issues of failed validation. */
|
|
30
|
-
readonly issues: ReadonlyArray<Issue>;
|
|
31
|
-
}
|
|
32
|
-
/** The issue interface of the failure output. */
|
|
33
|
-
export interface Issue {
|
|
34
|
-
/** The error message of the issue. */
|
|
35
|
-
readonly message: string;
|
|
36
|
-
/** The path of the issue, if any. */
|
|
37
|
-
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|
38
|
-
}
|
|
39
|
-
/** The path segment interface of the issue. */
|
|
40
|
-
export interface PathSegment {
|
|
41
|
-
/** The key representing a path segment. */
|
|
42
|
-
readonly key: PropertyKey;
|
|
43
|
-
}
|
|
44
|
-
/** The Standard Schema types interface. */
|
|
45
|
-
export interface Types<Input = unknown, Output = Input> {
|
|
46
|
-
/** The input type of the schema. */
|
|
47
|
-
readonly input: Input;
|
|
48
|
-
/** The output type of the schema. */
|
|
49
|
-
readonly output: Output;
|
|
50
|
-
}
|
|
51
|
-
/** Infers the input type of a Standard Schema. */
|
|
52
|
-
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
|
53
|
-
/** Infers the output type of a Standard Schema. */
|
|
54
|
-
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
|
55
|
-
export { };
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
type SchemaPrimitive = "string" | "number" | "boolean" | "any";
|
|
59
|
-
interface SchemaLike {
|
|
60
|
-
[key: string]: SchemaPrimitive | SchemaLike | BaseSchema<any, any>;
|
|
61
|
-
}
|
|
62
|
-
type Simplify<T> = T extends any ? {
|
|
63
|
-
[K in keyof T]: T[K];
|
|
64
|
-
} : never;
|
|
65
|
-
type RequiredKeys<S> = {
|
|
66
|
-
[K in keyof S]: S[K] extends OptionalSchema<any, any> ? never : K;
|
|
67
|
-
}[keyof S];
|
|
68
|
-
type OptionalKeys<S> = {
|
|
69
|
-
[K in keyof S]: S[K] extends OptionalSchema<any, any> ? K : never;
|
|
70
|
-
}[keyof S];
|
|
71
|
-
type SchemaPrimitiveMap = {
|
|
72
|
-
string: string;
|
|
73
|
-
number: number;
|
|
74
|
-
boolean: boolean;
|
|
75
|
-
any: unknown;
|
|
76
|
-
};
|
|
77
|
-
type SchemaType<S> = S extends BaseSchema<any, infer O> ? O : S extends keyof SchemaPrimitiveMap ? SchemaPrimitiveMap[S] : S extends Record<string, any> ? InferSchema<S> : unknown;
|
|
78
|
-
type InferObject<S extends SchemaDefinition> = Simplify<{
|
|
79
|
-
[K in RequiredKeys<S>]: SchemaType<S[K]>;
|
|
80
|
-
} & {
|
|
81
|
-
[K in OptionalKeys<S>]?: SchemaType<S[K]>;
|
|
82
|
-
}>;
|
|
83
|
-
type InferSchema<S> = S extends BaseSchema<any, infer O> ? O : S extends "string" ? string : S extends "number" ? number : S extends "boolean" ? boolean : S extends {
|
|
84
|
-
[key: string]: any;
|
|
85
|
-
} ? {
|
|
86
|
-
[K in keyof S as undefined extends InferSchema<S[K]> ? K : never]?: InferSchema<S[K]>;
|
|
87
|
-
} & {
|
|
88
|
-
[K in keyof S as undefined extends InferSchema<S[K]> ? never : K]: InferSchema<S[K]>;
|
|
89
|
-
} : unknown;
|
|
90
|
-
type SchemaDefinition = SchemaLike;
|
|
91
|
-
interface Schema<I, O> extends StandardSchemaV1<I, O> {
|
|
92
|
-
optional(): OptionalSchema<I, O | undefined>;
|
|
93
|
-
enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
|
|
94
|
-
array(): ArraySchema<I, O[]>;
|
|
95
|
-
instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
|
|
96
|
-
jsonSchema: any;
|
|
97
|
-
readonly inferred: O;
|
|
98
|
-
schema: Schema<I, O>;
|
|
99
|
-
}
|
|
100
|
-
declare abstract class BaseSchema<I, O> implements Schema<I, O> {
|
|
101
|
-
abstract readonly "~standard": StandardSchemaV1.Props<I, O>;
|
|
102
|
-
jsonSchema: any;
|
|
103
|
-
get inferred(): O;
|
|
104
|
-
schema: Schema<I, O>;
|
|
105
|
-
protected _coerce: boolean;
|
|
106
|
-
coerce(): this;
|
|
107
|
-
optional(): OptionalSchema<I, O | undefined>;
|
|
108
|
-
null(): UnionSchema<I, O | null>;
|
|
109
|
-
enum<V extends O & (string | number | boolean), Values extends readonly [V, ...V[]]>(values: Values): UnionSchema<I, Values[number]>;
|
|
110
|
-
array(): ArraySchema<I, O[]>;
|
|
111
|
-
instanceOf<C extends new (...args: any[]) => any>(constructor: C): InstanceOfSchema<I, InstanceType<C>>;
|
|
112
|
-
}
|
|
113
|
-
declare class StringSchemaType extends BaseSchema<unknown, string> {
|
|
114
|
-
readonly type: SchemaPrimitive;
|
|
115
|
-
private _validateDate;
|
|
116
|
-
private _validateUUID;
|
|
117
|
-
private _validateRegex;
|
|
118
|
-
private _validateEmail;
|
|
119
|
-
private _validatePhone;
|
|
120
|
-
private _validateDomain;
|
|
121
|
-
private _requireHttpOrHttps;
|
|
122
|
-
private _minLength?;
|
|
123
|
-
private _maxLength?;
|
|
124
|
-
constructor();
|
|
125
|
-
primitive(): SchemaPrimitive;
|
|
126
|
-
minLength(n: number): StringSchemaType;
|
|
127
|
-
maxLength(n: number): StringSchemaType;
|
|
128
|
-
date(): StringSchemaType;
|
|
129
|
-
uuid(): StringSchemaType;
|
|
130
|
-
regex(regex: RegExp): StringSchemaType;
|
|
131
|
-
email(): StringSchemaType;
|
|
132
|
-
phone(): StringSchemaType;
|
|
133
|
-
domain(requireHttpOrHttps?: boolean): StringSchemaType;
|
|
134
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, string>;
|
|
135
|
-
private _isValidDate;
|
|
136
|
-
private _isValidUUID;
|
|
137
|
-
private _isValidRegex;
|
|
138
|
-
private _isValidEmail;
|
|
139
|
-
private _isValidPhone;
|
|
140
|
-
private _isValidDomain;
|
|
141
|
-
}
|
|
142
|
-
declare class NumberSchemaType extends BaseSchema<unknown, number> {
|
|
143
|
-
readonly type: SchemaPrimitive;
|
|
144
|
-
private _min?;
|
|
145
|
-
private _max?;
|
|
146
|
-
constructor();
|
|
147
|
-
primitive(): SchemaPrimitive;
|
|
148
|
-
min(n: number): NumberSchemaType;
|
|
149
|
-
max(n: number): NumberSchemaType;
|
|
150
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, number>;
|
|
151
|
-
}
|
|
152
|
-
declare class BooleanSchemaType extends BaseSchema<unknown, boolean> {
|
|
153
|
-
readonly type: SchemaPrimitive;
|
|
154
|
-
constructor();
|
|
155
|
-
primitive(): SchemaPrimitive;
|
|
156
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, boolean>;
|
|
157
|
-
}
|
|
158
|
-
declare class AnySchemaType extends BaseSchema<unknown, any> {
|
|
159
|
-
readonly type: SchemaPrimitive;
|
|
160
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, any>;
|
|
161
|
-
}
|
|
162
|
-
declare class LiteralSchema<I, T extends string | number | boolean> extends BaseSchema<I, T> {
|
|
163
|
-
private readonly value;
|
|
164
|
-
constructor(value: T);
|
|
165
|
-
readonly "~standard": StandardSchemaV1.Props<I, T>;
|
|
166
|
-
}
|
|
167
|
-
declare class OptionalSchema<I, O> extends BaseSchema<I, O | undefined> {
|
|
168
|
-
private readonly innerSchema;
|
|
169
|
-
constructor(schema: Schema<I, O>);
|
|
170
|
-
readonly "~standard": StandardSchemaV1.Props<I, O | undefined>;
|
|
171
|
-
}
|
|
172
|
-
declare class NullSchemaType extends BaseSchema<unknown, null> {
|
|
173
|
-
readonly type = "null";
|
|
174
|
-
constructor();
|
|
175
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, null>;
|
|
176
|
-
}
|
|
177
|
-
declare class UnionSchema<I, O> extends BaseSchema<I, O> {
|
|
178
|
-
private readonly schemas;
|
|
179
|
-
constructor(...schemas: Schema<I, any>[]);
|
|
180
|
-
readonly "~standard": StandardSchemaV1.Props<I, O>;
|
|
181
|
-
}
|
|
182
|
-
declare class ArraySchema<I, O extends any[]> extends BaseSchema<I, O> {
|
|
183
|
-
private readonly innerSchema;
|
|
184
|
-
constructor(schema: Schema<I, O[number]>);
|
|
185
|
-
readonly "~standard": StandardSchemaV1.Props<I, O>;
|
|
186
|
-
}
|
|
187
|
-
declare class InstanceOfSchema<I, O> extends BaseSchema<I, O> {
|
|
188
|
-
private readonly innerSchema;
|
|
189
|
-
private readonly classConstructor;
|
|
190
|
-
constructor(schema: Schema<I, any>, classConstructor: new (...args: any[]) => any);
|
|
191
|
-
readonly "~standard": StandardSchemaV1.Props<I, O>;
|
|
192
|
-
}
|
|
193
|
-
declare class ObjectSchemaType<T extends Record<string, unknown>> extends BaseSchema<unknown, T> {
|
|
194
|
-
readonly definition: SchemaDefinition;
|
|
195
|
-
constructor(definition: SchemaDefinition);
|
|
196
|
-
readonly "~standard": StandardSchemaV1.Props<unknown, T>;
|
|
197
|
-
}
|
|
198
|
-
type AnySchema = SchemaPrimitive | BaseSchema<any, any> | SchemaDefinition;
|
|
199
|
-
declare function toStandard<T>(schema: AnySchema): Schema<unknown, T>;
|
|
200
|
-
/**
|
|
201
|
-
* Create standard schema types
|
|
202
|
-
* @returns {typeof h} Standard schema types
|
|
203
|
-
*/
|
|
204
|
-
declare const h: {
|
|
205
|
-
/**
|
|
206
|
-
* Create string schema type
|
|
207
|
-
* @returns {StringSchemaType} String schema type
|
|
208
|
-
*/
|
|
209
|
-
string: () => StringSchemaType;
|
|
210
|
-
/**
|
|
211
|
-
* Create number schema type
|
|
212
|
-
* @returns {NumberSchemaType} Number schema type
|
|
213
|
-
*/
|
|
214
|
-
number: () => NumberSchemaType;
|
|
215
|
-
/**
|
|
216
|
-
* Create boolean schema type
|
|
217
|
-
* @returns {BooleanSchemaType} Boolean schema type
|
|
218
|
-
*/
|
|
219
|
-
boolean: () => BooleanSchemaType;
|
|
220
|
-
/**
|
|
221
|
-
* Create null schema type
|
|
222
|
-
* @returns {NullSchemaType} Null schema type
|
|
223
|
-
*/
|
|
224
|
-
null: () => NullSchemaType;
|
|
225
|
-
/**
|
|
226
|
-
* Create any schema type
|
|
227
|
-
* @returns {AnySchemaType} Any schema type
|
|
228
|
-
*/
|
|
229
|
-
any: () => AnySchemaType;
|
|
230
|
-
/**
|
|
231
|
-
* Create literal schema type
|
|
232
|
-
* @param {T} value - Literal value
|
|
233
|
-
* @returns {LiteralSchema<unknown, T>} Literal schema type
|
|
234
|
-
*/
|
|
235
|
-
literal: <T extends string | number | boolean>(value: T) => LiteralSchema<unknown, T>;
|
|
236
|
-
/**
|
|
237
|
-
* Create object schema type
|
|
238
|
-
* @param {S} [schemaDef] - Schema definition
|
|
239
|
-
* @returns {ObjectSchemaType<InferObject<S>>} Object schema type
|
|
240
|
-
*/
|
|
241
|
-
object: <S extends SchemaDefinition>(schemaDef?: S) => ObjectSchemaType<InferObject<S>>;
|
|
242
|
-
/**
|
|
243
|
-
* Create array schema type
|
|
244
|
-
* @param {S} schema - Schema
|
|
245
|
-
* @returns {ArraySchema<unknown, InferSchema<S>[]>} Array schema type
|
|
246
|
-
*/
|
|
247
|
-
array: <S extends AnySchema>(schema: S) => ArraySchema<unknown, SchemaType<S>[]>;
|
|
248
|
-
/**
|
|
249
|
-
* Create enum schema type from a list of string, number or boolean values.
|
|
250
|
-
* @param {Values} values - An array of literal values.
|
|
251
|
-
* @returns {UnionSchema<unknown, Values[number]>} A schema that validates against one of the provided literal values.
|
|
252
|
-
*/
|
|
253
|
-
enum: <T extends string | number | boolean, Values extends readonly [T, ...T[]]>(values: Values) => UnionSchema<unknown, Values[number]>;
|
|
254
|
-
/**
|
|
255
|
-
* Create optional schema type
|
|
256
|
-
* @param {S} schema - Schema
|
|
257
|
-
* @returns {OptionalSchema<unknown, InferSchema<S> | undefined>} Optional schema type
|
|
258
|
-
*/
|
|
259
|
-
optional: <S extends AnySchema>(schema: S) => OptionalSchema<unknown, InferSchema<S> | undefined>;
|
|
260
|
-
/**
|
|
261
|
-
* Create options schema type
|
|
262
|
-
* @param {S} schemas - Schemas
|
|
263
|
-
* @returns {UnionSchema<unknown, InferSchema<S[number]>>} Options schema type
|
|
264
|
-
*/
|
|
265
|
-
options: <S extends AnySchema[]>(...schemas: S) => UnionSchema<unknown, InferSchema<S[number]>>;
|
|
266
|
-
/**
|
|
267
|
-
* Create instance of schema type
|
|
268
|
-
* @param {C} constructor - Constructor function
|
|
269
|
-
* @returns {InstanceOfSchema<unknown, InstanceType<C>>} Instance of schema type
|
|
270
|
-
*/
|
|
271
|
-
instanceOf: <C extends new (...args: any[]) => any>(constructor: C) => InstanceOfSchema<unknown, InstanceType<C>>;
|
|
272
|
-
/**
|
|
273
|
-
* Create date schema type
|
|
274
|
-
* @returns {StringSchemaType} Date schema type
|
|
275
|
-
*/
|
|
276
|
-
date: () => StringSchemaType;
|
|
277
|
-
/**
|
|
278
|
-
* Create UUID schema type
|
|
279
|
-
* @returns {StringSchemaType} UUID schema type
|
|
280
|
-
*/
|
|
281
|
-
uuid: () => StringSchemaType;
|
|
282
|
-
/**
|
|
283
|
-
* Create regex schema type
|
|
284
|
-
* @param {RegExp} regex - Regex
|
|
285
|
-
* @returns {StringSchemaType} Regex schema type
|
|
286
|
-
*/
|
|
287
|
-
regex: (regex: RegExp) => StringSchemaType;
|
|
288
|
-
/**
|
|
289
|
-
* Create email schema type
|
|
290
|
-
* @returns {StringSchemaType} Email schema type
|
|
291
|
-
*/
|
|
292
|
-
email: () => StringSchemaType;
|
|
293
|
-
/**
|
|
294
|
-
* Create phone schema type
|
|
295
|
-
* @returns {StringSchemaType} Phone schema type
|
|
296
|
-
*/
|
|
297
|
-
phone: () => StringSchemaType;
|
|
298
|
-
/** Create domain schema type
|
|
299
|
-
* @param {boolean} requireHttpOrHttps - Require http or https
|
|
300
|
-
* @returns {StringSchemaType} Domain schema type
|
|
301
|
-
*/
|
|
302
|
-
domain: (requireHttpOrHttps?: boolean) => StringSchemaType;
|
|
303
|
-
/**
|
|
304
|
-
* Convert schema to standard schema
|
|
305
|
-
* @param {AnySchema} schema - Schema
|
|
306
|
-
* @returns {Schema<unknown, any>} Standard schema
|
|
307
|
-
*/
|
|
308
|
-
toStandard: typeof toStandard;
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
export { type AnySchema, AnySchemaType, ArraySchema, BaseSchema, BooleanSchemaType, InstanceOfSchema, LiteralSchema, NullSchemaType, NumberSchemaType, ObjectSchemaType, OptionalSchema, StringSchemaType, UnionSchema, h };
|
package/dist/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var O=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var V=(t,e)=>{for(var n in e)O(t,n,{get:e[n],enumerable:!0})},_=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of T(e))!j.call(t,a)&&a!==n&&O(t,a,{get:()=>e[a],enumerable:!(r=k(e,a))||r.enumerable});return t};var P=t=>_(O({},"__esModule",{value:!0}),t);var D={};V(D,{AnySchemaType:()=>w,ArraySchema:()=>x,BaseSchema:()=>i,BooleanSchemaType:()=>l,InstanceOfSchema:()=>I,LiteralSchema:()=>y,NullSchemaType:()=>f,NumberSchemaType:()=>p,ObjectSchemaType:()=>g,OptionalSchema:()=>u,StringSchemaType:()=>S,UnionSchema:()=>h,h:()=>m});module.exports=P(D);var i=class{jsonSchema={};get inferred(){return null}schema=this;_coerce=!1;coerce(){return this._coerce=!0,this}optional(){return new u(this)}null(){return new h(this,new f)}enum(e){let n=e.map(r=>new y(r));return new h(...n)}array(){return new x(this)}instanceOf(e){return new I(this,e)}};function R(t,e){return typeof e=="string"&&t==="string"||typeof e=="number"&&t==="number"&&!Number.isNaN(e)||typeof e=="boolean"&&t==="boolean"}var S=class t extends i{type="string";_validateDate=!1;_validateUUID=!1;_validateRegex=!1;_validateEmail=!1;_validatePhone=!1;_validateDomain=!1;_requireHttpOrHttps=!1;_minLength;_maxLength;constructor(){super(),this.jsonSchema={type:"string"}}primitive(){return this.type}minLength(e){let n=new t;return Object.assign(n,this),n._minLength=e,n.jsonSchema={...this.jsonSchema,minLength:e},n}maxLength(e){let n=new t;return Object.assign(n,this),n._maxLength=e,n.jsonSchema={...this.jsonSchema,maxLength:e},n}date(){let e=new t;return Object.assign(e,this),e._validateDate=!0,e.jsonSchema={...this.jsonSchema,format:"date"},e}uuid(){let e=new t;return e._validateUUID=!0,e.jsonSchema={...this.jsonSchema,format:"uuid"},e}regex(e){let n=new t;return n._validateRegex=!0,n.jsonSchema={...this.jsonSchema,pattern:e.source},n}email(){let e=new t;return e._validateEmail=!0,e.jsonSchema={...this.jsonSchema,format:"email"},e}phone(){let e=new t;return e._validatePhone=!0,e.jsonSchema={...this.jsonSchema,format:"phone"},e}domain(e=!0){let n=new t;return n._validateDomain=!0,n.jsonSchema={...this.jsonSchema,format:"domain"},n._requireHttpOrHttps=e,n}"~standard"={version:1,vendor:"h-schema",validate:e=>(this._coerce&&typeof e!="string"&&(e=String(e)),typeof e!="string"?{issues:[{message:`Expected string, received ${typeof e}`}]}:this._minLength!==void 0&&e.length<this._minLength?{issues:[{message:`String shorter than ${this._minLength}`}]}:this._maxLength!==void 0&&e.length>this._maxLength?{issues:[{message:`String longer than ${this._maxLength}`}]}:this._validateUUID&&!this._isValidUUID(e)?{issues:[{message:"Invalid UUID format"}]}:this._validateRegex&&!this._isValidRegex(e)?{issues:[{message:"Invalid regex format"}]}:this._validateEmail&&!this._isValidEmail(e)?{issues:[{message:"Invalid email format"}]}:this._validatePhone&&!this._isValidPhone(e)?{issues:[{message:"Invalid phone number format"}]}:this._validateDomain&&!this._isValidDomain(e)?{issues:[{message:"Invalid domain format"}]}:this._validateDate&&!this._isValidDate(e)?{issues:[{message:"Invalid date format"}]}:{value:e}),types:{input:{},output:{}}};_isValidDate(e){let n=new Date(e);return!Number.isNaN(n.getTime())}_isValidUUID(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(e)}_isValidRegex(e){return new RegExp(this.jsonSchema.pattern).test(e)}_isValidEmail(e){return/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e)}_isValidPhone(e){return/^\+?[0-9]{7,15}$/.test(e)}_isValidDomain(e){let n=/^[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/;return this._requireHttpOrHttps&&(n=/^https?:\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,6}$/),n.test(e)}},p=class t extends i{type="number";_min;_max;constructor(){super(),this.jsonSchema={type:"number"}}primitive(){return this.type}min(e){let n=new t;return Object.assign(n,this),n._min=e,n.jsonSchema={...this.jsonSchema,minimum:e},n}max(e){let n=new t;return Object.assign(n,this),n._max=e,n.jsonSchema={...this.jsonSchema,maximum:e},n}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(this._coerce&&typeof e!="number"){let n=Number(e);Number.isNaN(n)||(e=n)}return typeof e!="number"||Number.isNaN(e)?{issues:[{message:`Expected number, received ${typeof e}`}]}:this._min!==void 0&&e<this._min?{issues:[{message:`Number less than ${this._min}`}]}:this._max!==void 0&&e>this._max?{issues:[{message:`Number greater than ${this._max}`}]}:{value:e}},types:{input:{},output:{}}}},l=class extends i{type="boolean";constructor(){super(),this.jsonSchema={type:"boolean"}}primitive(){return this.type}"~standard"={version:1,vendor:"h-schema",validate:e=>(this._coerce&&typeof e!="boolean"&&(e==="true"||e===1||e==="1"?e=!0:(e==="false"||e===0||e==="0")&&(e=!1)),typeof e!="boolean"?{issues:[{message:`Expected boolean, received ${typeof e}`}]}:{value:e}),types:{input:{},output:{}}}},w=class extends i{type="any";"~standard"={version:1,vendor:"h-schema",validate:e=>({value:e}),types:{input:{},output:{}}}},y=class extends i{value;constructor(e){super(),this.value=e,this.jsonSchema={const:e,type:typeof e}}"~standard"={version:1,vendor:"h-schema",validate:e=>e!==this.value?{issues:[{message:`Expected literal value ${this.value}, received ${e}`}]}:{value:e},types:{input:{},output:{}}}},u=class extends i{innerSchema;constructor(e){super(),this.innerSchema=e,this.jsonSchema={...e.jsonSchema}}"~standard"={version:1,vendor:"h-schema",validate:e=>e==null?{value:void 0}:this.innerSchema["~standard"].validate(e),types:{input:{},output:{}}}},f=class extends i{type="null";constructor(){super(),this.jsonSchema={type:"null"}}"~standard"={version:1,vendor:"h-schema",validate:e=>e!==null?{issues:[{message:`Expected null, received ${e===void 0?"undefined":typeof e}`}]}:{value:null},types:{input:{},output:{}}}},h=class extends i{schemas;constructor(...e){super(),this.schemas=e,this.jsonSchema={anyOf:e.map(n=>n.jsonSchema)}}"~standard"={version:1,vendor:"h-schema",validate:e=>{let n=[];for(let r of this.schemas){let a=r["~standard"].validate(e);if(!("issues"in a))return{value:a.value};n.push(...a.issues)}return{issues:n}},types:{input:{},output:{}}}},x=class extends i{innerSchema;constructor(e){super(),this.innerSchema=e,this.jsonSchema={type:"array",items:e.jsonSchema}}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(!Array.isArray(e))return{issues:[{message:`Expected array, received ${typeof e}`}]};let n=e.map((a,s)=>{let o=this.innerSchema["~standard"].validate(a);return"issues"in o?{index:s,issues:o.issues?.map(d=>({...d,path:d.path?[s,...d.path]:[s]}))}:{index:s,value:o.value}}),r=n.filter(a=>"issues"in a);return r.length>0?{issues:r.flatMap(a=>a.issues)}:{value:n.map(a=>"value"in a?a.value:null)}},types:{input:{},output:{}}}},I=class extends i{innerSchema;classConstructor;constructor(e,n){super(),this.innerSchema=e,this.classConstructor=n,this.jsonSchema={...e.jsonSchema,instanceOf:n.name}}"~standard"={version:1,vendor:"h-schema",validate:e=>e instanceof this.classConstructor?this.innerSchema["~standard"].validate(e):{issues:[{message:`Expected instance of ${this.classConstructor.name}`}]},types:{input:{},output:{}}}},g=class extends i{definition;constructor(e){super(),this.definition=e;let n={},r=[];for(let a in e){let s=e[a];s instanceof u||r.push(a),typeof s=="string"?n[a]={type:s}:s instanceof i?n[a]=s.jsonSchema:typeof s=="object"&&s!==null&&(n[a]={type:"object",properties:{}})}this.jsonSchema={type:"object",properties:n,required:r.length>0?r:void 0}}"~standard"={version:1,vendor:"h-schema",validate:e=>{if(typeof e!="object"||e===null||Array.isArray(e))return{issues:[{message:"Expected object, received "+(e===null?"null":Array.isArray(e)?"array":typeof e)}]};let n=e,r={},a=[];for(let s in this.definition){let o=this.definition[s],d=o instanceof u;if(!(s in n)&&!d){a.push({message:`Missing required property: ${s}`,path:[s]});continue}if(s in n){if(typeof o=="string"&&o in["string","number","boolean"]){let c=o;R(c,n[s])?r[s]=n[s]:a.push({message:`Invalid type for property ${s}: expected ${c}`,path:[s]})}else if(o instanceof i){let c=o["~standard"].validate(n[s]);"issues"in c?c.issues&&a.push(...c.issues.map(v=>({...v,path:v.path?[s,...v.path]:[s]}))):r[s]=c.value}}}return a.length>0?{issues:a}:{value:r}},types:{input:{},output:{}}}};function b(t){let e;if(t instanceof i)e=t;else if(typeof t=="string")if(t==="string")e=new S;else if(t==="number")e=new p;else if(t==="boolean")e=new l;else throw new Error("Invalid schema type provided to toStandard");else if(typeof t=="object"&&t!==null&&!Array.isArray(t))e=new g(t);else throw new Error("Invalid schema type provided to toStandard");let n={toJSONSchema:r=>r.jsonSchema};return{...e,inferred:null,"~standard":e["~standard"],jsonSchema:n.toJSONSchema(e),schema:e,optional:()=>new u(e),enum:e.enum.bind(e),array:e.array.bind(e),instanceOf:e.instanceOf.bind(e)}}var m={string:()=>new S,number:()=>new p,boolean:()=>new l,null:()=>new f,any:()=>new w,literal:t=>new y(t),object:t=>new g(t||{}),array:t=>b(t).array(),enum:t=>{if(!t||t.length===0)throw new Error("h.enum() requires a non-empty array of values.");let e=t.map(n=>m.literal(n));return m.options(...e)},optional:t=>b(t).optional(),options:(...t)=>{let e=t.map(n=>b(n).schema);return new h(...e)},instanceOf:t=>m.object({}).instanceOf(t),date:()=>m.string().date(),uuid:()=>m.string().uuid(),regex:t=>m.string().regex(t),email:()=>m.string().email(),phone:()=>m.string().phone(),domain:(t=!0)=>m.string().domain(t),toStandard:b};0&&(module.exports={AnySchemaType,ArraySchema,BaseSchema,BooleanSchemaType,InstanceOfSchema,LiteralSchema,NullSchemaType,NumberSchemaType,ObjectSchemaType,OptionalSchema,StringSchemaType,UnionSchema,h});
|