yedra 0.20.3 → 0.20.4

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.
@@ -55,6 +55,21 @@ export declare class RefinedSchema<T> extends ModifiableSchema<T> {
55
55
  private readonly predicate;
56
56
  private readonly docs?;
57
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>;
58
73
  parse(obj: unknown): T;
59
74
  documentation(): object;
60
75
  isOptional(): boolean;
@@ -99,6 +99,43 @@ export class RefinedSchema extends ModifiableSchema {
99
99
  this.predicate = predicate;
100
100
  this.docs = docs;
101
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
+ }
102
139
  parse(obj) {
103
140
  const parsed = this.schema.parse(obj);
104
141
  const result = this.predicate(parsed);
@@ -1,6 +1,6 @@
1
- import type { Readable } from 'node:stream';
2
- import type { StandardSchemaV1 } from '@standard-schema/spec';
3
- import { BodyType } from './body.js';
1
+ import type { Readable } from "node:stream";
2
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import { BodyType } from "./body.js";
4
4
  /**
5
5
  * The base class for all schemas.
6
6
  */
@@ -31,5 +31,5 @@ export declare abstract class Schema<T> extends BodyType<T, T> implements Standa
31
31
  *
32
32
  * @see https://standardschema.dev/
33
33
  */
34
- get '~standard'(): StandardSchemaV1.Props<T, T>;
34
+ get "~standard"(): StandardSchemaV1.Props<T, T>;
35
35
  }
@@ -1,5 +1,5 @@
1
- import { BodyType } from './body.js';
2
- import { Issue, ValidationError } from './error.js';
1
+ import { BodyType } from "./body.js";
2
+ import { Issue, ValidationError } from "./error.js";
3
3
  /**
4
4
  * The base class for all schemas.
5
5
  */
@@ -8,22 +8,22 @@ export class Schema extends BodyType {
8
8
  // Lazy import to keep this module browser-safe for yedra/schema.
9
9
  // deserialize() is only called server-side, so the Node-specific
10
10
  // stream utility is never resolved when bundled for the frontend.
11
- const { readableToBuffer } = await import('../util/stream.js');
11
+ const { readableToBuffer } = await import("../util/stream.js");
12
12
  const buffer = await readableToBuffer(stream);
13
13
  if (buffer.length === 0) {
14
14
  return this.parse({});
15
15
  }
16
- if (contentType !== 'application/json') {
16
+ if (contentType !== "application/json") {
17
17
  throw new ValidationError([
18
18
  new Issue([], `Expected content type \`application/json\`, but got \`${contentType}\``),
19
19
  ]);
20
20
  }
21
- const data = JSON.parse(Buffer.from(buffer).toString('utf8'));
21
+ const data = JSON.parse(Buffer.from(buffer).toString("utf8"));
22
22
  return this.parse(data);
23
23
  }
24
24
  bodyDocs() {
25
25
  return {
26
- 'application/json': {
26
+ "application/json": {
27
27
  schema: this.documentation(),
28
28
  },
29
29
  };
@@ -44,10 +44,11 @@ export class Schema extends BodyType {
44
44
  *
45
45
  * @see https://standardschema.dev/
46
46
  */
47
- get '~standard'() {
47
+ get "~standard"() {
48
48
  return {
49
49
  version: 1,
50
- vendor: 'yedra',
50
+ vendor: "yedra",
51
+ types: {},
51
52
  // Wraps parse() to match Standard Schema's non-throwing convention:
52
53
  // returns { value } on success, { issues } on failure.
53
54
  validate: (value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yedra",
3
- "version": "0.20.3",
3
+ "version": "0.20.4",
4
4
  "repository": "github:0codekit/yedra",
5
5
  "main": "dist/index.js",
6
6
  "exports": {