forgeframe 0.0.1

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.
@@ -0,0 +1,636 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Lightweight prop schema builders for ForgeFrame.
4
+ *
5
+ * @remarks
6
+ * This module provides a fluent, Zod-like API for defining prop schemas.
7
+ * All schemas implement StandardSchemaV1, enabling seamless integration
8
+ * with ForgeFrame's validation pipeline and compatibility with external
9
+ * schema libraries.
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import ForgeFrame, { prop } from 'forgeframe';
14
+ *
15
+ * const MyComponent = ForgeFrame.create({
16
+ * tag: 'my-component',
17
+ * url: '/component',
18
+ * props: {
19
+ * name: prop.string(),
20
+ * count: prop.number().default(0),
21
+ * onSubmit: prop.function<(data: unknown) => void>().optional(),
22
+ * },
23
+ * });
24
+ * ```
25
+ */
26
+ import type { StandardSchemaV1, StandardSchemaV1Props, StandardSchemaV1Result } from './schema';
27
+ /**
28
+ * Abstract base class for all prop schemas.
29
+ *
30
+ * @remarks
31
+ * Implements StandardSchemaV1 for compatibility with ForgeFrame's validation
32
+ * system and external schema libraries like Zod, Valibot, and ArkType.
33
+ *
34
+ * @typeParam T - The output type after validation
35
+ *
36
+ * @public
37
+ */
38
+ export declare abstract class PropSchema<T> implements StandardSchemaV1<unknown, T> {
39
+ /** @internal */
40
+ protected _optional: boolean;
41
+ /** @internal */
42
+ protected _nullable: boolean;
43
+ /** @internal */
44
+ protected _default?: T | (() => T);
45
+ /**
46
+ * StandardSchemaV1 implementation.
47
+ * @internal
48
+ */
49
+ readonly '~standard': StandardSchemaV1Props<unknown, T>;
50
+ /**
51
+ * Validates a non-undefined value.
52
+ * @internal
53
+ */
54
+ protected abstract _validate(value: unknown): StandardSchemaV1Result<T>;
55
+ /**
56
+ * Marks this prop as optional.
57
+ *
58
+ * @returns Schema that accepts undefined
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * props: {
63
+ * nickname: prop.string().optional(),
64
+ * }
65
+ * ```
66
+ */
67
+ optional(): PropSchema<T | undefined>;
68
+ /**
69
+ * Marks this prop as nullable (accepts null).
70
+ *
71
+ * @returns Schema that accepts null
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * props: {
76
+ * middleName: prop.string().nullable(),
77
+ * }
78
+ * ```
79
+ */
80
+ nullable(): PropSchema<T | null>;
81
+ /**
82
+ * Sets a default value for this prop.
83
+ *
84
+ * @param value - Default value or function returning default
85
+ * @returns Schema with default value
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * props: {
90
+ * count: prop.number().default(0),
91
+ * id: prop.string().default(() => crypto.randomUUID()),
92
+ * }
93
+ * ```
94
+ */
95
+ default(value: T | (() => T)): PropSchema<T>;
96
+ /**
97
+ * Creates a shallow clone of this schema.
98
+ * @internal
99
+ */
100
+ protected abstract _clone(): PropSchema<T>;
101
+ }
102
+ /**
103
+ * Schema for string props with optional validation constraints.
104
+ *
105
+ * @public
106
+ */
107
+ export declare class StringSchema extends PropSchema<string> {
108
+ /** @internal */
109
+ private _minLength?;
110
+ /** @internal */
111
+ private _maxLength?;
112
+ /** @internal */
113
+ private _pattern?;
114
+ /** @internal */
115
+ private _patternMessage?;
116
+ /** @internal */
117
+ private _trim;
118
+ /** @internal */
119
+ protected _validate(value: unknown): StandardSchemaV1Result<string>;
120
+ /** @internal */
121
+ protected _clone(): StringSchema;
122
+ /**
123
+ * Requires minimum string length.
124
+ *
125
+ * @param length - Minimum number of characters
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * name: prop.string().min(2)
130
+ * ```
131
+ */
132
+ min(length: number): StringSchema;
133
+ /**
134
+ * Requires maximum string length.
135
+ *
136
+ * @param length - Maximum number of characters
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * bio: prop.string().max(500)
141
+ * ```
142
+ */
143
+ max(length: number): StringSchema;
144
+ /**
145
+ * Requires exact string length.
146
+ *
147
+ * @param length - Exact number of characters required
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * code: prop.string().length(6)
152
+ * ```
153
+ */
154
+ length(length: number): StringSchema;
155
+ /**
156
+ * Requires string to match a regex pattern.
157
+ *
158
+ * @param regex - Pattern to match
159
+ * @param message - Optional custom error message
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * slug: prop.string().pattern(/^[a-z0-9-]+$/, 'Invalid slug format')
164
+ * ```
165
+ */
166
+ pattern(regex: RegExp, message?: string): StringSchema;
167
+ /**
168
+ * Validates as email address.
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * email: prop.string().email()
173
+ * ```
174
+ */
175
+ email(): StringSchema;
176
+ /**
177
+ * Validates as URL.
178
+ *
179
+ * @example
180
+ * ```typescript
181
+ * website: prop.string().url()
182
+ * ```
183
+ */
184
+ url(): StringSchema;
185
+ /**
186
+ * Validates as UUID.
187
+ *
188
+ * @example
189
+ * ```typescript
190
+ * id: prop.string().uuid()
191
+ * ```
192
+ */
193
+ uuid(): StringSchema;
194
+ /**
195
+ * Trims whitespace from both ends of the string.
196
+ *
197
+ * @remarks
198
+ * The trimmed value is used for validation and returned as the result.
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * name: prop.string().trim()
203
+ * username: prop.string().trim().min(3)
204
+ * ```
205
+ */
206
+ trim(): StringSchema;
207
+ /**
208
+ * Requires non-empty string (at least 1 character).
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * title: prop.string().nonempty()
213
+ * ```
214
+ */
215
+ nonempty(): StringSchema;
216
+ }
217
+ /**
218
+ * Schema for number props with optional validation constraints.
219
+ *
220
+ * @public
221
+ */
222
+ export declare class NumberSchema extends PropSchema<number> {
223
+ /** @internal */
224
+ private _min?;
225
+ /** @internal */
226
+ private _max?;
227
+ /** @internal */
228
+ private _int;
229
+ /** @internal */
230
+ protected _validate(value: unknown): StandardSchemaV1Result<number>;
231
+ /** @internal */
232
+ protected _clone(): NumberSchema;
233
+ /**
234
+ * Requires minimum value.
235
+ *
236
+ * @param n - Minimum value (inclusive)
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * age: prop.number().min(0)
241
+ * ```
242
+ */
243
+ min(n: number): NumberSchema;
244
+ /**
245
+ * Requires maximum value.
246
+ *
247
+ * @param n - Maximum value (inclusive)
248
+ *
249
+ * @example
250
+ * ```typescript
251
+ * rating: prop.number().max(5)
252
+ * ```
253
+ */
254
+ max(n: number): NumberSchema;
255
+ /**
256
+ * Requires integer value.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * count: prop.number().int()
261
+ * ```
262
+ */
263
+ int(): NumberSchema;
264
+ /**
265
+ * Requires positive number (> 0).
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * price: prop.number().positive()
270
+ * ```
271
+ */
272
+ positive(): NumberSchema;
273
+ /**
274
+ * Requires non-negative number (>= 0).
275
+ *
276
+ * @example
277
+ * ```typescript
278
+ * quantity: prop.number().nonnegative()
279
+ * ```
280
+ */
281
+ nonnegative(): NumberSchema;
282
+ /**
283
+ * Requires negative number (< 0).
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * debt: prop.number().negative()
288
+ * ```
289
+ */
290
+ negative(): NumberSchema;
291
+ }
292
+ /**
293
+ * Schema for boolean props.
294
+ *
295
+ * @public
296
+ */
297
+ export declare class BooleanSchema extends PropSchema<boolean> {
298
+ /** @internal */
299
+ protected _validate(value: unknown): StandardSchemaV1Result<boolean>;
300
+ /** @internal */
301
+ protected _clone(): BooleanSchema;
302
+ }
303
+ /**
304
+ * Schema for function props.
305
+ *
306
+ * @typeParam T - Function type
307
+ *
308
+ * @public
309
+ */
310
+ export declare class FunctionSchema<T extends (...args: any[]) => any = (...args: any[]) => any> extends PropSchema<T> {
311
+ /** @internal */
312
+ protected _validate(value: unknown): StandardSchemaV1Result<T>;
313
+ /** @internal */
314
+ protected _clone(): FunctionSchema<T>;
315
+ }
316
+ /**
317
+ * Schema for array props with optional item validation.
318
+ *
319
+ * @typeParam T - Array item type
320
+ *
321
+ * @public
322
+ */
323
+ export declare class ArraySchema<T = unknown> extends PropSchema<T[]> {
324
+ /** @internal */
325
+ private _itemSchema?;
326
+ /** @internal */
327
+ private _minLength?;
328
+ /** @internal */
329
+ private _maxLength?;
330
+ /** @internal */
331
+ protected _validate(value: unknown): StandardSchemaV1Result<T[]>;
332
+ /** @internal */
333
+ protected _clone(): ArraySchema<T>;
334
+ /**
335
+ * Specifies the schema for array items.
336
+ *
337
+ * @typeParam U - Item type
338
+ * @param schema - Schema for validating each item
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * tags: prop.array().of(prop.string())
343
+ * scores: prop.array().of(prop.number().min(0).max(100))
344
+ * ```
345
+ */
346
+ of<U>(schema: PropSchema<U>): ArraySchema<U>;
347
+ /**
348
+ * Requires minimum array length.
349
+ *
350
+ * @param length - Minimum number of items
351
+ *
352
+ * @example
353
+ * ```typescript
354
+ * items: prop.array().min(1)
355
+ * ```
356
+ */
357
+ min(length: number): ArraySchema<T>;
358
+ /**
359
+ * Requires maximum array length.
360
+ *
361
+ * @param length - Maximum number of items
362
+ *
363
+ * @example
364
+ * ```typescript
365
+ * selections: prop.array().max(5)
366
+ * ```
367
+ */
368
+ max(length: number): ArraySchema<T>;
369
+ /**
370
+ * Requires non-empty array.
371
+ *
372
+ * @example
373
+ * ```typescript
374
+ * options: prop.array().nonempty()
375
+ * ```
376
+ */
377
+ nonempty(): ArraySchema<T>;
378
+ }
379
+ /**
380
+ * Infers the output type from an object shape definition.
381
+ * @public
382
+ */
383
+ export type InferObjectShape<S extends Record<string, PropSchema<unknown>>> = {
384
+ [K in keyof S]: S[K] extends PropSchema<infer U> ? U : never;
385
+ };
386
+ /**
387
+ * Schema for object props with optional shape validation.
388
+ *
389
+ * @typeParam T - Object type
390
+ *
391
+ * @public
392
+ */
393
+ export declare class ObjectSchema<T extends object = Record<string, unknown>> extends PropSchema<T> {
394
+ /** @internal */
395
+ private _shape?;
396
+ /** @internal */
397
+ private _strict;
398
+ /** @internal */
399
+ protected _validate(value: unknown): StandardSchemaV1Result<T>;
400
+ /** @internal */
401
+ protected _clone(): ObjectSchema<T>;
402
+ /**
403
+ * Defines the shape of the object with field schemas.
404
+ *
405
+ * @typeParam S - Shape definition type
406
+ * @param shape - Object mapping field names to schemas
407
+ *
408
+ * @example
409
+ * ```typescript
410
+ * user: prop.object().shape({
411
+ * name: prop.string(),
412
+ * age: prop.number().optional(),
413
+ * })
414
+ * ```
415
+ */
416
+ shape<S extends Record<string, PropSchema<unknown>>>(shape: S): ObjectSchema<InferObjectShape<S>>;
417
+ /**
418
+ * Rejects objects with unknown keys.
419
+ *
420
+ * @example
421
+ * ```typescript
422
+ * config: prop.object().shape({ debug: prop.boolean() }).strict()
423
+ * ```
424
+ */
425
+ strict(): ObjectSchema<T>;
426
+ }
427
+ /**
428
+ * Schema for literal value props.
429
+ *
430
+ * @typeParam T - Literal type
431
+ *
432
+ * @public
433
+ */
434
+ export declare class LiteralSchema<T extends string | number | boolean> extends PropSchema<T> {
435
+ /** @internal */
436
+ private _value;
437
+ constructor(value: T);
438
+ /** @internal */
439
+ protected _validate(value: unknown): StandardSchemaV1Result<T>;
440
+ /** @internal */
441
+ protected _clone(): LiteralSchema<T>;
442
+ }
443
+ /**
444
+ * Schema for enum/union value props.
445
+ *
446
+ * @typeParam T - Union of allowed values
447
+ *
448
+ * @public
449
+ */
450
+ export declare class EnumSchema<T extends string | number> extends PropSchema<T> {
451
+ /** @internal */
452
+ private _values;
453
+ constructor(values: readonly T[]);
454
+ /** @internal */
455
+ protected _validate(value: unknown): StandardSchemaV1Result<T>;
456
+ /** @internal */
457
+ protected _clone(): EnumSchema<T>;
458
+ }
459
+ /**
460
+ * Schema that accepts any value.
461
+ *
462
+ * @remarks
463
+ * Accepts null by default since "any" means any value.
464
+ *
465
+ * @public
466
+ */
467
+ export declare class AnySchema extends PropSchema<unknown> {
468
+ constructor();
469
+ /** @internal */
470
+ protected _validate(value: unknown): StandardSchemaV1Result<unknown>;
471
+ /** @internal */
472
+ protected _clone(): AnySchema;
473
+ }
474
+ /**
475
+ * Factory functions for creating prop schemas.
476
+ *
477
+ * @remarks
478
+ * Use these functions to define props with a fluent, chainable API.
479
+ * All schemas implement StandardSchemaV1 and integrate seamlessly with
480
+ * ForgeFrame's validation system.
481
+ *
482
+ * @example
483
+ * ```typescript
484
+ * import ForgeFrame, { prop } from 'forgeframe';
485
+ *
486
+ * const MyComponent = ForgeFrame.create({
487
+ * tag: 'my-component',
488
+ * url: '/component',
489
+ * props: {
490
+ * // Required string
491
+ * name: prop.string(),
492
+ *
493
+ * // Number with default
494
+ * count: prop.number().default(0),
495
+ *
496
+ * // Optional email
497
+ * email: prop.string().email().optional(),
498
+ *
499
+ * // Function callback
500
+ * onSubmit: prop.function<(data: { ok: boolean }) => void>(),
501
+ *
502
+ * // Array of strings
503
+ * tags: prop.array().of(prop.string()),
504
+ *
505
+ * // Nested object
506
+ * config: prop.object().shape({
507
+ * theme: prop.enum(['light', 'dark']).default('light'),
508
+ * debug: prop.boolean().default(false),
509
+ * }),
510
+ * },
511
+ * });
512
+ * ```
513
+ *
514
+ * @public
515
+ */
516
+ export declare const prop: {
517
+ /**
518
+ * Creates a string schema.
519
+ *
520
+ * @example
521
+ * ```typescript
522
+ * prop.string()
523
+ * prop.string().min(1).max(100)
524
+ * prop.string().email()
525
+ * prop.string().url()
526
+ * prop.string().pattern(/^[a-z]+$/)
527
+ * ```
528
+ */
529
+ readonly string: () => StringSchema;
530
+ /**
531
+ * Creates a number schema.
532
+ *
533
+ * @example
534
+ * ```typescript
535
+ * prop.number()
536
+ * prop.number().min(0).max(100)
537
+ * prop.number().int()
538
+ * prop.number().positive()
539
+ * ```
540
+ */
541
+ readonly number: () => NumberSchema;
542
+ /**
543
+ * Creates a boolean schema.
544
+ *
545
+ * @example
546
+ * ```typescript
547
+ * prop.boolean()
548
+ * prop.boolean().default(false)
549
+ * ```
550
+ */
551
+ readonly boolean: () => BooleanSchema;
552
+ /**
553
+ * Creates a function schema.
554
+ *
555
+ * @typeParam T - Function type signature
556
+ *
557
+ * @example
558
+ * ```typescript
559
+ * prop.function()
560
+ * prop.function<() => void>()
561
+ * prop.function<(data: { id: string }) => Promise<void>>()
562
+ * ```
563
+ */
564
+ readonly function: <T extends (...args: any[]) => any = (...args: any[]) => any>() => FunctionSchema<T>;
565
+ /**
566
+ * Creates an array schema.
567
+ *
568
+ * @typeParam T - Array item type
569
+ *
570
+ * @example
571
+ * ```typescript
572
+ * prop.array()
573
+ * prop.array().of(prop.string())
574
+ * prop.array().of(prop.number()).min(1).max(10)
575
+ * ```
576
+ */
577
+ readonly array: <T = unknown>() => ArraySchema<T>;
578
+ /**
579
+ * Creates an object schema.
580
+ *
581
+ * @typeParam T - Object type
582
+ *
583
+ * @example
584
+ * ```typescript
585
+ * prop.object()
586
+ * prop.object().shape({
587
+ * name: prop.string(),
588
+ * age: prop.number().optional(),
589
+ * })
590
+ * prop.object().shape({ key: prop.string() }).strict()
591
+ * ```
592
+ */
593
+ readonly object: <T extends object = Record<string, unknown>>() => ObjectSchema<T>;
594
+ /**
595
+ * Creates a literal schema for exact value matching.
596
+ *
597
+ * @param value - The exact value to match
598
+ *
599
+ * @example
600
+ * ```typescript
601
+ * prop.literal('active')
602
+ * prop.literal(42)
603
+ * prop.literal(true)
604
+ * ```
605
+ */
606
+ readonly literal: <T extends string | number | boolean>(value: T) => LiteralSchema<T>;
607
+ /**
608
+ * Creates an enum schema for a set of allowed values.
609
+ *
610
+ * @param values - Array of allowed values
611
+ *
612
+ * @example
613
+ * ```typescript
614
+ * prop.enum(['pending', 'active', 'completed'])
615
+ * prop.enum([1, 2, 3])
616
+ * ```
617
+ */
618
+ readonly enum: <T extends string | number>(values: readonly T[]) => EnumSchema<T>;
619
+ /**
620
+ * Creates a schema that accepts any value.
621
+ *
622
+ * @remarks
623
+ * Use sparingly - prefer typed schemas when possible.
624
+ *
625
+ * @example
626
+ * ```typescript
627
+ * prop.any()
628
+ * ```
629
+ */
630
+ readonly any: () => AnySchema;
631
+ };
632
+ /**
633
+ * Type alias for the prop factory namespace.
634
+ * @public
635
+ */
636
+ export type Prop = typeof prop;