yedra 0.15.5 → 0.15.7

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/lib.d.ts CHANGED
@@ -2,7 +2,7 @@ export { HttpError, BadRequestError, UnauthorizedError, PaymentRequiredError, Fo
2
2
  export { Log } from './routing/log.js';
3
3
  export declare const validatePath: (path: string) => void;
4
4
  export { parseEnv } from './routing/env.js';
5
- export { array } from './validation/array.js';
5
+ export { array } from './validation/modifiable.js';
6
6
  export { boolean } from './validation/boolean.js';
7
7
  export { date } from './validation/date.js';
8
8
  export { _enum as enum } from './validation/enum.js';
package/dist/lib.js CHANGED
@@ -7,7 +7,7 @@ export const validatePath = (path) => {
7
7
  };
8
8
  export { parseEnv } from './routing/env.js';
9
9
  // validation
10
- export { array } from './validation/array.js';
10
+ export { array } from './validation/modifiable.js';
11
11
  export { boolean } from './validation/boolean.js';
12
12
  export { date } from './validation/date.js';
13
13
  export { _enum as enum } from './validation/enum.js';
@@ -1,6 +1,7 @@
1
1
  import { ModifiableSchema } from './modifiable.js';
2
2
  declare class EnumSchema<T extends [...(string | number)[]]> extends ModifiableSchema<T[number]> {
3
3
  private readonly options;
4
+ private readonly normalized;
4
5
  constructor(options: T);
5
6
  parse(obj: unknown): T[number];
6
7
  documentation(): object;
@@ -4,17 +4,26 @@ class EnumSchema extends ModifiableSchema {
4
4
  constructor(options) {
5
5
  super();
6
6
  this.options = options;
7
+ this.normalized = options.map((option) => option.toString());
7
8
  }
8
9
  parse(obj) {
9
- if (!obj ||
10
- (typeof obj !== 'string' && typeof obj !== 'number') ||
11
- !this.options.includes(obj)) {
12
- const actual = typeof obj === 'string' || typeof obj === 'number' ? obj : typeof obj;
10
+ if (typeof obj !== 'string' && typeof obj !== 'number') {
11
+ // enum objects can only be strings or numbers
13
12
  throw new ValidationError([
14
- new Issue([], `Expected one of ${this.options.join(', ')} but got ${actual}`),
13
+ new Issue([], `Expected one of ${this.options.join(', ')} but got ${typeof obj}`),
15
14
  ]);
16
15
  }
17
- return obj;
16
+ // compare only the stringified (normalized) values
17
+ const normalizedObj = obj.toString();
18
+ const index = this.normalized.findIndex((value) => value === normalizedObj);
19
+ if (index === -1) {
20
+ // invalid value
21
+ throw new ValidationError([
22
+ new Issue([], `Expected one of ${this.options.join(', ')} but got ${obj}`),
23
+ ]);
24
+ }
25
+ // return the un-normalized value
26
+ return this.options[index];
18
27
  }
19
28
  documentation() {
20
29
  return {
@@ -1,4 +1,4 @@
1
- import type { ArraySchema } from './array.js';
1
+ import type { Typeof } from './body.js';
2
2
  import { DocSchema } from './doc.js';
3
3
  import { Schema } from './schema.js';
4
4
  export declare abstract class ModifiableSchema<T> extends Schema<T> {
@@ -27,4 +27,34 @@ declare class OptionalSchema<T> extends Schema<T | undefined> {
27
27
  documentation(): object;
28
28
  isOptional(): boolean;
29
29
  }
30
+ export declare class ArraySchema<ItemSchema extends Schema<unknown>> extends ModifiableSchema<Typeof<ItemSchema>[]> {
31
+ private readonly itemSchema;
32
+ private readonly minItems?;
33
+ private readonly maxItems?;
34
+ constructor(itemSchema: ItemSchema, minItems?: number, maxItems?: number);
35
+ /**
36
+ * Set the minimum number of items for arrays.
37
+ * @param items - The minimum number of items.
38
+ */
39
+ min(items: number): ArraySchema<ItemSchema>;
40
+ /**
41
+ * Set the maximum number of items for arrays.
42
+ * @param items - The maximum number of items.
43
+ */
44
+ max(items: number): ArraySchema<ItemSchema>;
45
+ /**
46
+ * Set the exact number of items for arrays.
47
+ * This is equivalent to calling both min and max.
48
+ * @param items - The number of items.
49
+ */
50
+ length(items: number): ArraySchema<ItemSchema>;
51
+ parse(obj: unknown): Typeof<ItemSchema>[];
52
+ documentation(): object;
53
+ }
54
+ /**
55
+ * A schema matching arrays of the provided item type.
56
+ * @param itemSchema - The schema for array items.
57
+ * @deprecated Use the .array() method instead.
58
+ */
59
+ export declare const array: <ItemSchema extends Schema<unknown>>(itemSchema: ItemSchema) => ArraySchema<ItemSchema>;
30
60
  export {};
@@ -1,4 +1,5 @@
1
1
  import { DocSchema } from './doc.js';
2
+ import { Issue, ValidationError } from './error.js';
2
3
  import { Schema } from './schema.js';
3
4
  export class ModifiableSchema extends Schema {
4
5
  /**
@@ -11,7 +12,7 @@ export class ModifiableSchema extends Schema {
11
12
  return new DocSchema(this, description, example);
12
13
  }
13
14
  array() {
14
- return new ArraySchemaModule.ArraySchema(this);
15
+ return new ArraySchema(this);
15
16
  }
16
17
  }
17
18
  class OptionalSchema extends Schema {
@@ -31,7 +32,7 @@ class OptionalSchema extends Schema {
31
32
  return new DocSchema(this, description, example);
32
33
  }
33
34
  array() {
34
- return new ArraySchemaModule.ArraySchema(this);
35
+ return new ArraySchema(this);
35
36
  }
36
37
  parse(obj) {
37
38
  if (obj === undefined || obj === null) {
@@ -46,4 +47,83 @@ class OptionalSchema extends Schema {
46
47
  return true;
47
48
  }
48
49
  }
49
- const ArraySchemaModule = await import('./array.js');
50
+ export class ArraySchema extends ModifiableSchema {
51
+ constructor(itemSchema, minItems, maxItems) {
52
+ super();
53
+ this.itemSchema = itemSchema;
54
+ this.minItems = minItems;
55
+ this.maxItems = maxItems;
56
+ }
57
+ /**
58
+ * Set the minimum number of items for arrays.
59
+ * @param items - The minimum number of items.
60
+ */
61
+ min(items) {
62
+ return new ArraySchema(this.itemSchema, items, this.maxItems);
63
+ }
64
+ /**
65
+ * Set the maximum number of items for arrays.
66
+ * @param items - The maximum number of items.
67
+ */
68
+ max(items) {
69
+ return new ArraySchema(this.itemSchema, this.minItems, items);
70
+ }
71
+ /**
72
+ * Set the exact number of items for arrays.
73
+ * This is equivalent to calling both min and max.
74
+ * @param items - The number of items.
75
+ */
76
+ length(items) {
77
+ return new ArraySchema(this.itemSchema, items, items);
78
+ }
79
+ parse(obj) {
80
+ if (!Array.isArray(obj)) {
81
+ throw new ValidationError([
82
+ new Issue([], `Expected array but got ${typeof obj}`),
83
+ ]);
84
+ }
85
+ if (this.minItems && obj.length < this.minItems) {
86
+ throw new ValidationError([
87
+ new Issue([], `Must have at least ${this.minItems} items, but has ${obj.length}`),
88
+ ]);
89
+ }
90
+ if (this.maxItems && obj.length > this.maxItems) {
91
+ throw new ValidationError([
92
+ new Issue([], `Must have at most ${this.maxItems} items, but has ${obj.length}`),
93
+ ]);
94
+ }
95
+ const elems = [];
96
+ const issues = [];
97
+ for (let i = 0; i < obj.length; ++i) {
98
+ try {
99
+ elems.push(this.itemSchema.parse(obj[i]));
100
+ }
101
+ catch (error) {
102
+ if (error instanceof ValidationError) {
103
+ issues.push(...error.withPrefix(i.toString()));
104
+ }
105
+ else {
106
+ throw error;
107
+ }
108
+ }
109
+ }
110
+ if (issues.length > 0) {
111
+ throw new ValidationError(issues);
112
+ }
113
+ return elems;
114
+ }
115
+ documentation() {
116
+ return {
117
+ type: 'array',
118
+ items: this.itemSchema.documentation(),
119
+ minItems: this.minItems,
120
+ maxItems: this.maxItems,
121
+ };
122
+ }
123
+ }
124
+ /**
125
+ * A schema matching arrays of the provided item type.
126
+ * @param itemSchema - The schema for array items.
127
+ * @deprecated Use the .array() method instead.
128
+ */
129
+ export const array = (itemSchema) => new ArraySchema(itemSchema);
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "yedra",
3
- "version": "0.15.5",
3
+ "version": "0.15.7",
4
4
  "repository": "github:0codekit/yedra",
5
5
  "main": "dist/index.js",
6
6
  "devDependencies": {
7
7
  "@biomejs/biome": "^1.9.4",
8
8
  "@types/bun": "^1.2.16",
9
- "@types/node": "^24.0.1",
9
+ "@types/node": "^24.0.3",
10
10
  "@types/uuid": "^10.0.0",
11
11
  "@types/ws": "^8.18.1",
12
12
  "typescript": "^5.8.3"
@@ -1,33 +0,0 @@
1
- import type { Typeof } from './body.js';
2
- import { ModifiableSchema } from './modifiable.js';
3
- import type { Schema } from './schema.js';
4
- export declare class ArraySchema<ItemSchema extends Schema<unknown>> extends ModifiableSchema<Typeof<ItemSchema>[]> {
5
- private readonly itemSchema;
6
- private readonly minItems?;
7
- private readonly maxItems?;
8
- constructor(itemSchema: ItemSchema, minItems?: number, maxItems?: number);
9
- /**
10
- * Set the minimum number of items for arrays.
11
- * @param items - The minimum number of items.
12
- */
13
- min(items: number): ArraySchema<ItemSchema>;
14
- /**
15
- * Set the maximum number of items for arrays.
16
- * @param items - The maximum number of items.
17
- */
18
- max(items: number): ArraySchema<ItemSchema>;
19
- /**
20
- * Set the exact number of items for arrays.
21
- * This is equivalent to calling both min and max.
22
- * @param items - The number of items.
23
- */
24
- length(items: number): ArraySchema<ItemSchema>;
25
- parse(obj: unknown): Typeof<ItemSchema>[];
26
- documentation(): object;
27
- }
28
- /**
29
- * A schema matching arrays of the provided item type.
30
- * @param itemSchema - The schema for array items.
31
- * @deprecated Use the .array() method instead.
32
- */
33
- export declare const array: <ItemSchema extends Schema<unknown>>(itemSchema: ItemSchema) => ArraySchema<ItemSchema>;
@@ -1,82 +0,0 @@
1
- import { Issue, ValidationError } from './error.js';
2
- import { ModifiableSchema } from './modifiable.js';
3
- export class ArraySchema extends ModifiableSchema {
4
- constructor(itemSchema, minItems, maxItems) {
5
- super();
6
- this.itemSchema = itemSchema;
7
- this.minItems = minItems;
8
- this.maxItems = maxItems;
9
- }
10
- /**
11
- * Set the minimum number of items for arrays.
12
- * @param items - The minimum number of items.
13
- */
14
- min(items) {
15
- return new ArraySchema(this.itemSchema, items, this.maxItems);
16
- }
17
- /**
18
- * Set the maximum number of items for arrays.
19
- * @param items - The maximum number of items.
20
- */
21
- max(items) {
22
- return new ArraySchema(this.itemSchema, this.minItems, items);
23
- }
24
- /**
25
- * Set the exact number of items for arrays.
26
- * This is equivalent to calling both min and max.
27
- * @param items - The number of items.
28
- */
29
- length(items) {
30
- return new ArraySchema(this.itemSchema, items, items);
31
- }
32
- parse(obj) {
33
- if (!Array.isArray(obj)) {
34
- throw new ValidationError([
35
- new Issue([], `Expected array but got ${typeof obj}`),
36
- ]);
37
- }
38
- if (this.minItems && obj.length < this.minItems) {
39
- throw new ValidationError([
40
- new Issue([], `Must have at least ${this.minItems} items, but has ${obj.length}`),
41
- ]);
42
- }
43
- if (this.maxItems && obj.length > this.maxItems) {
44
- throw new ValidationError([
45
- new Issue([], `Must have at most ${this.maxItems} items, but has ${obj.length}`),
46
- ]);
47
- }
48
- const elems = [];
49
- const issues = [];
50
- for (let i = 0; i < obj.length; ++i) {
51
- try {
52
- elems.push(this.itemSchema.parse(obj[i]));
53
- }
54
- catch (error) {
55
- if (error instanceof ValidationError) {
56
- issues.push(...error.withPrefix(i.toString()));
57
- }
58
- else {
59
- throw error;
60
- }
61
- }
62
- }
63
- if (issues.length > 0) {
64
- throw new ValidationError(issues);
65
- }
66
- return elems;
67
- }
68
- documentation() {
69
- return {
70
- type: 'array',
71
- items: this.itemSchema.documentation(),
72
- minItems: this.minItems,
73
- maxItems: this.maxItems,
74
- };
75
- }
76
- }
77
- /**
78
- * A schema matching arrays of the provided item type.
79
- * @param itemSchema - The schema for array items.
80
- * @deprecated Use the .array() method instead.
81
- */
82
- export const array = (itemSchema) => new ArraySchema(itemSchema);