yedra 0.20.11 → 0.20.13

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.
Files changed (81) hide show
  1. package/dist/routing/rest.d.ts +2 -0
  2. package/dist/routing/rest.js +2 -0
  3. package/dist/routing/websocket.d.ts +1 -0
  4. package/dist/routing/websocket.js +1 -0
  5. package/dist/src/index.d.ts +9 -0
  6. package/dist/src/index.js +7 -0
  7. package/dist/src/lib.d.ts +10 -0
  8. package/dist/src/lib.js +16 -0
  9. package/dist/src/routing/app.d.ts +165 -0
  10. package/dist/src/routing/app.js +537 -0
  11. package/dist/src/routing/env.d.ts +4 -0
  12. package/dist/src/routing/env.js +13 -0
  13. package/dist/src/routing/errors.d.ts +50 -0
  14. package/dist/src/routing/errors.js +64 -0
  15. package/dist/src/routing/log.d.ts +22 -0
  16. package/dist/src/routing/log.js +30 -0
  17. package/dist/src/routing/path.d.ts +44 -0
  18. package/dist/src/routing/path.js +110 -0
  19. package/dist/src/routing/rest.d.ts +103 -0
  20. package/dist/src/routing/rest.js +181 -0
  21. package/dist/src/routing/websocket.d.ts +60 -0
  22. package/dist/src/routing/websocket.js +132 -0
  23. package/dist/src/schema-lib.d.ts +17 -0
  24. package/dist/src/schema-lib.js +20 -0
  25. package/dist/src/schema.d.ts +2 -0
  26. package/dist/src/schema.js +4 -0
  27. package/dist/src/util/counter.d.ts +7 -0
  28. package/dist/src/util/counter.js +24 -0
  29. package/dist/src/util/docs.d.ts +9 -0
  30. package/dist/src/util/docs.js +57 -0
  31. package/dist/src/util/security.d.ts +14 -0
  32. package/dist/src/util/security.js +6 -0
  33. package/dist/src/util/stream.d.ts +2 -0
  34. package/dist/src/util/stream.js +7 -0
  35. package/dist/src/validation/body.d.ts +46 -0
  36. package/dist/src/validation/body.js +15 -0
  37. package/dist/src/validation/boolean.d.ts +10 -0
  38. package/dist/src/validation/boolean.js +27 -0
  39. package/dist/src/validation/date.d.ts +11 -0
  40. package/dist/src/validation/date.js +29 -0
  41. package/dist/src/validation/doc.d.ts +10 -0
  42. package/dist/src/validation/doc.js +22 -0
  43. package/dist/src/validation/either.d.ts +15 -0
  44. package/dist/src/validation/either.js +38 -0
  45. package/dist/src/validation/enum.d.ts +19 -0
  46. package/dist/src/validation/enum.js +44 -0
  47. package/dist/src/validation/error.d.ts +21 -0
  48. package/dist/src/validation/error.js +32 -0
  49. package/dist/src/validation/integer.d.ts +23 -0
  50. package/dist/src/validation/integer.js +64 -0
  51. package/dist/src/validation/json.d.ts +12 -0
  52. package/dist/src/validation/json.js +33 -0
  53. package/dist/src/validation/lazy.d.ts +48 -0
  54. package/dist/src/validation/lazy.js +78 -0
  55. package/dist/src/validation/modifiable.d.ts +105 -0
  56. package/dist/src/validation/modifiable.js +223 -0
  57. package/dist/src/validation/none.d.ts +10 -0
  58. package/dist/src/validation/none.js +14 -0
  59. package/dist/src/validation/null.d.ts +10 -0
  60. package/dist/src/validation/null.js +21 -0
  61. package/dist/src/validation/number.d.ts +23 -0
  62. package/dist/src/validation/number.js +58 -0
  63. package/dist/src/validation/object.d.ts +38 -0
  64. package/dist/src/validation/object.js +87 -0
  65. package/dist/src/validation/raw.d.ts +13 -0
  66. package/dist/src/validation/raw.js +21 -0
  67. package/dist/src/validation/record.d.ts +16 -0
  68. package/dist/src/validation/record.js +51 -0
  69. package/dist/src/validation/schema.d.ts +35 -0
  70. package/dist/src/validation/schema.js +76 -0
  71. package/dist/src/validation/stream.d.ts +13 -0
  72. package/dist/src/validation/stream.js +26 -0
  73. package/dist/src/validation/string.d.ts +29 -0
  74. package/dist/src/validation/string.js +51 -0
  75. package/dist/src/validation/union.d.ts +16 -0
  76. package/dist/src/validation/union.js +36 -0
  77. package/dist/src/validation/unknown.d.ts +10 -0
  78. package/dist/src/validation/unknown.js +13 -0
  79. package/dist/src/validation/uuid.d.ts +11 -0
  80. package/dist/src/validation/uuid.js +23 -0
  81. package/package.json +3 -4
@@ -0,0 +1,48 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ import type { Schema } from './schema.js';
3
+ /**
4
+ * Run a function while collecting lazy schema definitions.
5
+ * Any `LazySchema` whose `documentation()` is called during `fn`
6
+ * will register its full definition in the returned map.
7
+ */
8
+ export declare function collectLazySchemas<T>(fn: () => T): {
9
+ result: T;
10
+ schemas: Map<string, object>;
11
+ };
12
+ /**
13
+ * A schema that defers evaluation to support recursive definitions.
14
+ * The getter function is called lazily on each parse/documentation
15
+ * invocation, breaking the circular reference at definition time.
16
+ *
17
+ * Usage:
18
+ * ```typescript
19
+ * interface Category {
20
+ * name: string;
21
+ * subcategories: Category[];
22
+ * }
23
+ *
24
+ * const category: y.LazySchema<Category> = y.lazy("Category", () =>
25
+ * y.object({
26
+ * name: y.string(),
27
+ * subcategories: category.array(),
28
+ * }),
29
+ * );
30
+ * ```
31
+ */
32
+ export declare class LazySchema<T> extends ModifiableSchema<T> {
33
+ readonly schemaName: string;
34
+ private readonly getter;
35
+ constructor(name: string, getter: () => Schema<T>);
36
+ parse(obj: unknown): T;
37
+ documentation(): object;
38
+ private registerSchema;
39
+ isOptional(): boolean;
40
+ }
41
+ /**
42
+ * Create a lazily-evaluated schema for recursive type definitions.
43
+ * @param name - The schema name, used for `$ref` in OpenAPI documentation.
44
+ * @param getter - A function that returns the schema. Called at
45
+ * parse time, not at definition time, so circular references
46
+ * are safe.
47
+ */
48
+ export declare const lazy: <T>(name: string, getter: () => Schema<T>) => LazySchema<T>;
@@ -0,0 +1,78 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ /**
3
+ * Temporary context used during OpenAPI doc generation to accumulate
4
+ * lazy schema definitions. Set by `collectLazySchemas()`, read by
5
+ * `LazySchema.documentation()`. Not a persistent global registry —
6
+ * only lives for the duration of a single doc generation call.
7
+ */
8
+ let schemaCollector = null;
9
+ /**
10
+ * Run a function while collecting lazy schema definitions.
11
+ * Any `LazySchema` whose `documentation()` is called during `fn`
12
+ * will register its full definition in the returned map.
13
+ */
14
+ export function collectLazySchemas(fn) {
15
+ const schemas = new Map();
16
+ schemaCollector = schemas;
17
+ try {
18
+ const result = fn();
19
+ return { result, schemas };
20
+ }
21
+ finally {
22
+ schemaCollector = null;
23
+ }
24
+ }
25
+ /**
26
+ * A schema that defers evaluation to support recursive definitions.
27
+ * The getter function is called lazily on each parse/documentation
28
+ * invocation, breaking the circular reference at definition time.
29
+ *
30
+ * Usage:
31
+ * ```typescript
32
+ * interface Category {
33
+ * name: string;
34
+ * subcategories: Category[];
35
+ * }
36
+ *
37
+ * const category: y.LazySchema<Category> = y.lazy("Category", () =>
38
+ * y.object({
39
+ * name: y.string(),
40
+ * subcategories: category.array(),
41
+ * }),
42
+ * );
43
+ * ```
44
+ */
45
+ export class LazySchema extends ModifiableSchema {
46
+ constructor(name, getter) {
47
+ super();
48
+ this.schemaName = name;
49
+ this.getter = getter;
50
+ }
51
+ parse(obj) {
52
+ return this.getter().parse(obj);
53
+ }
54
+ documentation() {
55
+ if (schemaCollector && !schemaCollector.has(this.schemaName)) {
56
+ this.registerSchema(schemaCollector);
57
+ }
58
+ return { $ref: `#/components/schemas/${this.schemaName}` };
59
+ }
60
+ registerSchema(collector) {
61
+ // Set a placeholder first to break infinite recursion —
62
+ // if the getter references this schema, documentation()
63
+ // will see the key already exists and skip re-registration.
64
+ collector.set(this.schemaName, {});
65
+ collector.set(this.schemaName, this.getter().documentation());
66
+ }
67
+ isOptional() {
68
+ return this.getter().isOptional();
69
+ }
70
+ }
71
+ /**
72
+ * Create a lazily-evaluated schema for recursive type definitions.
73
+ * @param name - The schema name, used for `$ref` in OpenAPI documentation.
74
+ * @param getter - A function that returns the schema. Called at
75
+ * parse time, not at definition time, so circular references
76
+ * are safe.
77
+ */
78
+ export const lazy = (name, getter) => new LazySchema(name, getter);
@@ -0,0 +1,105 @@
1
+ import type { Typeof } from './body.js';
2
+ import { DocSchema } from './doc.js';
3
+ import { Schema } from './schema.js';
4
+ export declare abstract class ModifiableSchema<T> extends Schema<T> {
5
+ /**
6
+ * Mark this schema as optional.
7
+ */
8
+ optional(): OptionalSchema<T>;
9
+ /**
10
+ * Provide a default value. If the input is undefined or null, the default
11
+ * value is returned instead.
12
+ * @param value - The default value.
13
+ */
14
+ default(value: T): DefaultSchema<T>;
15
+ /**
16
+ * Add a custom validation predicate. The inner schema is parsed first,
17
+ * then the predicate is checked against the parsed value.
18
+ * @param fn - A predicate that returns true if the value is valid, or a
19
+ * string error message if it is not.
20
+ */
21
+ refine(fn: (value: T) => boolean | string, docs?: Record<string, unknown>): RefinedSchema<T>;
22
+ describe(description: string, example?: T): DocSchema<T>;
23
+ array(): ArraySchema<Schema<T>>;
24
+ }
25
+ declare class OptionalSchema<T> extends Schema<T | undefined> {
26
+ private schema;
27
+ constructor(schema: Schema<T>);
28
+ /**
29
+ * Add a description and example to the schema.
30
+ * @param doc - The documentation.
31
+ * @deprecated - Use .describe() instead.
32
+ */
33
+ doc(doc: {
34
+ description: string;
35
+ example?: T;
36
+ }): DocSchema<T | undefined>;
37
+ describe(description: string, example?: T): DocSchema<T | undefined>;
38
+ array(): ArraySchema<Schema<T | undefined>>;
39
+ parse(obj: unknown): T | undefined;
40
+ documentation(): object;
41
+ isOptional(): boolean;
42
+ }
43
+ declare class DefaultSchema<T> extends Schema<T> {
44
+ private readonly schema;
45
+ private readonly defaultValue;
46
+ constructor(schema: Schema<T>, defaultValue: T);
47
+ describe(description: string, example?: T): DocSchema<T>;
48
+ array(): ArraySchema<Schema<T>>;
49
+ parse(obj: unknown): T;
50
+ documentation(): object;
51
+ isOptional(): boolean;
52
+ }
53
+ export declare class RefinedSchema<T> extends ModifiableSchema<T> {
54
+ private readonly schema;
55
+ private readonly predicate;
56
+ private readonly docs?;
57
+ constructor(schema: Schema<T>, predicate: (value: T) => boolean | string, docs?: Record<string, unknown>);
58
+ /**
59
+ * Set the minimum length/items for strings and arrays.
60
+ * @param value - The minimum constraint.
61
+ */
62
+ min(value: number): RefinedSchema<T>;
63
+ /**
64
+ * Set the maximum length/items for strings and arrays.
65
+ * @param value - The maximum constraint.
66
+ */
67
+ max(value: number): RefinedSchema<T>;
68
+ /**
69
+ * Set the exact length/items for strings and arrays.
70
+ * @param value - The exact constraint.
71
+ */
72
+ length(value: number): RefinedSchema<T>;
73
+ parse(obj: unknown): T;
74
+ documentation(): object;
75
+ isOptional(): boolean;
76
+ }
77
+ export declare class ArraySchema<ItemSchema extends Schema<unknown>> extends ModifiableSchema<Typeof<ItemSchema>[]> {
78
+ private readonly itemSchema;
79
+ constructor(itemSchema: ItemSchema);
80
+ /**
81
+ * Set the minimum number of items for arrays.
82
+ * @param items - The minimum number of items.
83
+ */
84
+ min(items: number): RefinedSchema<Typeof<ItemSchema>[]>;
85
+ /**
86
+ * Set the maximum number of items for arrays.
87
+ * @param items - The maximum number of items.
88
+ */
89
+ max(items: number): RefinedSchema<Typeof<ItemSchema>[]>;
90
+ /**
91
+ * Set the exact number of items for arrays.
92
+ * This is equivalent to calling both min and max.
93
+ * @param items - The number of items.
94
+ */
95
+ length(items: number): RefinedSchema<Typeof<ItemSchema>[]>;
96
+ parse(obj: unknown): Typeof<ItemSchema>[];
97
+ documentation(): object;
98
+ }
99
+ /**
100
+ * A schema matching arrays of the provided item type.
101
+ * @param itemSchema - The schema for array items.
102
+ * @deprecated Use the .array() method instead.
103
+ */
104
+ export declare const array: <ItemSchema extends Schema<unknown>>(itemSchema: ItemSchema) => ArraySchema<ItemSchema>;
105
+ export {};
@@ -0,0 +1,223 @@
1
+ import { DocSchema } from './doc.js';
2
+ import { Issue, ValidationError } from './error.js';
3
+ import { Schema } from './schema.js';
4
+ export class ModifiableSchema extends Schema {
5
+ /**
6
+ * Mark this schema as optional.
7
+ */
8
+ optional() {
9
+ return new OptionalSchema(this);
10
+ }
11
+ /**
12
+ * Provide a default value. If the input is undefined or null, the default
13
+ * value is returned instead.
14
+ * @param value - The default value.
15
+ */
16
+ default(value) {
17
+ return new DefaultSchema(this, value);
18
+ }
19
+ /**
20
+ * Add a custom validation predicate. The inner schema is parsed first,
21
+ * then the predicate is checked against the parsed value.
22
+ * @param fn - A predicate that returns true if the value is valid, or a
23
+ * string error message if it is not.
24
+ */
25
+ refine(fn, docs) {
26
+ return new RefinedSchema(this, fn, docs);
27
+ }
28
+ describe(description, example) {
29
+ return new DocSchema(this, description, example);
30
+ }
31
+ array() {
32
+ return new ArraySchema(this);
33
+ }
34
+ }
35
+ class OptionalSchema extends Schema {
36
+ constructor(schema) {
37
+ super();
38
+ this.schema = schema;
39
+ }
40
+ /**
41
+ * Add a description and example to the schema.
42
+ * @param doc - The documentation.
43
+ * @deprecated - Use .describe() instead.
44
+ */
45
+ doc(doc) {
46
+ return new DocSchema(this, doc.description, doc.example);
47
+ }
48
+ describe(description, example) {
49
+ return new DocSchema(this, description, example);
50
+ }
51
+ array() {
52
+ return new ArraySchema(this);
53
+ }
54
+ parse(obj) {
55
+ if (obj === undefined) {
56
+ return undefined;
57
+ }
58
+ return this.schema.parse(obj);
59
+ }
60
+ documentation() {
61
+ return this.schema.documentation();
62
+ }
63
+ isOptional() {
64
+ return true;
65
+ }
66
+ }
67
+ class DefaultSchema extends Schema {
68
+ constructor(schema, defaultValue) {
69
+ super();
70
+ this.schema = schema;
71
+ this.defaultValue = defaultValue;
72
+ }
73
+ describe(description, example) {
74
+ return new DocSchema(this, description, example);
75
+ }
76
+ array() {
77
+ return new ArraySchema(this);
78
+ }
79
+ parse(obj) {
80
+ if (obj === undefined || obj === null) {
81
+ return this.defaultValue;
82
+ }
83
+ return this.schema.parse(obj);
84
+ }
85
+ documentation() {
86
+ return {
87
+ ...this.schema.documentation(),
88
+ default: this.defaultValue,
89
+ };
90
+ }
91
+ isOptional() {
92
+ return true;
93
+ }
94
+ }
95
+ export class RefinedSchema extends ModifiableSchema {
96
+ constructor(schema, predicate, docs) {
97
+ super();
98
+ this.schema = schema;
99
+ this.predicate = predicate;
100
+ this.docs = docs;
101
+ }
102
+ /**
103
+ * Set the minimum length/items for strings and arrays.
104
+ * @param value - The minimum constraint.
105
+ */
106
+ min(value) {
107
+ const docs = this.documentation();
108
+ const isArray = docs.type === 'array';
109
+ return this.refine(((v) => {
110
+ const len = v.length;
111
+ return (len >= value ||
112
+ (isArray
113
+ ? `Must have at least ${value} items`
114
+ : `Must be at least ${value} characters`));
115
+ }), isArray ? { minItems: value } : { minLength: value });
116
+ }
117
+ /**
118
+ * Set the maximum length/items for strings and arrays.
119
+ * @param value - The maximum constraint.
120
+ */
121
+ max(value) {
122
+ const docs = this.documentation();
123
+ const isArray = docs.type === 'array';
124
+ return this.refine(((v) => {
125
+ const len = v.length;
126
+ return (len <= value ||
127
+ (isArray
128
+ ? `Must have at most ${value} items`
129
+ : `Must be at most ${value} characters`));
130
+ }), isArray ? { maxItems: value } : { maxLength: value });
131
+ }
132
+ /**
133
+ * Set the exact length/items for strings and arrays.
134
+ * @param value - The exact constraint.
135
+ */
136
+ length(value) {
137
+ return this.min(value).max(value);
138
+ }
139
+ parse(obj) {
140
+ const parsed = this.schema.parse(obj);
141
+ const result = this.predicate(parsed);
142
+ if (result === true) {
143
+ return parsed;
144
+ }
145
+ const message = typeof result === 'string' ? result : 'Validation failed';
146
+ throw new ValidationError([new Issue([], message)]);
147
+ }
148
+ documentation() {
149
+ return {
150
+ ...this.schema.documentation(),
151
+ ...this.docs,
152
+ };
153
+ }
154
+ isOptional() {
155
+ return this.schema.isOptional();
156
+ }
157
+ }
158
+ export class ArraySchema extends ModifiableSchema {
159
+ constructor(itemSchema) {
160
+ super();
161
+ this.itemSchema = itemSchema;
162
+ }
163
+ /**
164
+ * Set the minimum number of items for arrays.
165
+ * @param items - The minimum number of items.
166
+ */
167
+ min(items) {
168
+ return this.refine((arr) => arr.length >= items || `Must have at least ${items} items`, { minItems: items });
169
+ }
170
+ /**
171
+ * Set the maximum number of items for arrays.
172
+ * @param items - The maximum number of items.
173
+ */
174
+ max(items) {
175
+ return this.refine((arr) => arr.length <= items || `Must have at most ${items} items`, { maxItems: items });
176
+ }
177
+ /**
178
+ * Set the exact number of items for arrays.
179
+ * This is equivalent to calling both min and max.
180
+ * @param items - The number of items.
181
+ */
182
+ length(items) {
183
+ return this.min(items).refine((arr) => arr.length <= items || `Must have at most ${items} items`, { maxItems: items });
184
+ }
185
+ parse(obj) {
186
+ if (!Array.isArray(obj)) {
187
+ throw new ValidationError([
188
+ new Issue([], `Expected array but got ${typeof obj}`),
189
+ ]);
190
+ }
191
+ const elems = [];
192
+ const issues = [];
193
+ for (let i = 0; i < obj.length; ++i) {
194
+ try {
195
+ elems.push(this.itemSchema.parse(obj[i]));
196
+ }
197
+ catch (error) {
198
+ if (error instanceof ValidationError) {
199
+ issues.push(...error.withPrefix(i.toString()));
200
+ }
201
+ else {
202
+ throw error;
203
+ }
204
+ }
205
+ }
206
+ if (issues.length > 0) {
207
+ throw new ValidationError(issues);
208
+ }
209
+ return elems;
210
+ }
211
+ documentation() {
212
+ return {
213
+ type: 'array',
214
+ items: this.itemSchema.documentation(),
215
+ };
216
+ }
217
+ }
218
+ /**
219
+ * A schema matching arrays of the provided item type.
220
+ * @param itemSchema - The schema for array items.
221
+ * @deprecated Use the .array() method instead.
222
+ */
223
+ export const array = (itemSchema) => new ArraySchema(itemSchema);
@@ -0,0 +1,10 @@
1
+ import type { Readable } from 'node:stream';
2
+ import { BodyType } from './body.js';
3
+ export declare class NoneBody extends BodyType<undefined, undefined> {
4
+ deserialize(_stream: Readable, _contentType: string): Promise<undefined>;
5
+ bodyDocs(): object;
6
+ }
7
+ /**
8
+ * Accepts no input body.
9
+ */
10
+ export declare const none: () => NoneBody;
@@ -0,0 +1,14 @@
1
+ import { BodyType } from './body.js';
2
+ export class NoneBody extends BodyType {
3
+ deserialize(_stream, _contentType) {
4
+ return Promise.resolve(undefined);
5
+ }
6
+ bodyDocs() {
7
+ // TODO
8
+ return {};
9
+ }
10
+ }
11
+ /**
12
+ * Accepts no input body.
13
+ */
14
+ export const none = () => new NoneBody();
@@ -0,0 +1,10 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ declare class NullSchema extends ModifiableSchema<null> {
3
+ parse(obj: unknown): null;
4
+ documentation(): object;
5
+ }
6
+ /**
7
+ * A schema that matches only null.
8
+ */
9
+ export declare const _null: () => NullSchema;
10
+ export {};
@@ -0,0 +1,21 @@
1
+ import { Issue, ValidationError } from './error.js';
2
+ import { ModifiableSchema } from './modifiable.js';
3
+ class NullSchema extends ModifiableSchema {
4
+ parse(obj) {
5
+ if (obj !== null) {
6
+ throw new ValidationError([
7
+ new Issue([], `Expected null but got ${typeof obj}`),
8
+ ]);
9
+ }
10
+ return null;
11
+ }
12
+ documentation() {
13
+ return {
14
+ type: 'null',
15
+ };
16
+ }
17
+ }
18
+ /**
19
+ * A schema that matches only null.
20
+ */
21
+ export const _null = () => new NullSchema();
@@ -0,0 +1,23 @@
1
+ import { ModifiableSchema } from './modifiable.js';
2
+ declare class NumberSchema extends ModifiableSchema<number> {
3
+ private readonly minValue?;
4
+ private readonly maxValue?;
5
+ constructor(min?: number, max?: number);
6
+ /**
7
+ * Set the minimum value the number is allowed to be.
8
+ * @param value - The minimum value.
9
+ */
10
+ min(value: number): NumberSchema;
11
+ /**
12
+ * Set the maximum value the number is allowed to be.
13
+ * @param value - The maximum value.
14
+ */
15
+ max(value: number): NumberSchema;
16
+ parse(obj: unknown): number;
17
+ documentation(): object;
18
+ }
19
+ /**
20
+ * A schema that matches a number.
21
+ */
22
+ export declare const number: () => NumberSchema;
23
+ export {};
@@ -0,0 +1,58 @@
1
+ import { Issue, ValidationError } from './error.js';
2
+ import { ModifiableSchema } from './modifiable.js';
3
+ class NumberSchema extends ModifiableSchema {
4
+ constructor(min, max) {
5
+ super();
6
+ this.minValue = min;
7
+ this.maxValue = max;
8
+ }
9
+ /**
10
+ * Set the minimum value the number is allowed to be.
11
+ * @param value - The minimum value.
12
+ */
13
+ min(value) {
14
+ return new NumberSchema(value, this.maxValue);
15
+ }
16
+ /**
17
+ * Set the maximum value the number is allowed to be.
18
+ * @param value - The maximum value.
19
+ */
20
+ max(value) {
21
+ return new NumberSchema(this.minValue, value);
22
+ }
23
+ parse(obj) {
24
+ if (typeof obj !== 'number' && typeof obj !== 'string') {
25
+ throw new ValidationError([
26
+ new Issue([], `Expected number but got ${typeof obj}`),
27
+ ]);
28
+ }
29
+ const num = typeof obj === 'number' ? obj : Number.parseFloat(obj);
30
+ if (Number.isNaN(num)) {
31
+ throw new ValidationError([
32
+ new Issue([], `Expected number but got ${typeof obj}`),
33
+ ]);
34
+ }
35
+ if (this.minValue !== undefined && num < this.minValue) {
36
+ throw new ValidationError([
37
+ new Issue([], `Must be at least ${this.minValue}, but was ${num}`),
38
+ ]);
39
+ }
40
+ if (this.maxValue !== undefined && num > this.maxValue) {
41
+ throw new ValidationError([
42
+ new Issue([], `Must be at most ${this.maxValue}, but was ${num}`),
43
+ ]);
44
+ }
45
+ return num;
46
+ }
47
+ documentation() {
48
+ return {
49
+ type: 'number',
50
+ ...(this.minValue !== undefined && { minimum: this.minValue }),
51
+ ...(this.maxValue !== undefined && { maximum: this.maxValue }),
52
+ };
53
+ }
54
+ }
55
+ /**
56
+ * A schema that matches a number.
57
+ */
58
+ export const number = () => new NumberSchema();
@@ -0,0 +1,38 @@
1
+ import type { Typeof } from './body.js';
2
+ import { ModifiableSchema } from './modifiable.js';
3
+ import { Schema } from './schema.js';
4
+ /**
5
+ * Make a union of all keys that are not extended by undefined
6
+ * (i.e. have undefined as a variant) of the specified type.
7
+ */
8
+ type RequiredKeys<T> = {
9
+ [K in keyof T]: undefined extends T[K] ? never : K;
10
+ }[keyof T];
11
+ /**
12
+ * Make all fields that have undefined as a variant optional.
13
+ */
14
+ type MakeFieldsOptional<T> = Pick<T, RequiredKeys<T>> & Partial<T>;
15
+ export declare class ObjectSchema<Shape extends Record<string, Schema<unknown>>> extends ModifiableSchema<MakeFieldsOptional<{
16
+ [K in keyof Shape]: Typeof<Shape[K]>;
17
+ }>> {
18
+ readonly shape: Shape;
19
+ private lax;
20
+ constructor(shape: Shape, lax: boolean);
21
+ parse(obj: unknown): MakeFieldsOptional<{
22
+ [K in keyof Shape]: Typeof<Shape[K]>;
23
+ }>;
24
+ documentation(): object;
25
+ }
26
+ /**
27
+ * A schema matching a JavaScript object of the specified shape. Fields which
28
+ * can be undefined are automatically marked as optional.
29
+ * @param shape - The object shape.
30
+ *
31
+ * ```typescript
32
+ * const schema = y.object({ num: y.number(), str: y.string().optional() });
33
+ * type SchemaType = y.Typeof<typeof schema>; // { num: number, str?: string }
34
+ * ```
35
+ */
36
+ export declare const object: <Shape extends Record<string, Schema<unknown>>>(shape: Shape) => ObjectSchema<Shape>;
37
+ export declare const laxObject: <Shape extends Record<string, Schema<unknown>>>(shape: Shape) => ObjectSchema<Shape>;
38
+ export {};