@zipbul/baker 2.1.0 → 3.0.0

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 (91) hide show
  1. package/CHANGELOG.md +256 -0
  2. package/MIGRATION-3.0.md +104 -0
  3. package/README.md +121 -75
  4. package/dist/index.d.ts +8 -7
  5. package/dist/index.js +10 -229
  6. package/dist/src/collect.d.ts +13 -10
  7. package/dist/src/collect.js +26 -0
  8. package/dist/src/configure.d.ts +8 -11
  9. package/dist/src/configure.js +43 -0
  10. package/dist/src/create-rule.d.ts +1 -1
  11. package/dist/src/create-rule.js +41 -0
  12. package/dist/src/decorators/field.d.ts +22 -18
  13. package/dist/src/decorators/field.js +268 -0
  14. package/dist/src/decorators/index.d.ts +1 -0
  15. package/dist/src/decorators/index.js +2 -2
  16. package/dist/src/decorators/recipe.d.ts +17 -0
  17. package/dist/src/decorators/recipe.js +23 -0
  18. package/dist/src/errors.d.ts +28 -17
  19. package/dist/src/errors.js +52 -0
  20. package/dist/src/functions/check-call-options.d.ts +8 -0
  21. package/dist/src/functions/check-call-options.js +51 -0
  22. package/dist/src/functions/deserialize.d.ts +14 -6
  23. package/dist/src/functions/deserialize.js +57 -0
  24. package/dist/src/functions/serialize.d.ts +10 -3
  25. package/dist/src/functions/serialize.js +52 -0
  26. package/dist/src/functions/validate.d.ts +13 -8
  27. package/dist/src/functions/validate.js +49 -0
  28. package/dist/src/interfaces.d.ts +1 -1
  29. package/dist/src/interfaces.js +4 -0
  30. package/dist/src/meta-access.d.ts +19 -0
  31. package/dist/src/meta-access.js +75 -0
  32. package/dist/src/registry.js +8 -0
  33. package/dist/src/rule-metadata.d.ts +11 -0
  34. package/dist/src/rule-metadata.js +17 -0
  35. package/dist/src/rule-plan.d.ts +29 -0
  36. package/dist/src/rule-plan.js +117 -0
  37. package/dist/src/rules/array.d.ts +7 -6
  38. package/dist/src/rules/array.js +96 -0
  39. package/dist/src/rules/common.d.ts +2 -2
  40. package/dist/src/rules/common.js +77 -0
  41. package/dist/src/rules/date.js +35 -0
  42. package/dist/src/rules/index.d.ts +2 -4
  43. package/dist/src/rules/index.js +8 -11
  44. package/dist/src/rules/locales.d.ts +5 -4
  45. package/dist/src/rules/locales.js +249 -0
  46. package/dist/src/rules/number.d.ts +2 -2
  47. package/dist/src/rules/number.js +79 -0
  48. package/dist/src/rules/object.d.ts +1 -1
  49. package/dist/src/rules/object.js +49 -0
  50. package/dist/src/rules/string.d.ts +83 -80
  51. package/dist/src/rules/string.js +1998 -0
  52. package/dist/src/rules/typechecker.d.ts +6 -6
  53. package/dist/src/rules/typechecker.js +143 -0
  54. package/dist/src/seal/circular-analyzer.js +63 -0
  55. package/dist/src/seal/codegen-utils.d.ts +7 -0
  56. package/dist/src/seal/codegen-utils.js +18 -0
  57. package/dist/src/seal/deserialize-builder.d.ts +8 -3
  58. package/dist/src/seal/deserialize-builder.js +1546 -0
  59. package/dist/src/seal/expose-validator.d.ts +3 -2
  60. package/dist/src/seal/expose-validator.js +65 -0
  61. package/dist/src/seal/seal-state.d.ts +10 -0
  62. package/dist/src/seal/seal-state.js +18 -0
  63. package/dist/src/seal/seal.d.ts +22 -21
  64. package/dist/src/seal/seal.js +431 -0
  65. package/dist/src/seal/serialize-builder.d.ts +3 -2
  66. package/dist/src/seal/serialize-builder.js +374 -0
  67. package/dist/src/seal/validate-meta.d.ts +13 -0
  68. package/dist/src/seal/validate-meta.js +61 -0
  69. package/dist/src/symbols.d.ts +1 -1
  70. package/dist/src/symbols.js +13 -2
  71. package/dist/src/transformers/collection.transformer.js +25 -0
  72. package/dist/src/transformers/date.transformer.js +18 -0
  73. package/dist/src/transformers/index.js +6 -2
  74. package/dist/src/transformers/luxon.transformer.d.ts +4 -2
  75. package/dist/src/transformers/luxon.transformer.js +34 -0
  76. package/dist/src/transformers/moment.transformer.d.ts +4 -2
  77. package/dist/src/transformers/moment.transformer.js +32 -0
  78. package/dist/src/transformers/number.transformer.js +8 -0
  79. package/dist/src/transformers/string.transformer.js +12 -0
  80. package/dist/src/types.d.ts +68 -28
  81. package/dist/src/types.js +1 -0
  82. package/dist/src/utils.d.ts +4 -2
  83. package/dist/src/utils.js +10 -0
  84. package/package.json +80 -67
  85. package/dist/index-fnv35wrf.js +0 -3
  86. package/dist/index-k3d659ad.js +0 -3
  87. package/dist/index-s0n74vx1.js +0 -3
  88. package/dist/index-xdn55cz3.js +0 -1
  89. package/dist/src/functions/_run-sealed.d.ts +0 -7
  90. package/dist/src/functions/index.d.ts +0 -3
  91. package/dist/src/seal/index.d.ts +0 -5
@@ -0,0 +1,52 @@
1
+ // ─────────────────────────────────────────────────────────────────────────────
2
+ // BakerIssue — Individual field error (§12.2)
3
+ // ─────────────────────────────────────────────────────────────────────────────
4
+ // ─────────────────────────────────────────────────────────────────────────────
5
+ // BakerIssueSet — Validation failure return (§12.2)
6
+ // ─────────────────────────────────────────────────────────────────────────────
7
+ /** Symbol tag for isBakerIssueSet() type guard — collision-proof discriminator */
8
+ export const BAKER_ERROR = Symbol.for('baker:error');
9
+ /**
10
+ * Type guard — narrows deserialize()/validate() result to BakerIssueSet.
11
+ *
12
+ * @example
13
+ * const result = await deserialize(UserDto, input);
14
+ * if (isBakerIssueSet(result)) {
15
+ * result.errors // readonly BakerIssue[]
16
+ * } else {
17
+ * result // UserDto
18
+ * }
19
+ */
20
+ export function isBakerIssueSet(value) {
21
+ return (value != null &&
22
+ typeof value === 'object' &&
23
+ !Array.isArray(value) &&
24
+ value[BAKER_ERROR] === true);
25
+ }
26
+ /** @internal — create BakerIssueSet object */
27
+ export function toBakerIssueSet(errors) {
28
+ return { [BAKER_ERROR]: true, errors };
29
+ }
30
+ // ─────────────────────────────────────────────────────────────────────────────
31
+ // BakerError — the single throw channel (§12.2)
32
+ // ─────────────────────────────────────────────────────────────────────────────
33
+ /**
34
+ * The single error thrown by baker for any developer/config/schema misuse — i.e. anything
35
+ * discoverable without external input. End-user input-data failures are NOT thrown; they are
36
+ * returned as a {@link BakerIssueSet}.
37
+ *
38
+ * Thrown when, e.g.:
39
+ * - deserialize()/serialize()/validate() is called on an unsealed class
40
+ * - configure() is called after seal(), or with an unknown key
41
+ * - seal-time metadata invariants fail (discriminator, Map keys, banned names, …)
42
+ * - per-call options contain unsupported keys
43
+ * - @Field receives a non-rule value, or a rule/transformer factory is misused
44
+ * - a user @Type/collectionValue thunk throws (wrapped, with the original error as `cause`)
45
+ * - an optional peer dependency (luxon/moment) is missing
46
+ */
47
+ export class BakerError extends Error {
48
+ constructor(message, options) {
49
+ super(message, options);
50
+ this.name = 'BakerError';
51
+ }
52
+ }
@@ -0,0 +1,8 @@
1
+ import type { RuntimeOptions } from '../interfaces';
2
+ /**
3
+ * @internal — validate per-call options object at public-API entry.
4
+ * `groups` is the only valid per-call key; everything else is rejected:
5
+ * - seal-time keys (BakerConfig / SealOptions) → "move to configure({...})"
6
+ * - any other key → "unknown call option"
7
+ */
8
+ export declare function checkCallOptions(opts: unknown): RuntimeOptions | undefined;
@@ -0,0 +1,51 @@
1
+ import { BakerError } from '../errors.js';
2
+ const CALL_OPTION_KEYS = new Set(['groups']);
3
+ const SEAL_TIME_KEYS = new Set([
4
+ // BakerConfig (public, configure-time)
5
+ 'autoConvert',
6
+ 'allowClassDefaults',
7
+ 'stopAtFirstError',
8
+ 'forbidUnknown',
9
+ 'debug',
10
+ // SealOptions (internal, legacy aliases — same set covered by public names)
11
+ 'enableImplicitConversion',
12
+ 'exposeDefaultValues',
13
+ 'whitelist',
14
+ ]);
15
+ /**
16
+ * @internal — validate per-call options object at public-API entry.
17
+ * `groups` is the only valid per-call key; everything else is rejected:
18
+ * - seal-time keys (BakerConfig / SealOptions) → "move to configure({...})"
19
+ * - any other key → "unknown call option"
20
+ */
21
+ export function checkCallOptions(opts) {
22
+ if (opts === undefined || opts === null) {
23
+ return undefined;
24
+ }
25
+ if (typeof opts !== 'object' || Array.isArray(opts)) {
26
+ throw new BakerError(`Call options must be a plain object. Received: ${Array.isArray(opts) ? 'array' : typeof opts}.`);
27
+ }
28
+ // Strict same-realm plain-object check.
29
+ // Accept: `{}` (proto === Object.prototype) and `Object.create(null)` (proto === null).
30
+ // Reject: every other prototype, including class instances whose `constructor.name` is
31
+ // renamed to 'Object' (trojan), built-ins (Date/Map/Set), cross-realm objects.
32
+ // Cross-realm consumers can normalize with `Object.assign({}, opts)` before calling.
33
+ const proto = Object.getPrototypeOf(opts);
34
+ if (proto !== null && proto !== Object.prototype) {
35
+ const ctorName = opts.constructor?.name ?? 'unknown';
36
+ throw new BakerError(`Call options must be a plain object literal. Received instance of ${ctorName}.`);
37
+ }
38
+ for (const key of Object.keys(opts)) {
39
+ if (CALL_OPTION_KEYS.has(key)) {
40
+ continue;
41
+ }
42
+ if (SEAL_TIME_KEYS.has(key)) {
43
+ throw new BakerError(`Option '${key}' is a seal-time setting and cannot be passed per-call. ` +
44
+ `Move it to configure({ ${key}: ... }) at app startup. ` +
45
+ `Per-call options: ${[...CALL_OPTION_KEYS].join(', ')}.`);
46
+ }
47
+ throw new BakerError(`Unknown per-call option '${key}'. Valid per-call options: ${[...CALL_OPTION_KEYS].join(', ')}. ` +
48
+ `Seal-time options go to configure({...}).`);
49
+ }
50
+ return opts;
51
+ }
@@ -1,11 +1,19 @@
1
- import type { BakerErrors } from '../errors';
2
1
  import type { RuntimeOptions } from '../interfaces';
2
+ import { type BakerIssueSet } from '../errors';
3
3
  /**
4
4
  * Converts input to a Class instance + validates.
5
- * - Auto-seals on first call (batches entire globalRegistry)
6
- * - Sync DTOs return directly, async DTOs return Promise
5
+ * - Requires `seal()` to be called beforehand; throws `BakerError` if not sealed
6
+ * - Sync DTOs return directly; async DTOs return Promise
7
7
  * - Success: T
8
- * - Validation failure: BakerErrors (use isBakerError() to narrow)
9
- * - Class without decorators: throws SealError
8
+ * - Validation failure: BakerIssueSet (use isBakerIssueSet() to narrow)
10
9
  */
11
- export declare function deserialize<T>(Class: new (...args: any[]) => T, input: unknown, options?: RuntimeOptions): T | BakerErrors | Promise<T | BakerErrors>;
10
+ export declare function deserialize<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): T | BakerIssueSet | Promise<T | BakerIssueSet>;
11
+ /**
12
+ * Sync-asserted deserialize. Throws `BakerError` if Class has any async rule/transform
13
+ * on the deserialize side.
14
+ */
15
+ export declare function deserializeSync<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): T | BakerIssueSet;
16
+ /**
17
+ * Async-asserted deserialize. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
18
+ */
19
+ export declare function deserializeAsync<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): Promise<T | BakerIssueSet>;
@@ -0,0 +1,57 @@
1
+ import { isErr } from '@zipbul/result';
2
+ import { toBakerIssueSet, BakerError } from '../errors.js';
3
+ import { ensureSealed } from '../seal/seal.js';
4
+ import { checkCallOptions } from './check-call-options.js';
5
+ export function deserialize(Class, input, options) {
6
+ const checkedOpts = checkCallOptions(options);
7
+ const sealed = ensureSealed(Class);
8
+ if (sealed.isAsync) {
9
+ return sealed.deserialize(input, checkedOpts).then((result) => {
10
+ if (isErr(result)) {
11
+ return toBakerIssueSet(result.data);
12
+ }
13
+ return result;
14
+ });
15
+ }
16
+ const result = sealed.deserialize(input, checkedOpts);
17
+ if (isErr(result)) {
18
+ return toBakerIssueSet(result.data);
19
+ }
20
+ return result;
21
+ }
22
+ /**
23
+ * Sync-asserted deserialize. Throws `BakerError` if Class has any async rule/transform
24
+ * on the deserialize side.
25
+ */
26
+ export function deserializeSync(Class, input, options) {
27
+ const checkedOpts = checkCallOptions(options);
28
+ const sealed = ensureSealed(Class);
29
+ if (sealed.isAsync) {
30
+ throw new BakerError(`deserializeSync(${Class.name}): DTO has async rules/transforms. Use deserializeAsync() instead.`);
31
+ }
32
+ const result = sealed.deserialize(input, checkedOpts);
33
+ if (isErr(result)) {
34
+ return toBakerIssueSet(result.data);
35
+ }
36
+ return result;
37
+ }
38
+ /**
39
+ * Async-asserted deserialize. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
40
+ */
41
+ export function deserializeAsync(Class, input, options) {
42
+ const checkedOpts = checkCallOptions(options);
43
+ const sealed = ensureSealed(Class);
44
+ if (sealed.isAsync) {
45
+ return sealed.deserialize(input, checkedOpts).then((result) => {
46
+ if (isErr(result)) {
47
+ return toBakerIssueSet(result.data);
48
+ }
49
+ return result;
50
+ });
51
+ }
52
+ const result = sealed.deserialize(input, checkedOpts);
53
+ if (isErr(result)) {
54
+ return Promise.resolve(toBakerIssueSet(result.data));
55
+ }
56
+ return Promise.resolve(result);
57
+ }
@@ -1,9 +1,16 @@
1
1
  import type { RuntimeOptions } from '../interfaces';
2
2
  /**
3
3
  * Converts a Class instance to a plain object.
4
- * - Auto-seals on first call (batches entire globalRegistry)
5
- * - Sync DTOs return directly, async DTOs return Promise
4
+ * - Requires `seal()` to be called beforehand; throws `BakerError` if not sealed
5
+ * - Sync DTOs return directly; async DTOs return Promise
6
6
  * - No validation — always returns Record<string, unknown>
7
- * - Class without decorators: throws SealError
8
7
  */
9
8
  export declare function serialize<T>(instance: T, options?: RuntimeOptions): Record<string, unknown> | Promise<Record<string, unknown>>;
9
+ /**
10
+ * Sync-asserted serialize. Throws `BakerError` if Class has any async transform on the serialize side.
11
+ */
12
+ export declare function serializeSync<T>(instance: T, options?: RuntimeOptions): Record<string, unknown>;
13
+ /**
14
+ * Async-asserted serialize. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
15
+ */
16
+ export declare function serializeAsync<T>(instance: T, options?: RuntimeOptions): Promise<Record<string, unknown>>;
@@ -0,0 +1,52 @@
1
+ import { BakerError } from '../errors.js';
2
+ import { ensureSealed } from '../seal/seal.js';
3
+ import { checkCallOptions } from './check-call-options.js';
4
+ // ─────────────────────────────────────────────────────────────────────────────
5
+ // serialize — Public API (§5.2)
6
+ // ─────────────────────────────────────────────────────────────────────────────
7
+ /** Boundary check shared by serialize / serializeSync / serializeAsync. */
8
+ function resolveSerializer(instance, fnName) {
9
+ if (instance == null || typeof instance !== 'object') {
10
+ throw new BakerError(`${fnName}: expected a class instance, got ${instance === null ? 'null' : typeof instance}`);
11
+ }
12
+ const Class = instance.constructor;
13
+ if (typeof Class !== 'function') {
14
+ throw new BakerError(`${fnName}: instance has no constructor`);
15
+ }
16
+ // Reject plain objects and forged ones (e.g. `{ constructor: SomeDto }`): a real instance is
17
+ // `instanceof` its own constructor via the prototype chain; the `constructor` property alone
18
+ // (which anyone can set) is not trusted.
19
+ if (Class === Object || !(instance instanceof Class)) {
20
+ throw new BakerError(`${fnName}: received a plain object. Pass an instance of a DTO class decorated with @Field.`);
21
+ }
22
+ return ensureSealed(Class);
23
+ }
24
+ export function serialize(instance, options) {
25
+ const checkedOpts = checkCallOptions(options);
26
+ const sealed = resolveSerializer(instance, 'serialize');
27
+ return sealed.isSerializeAsync
28
+ ? sealed.serialize(instance, checkedOpts)
29
+ : sealed.serialize(instance, checkedOpts);
30
+ }
31
+ /**
32
+ * Sync-asserted serialize. Throws `BakerError` if Class has any async transform on the serialize side.
33
+ */
34
+ export function serializeSync(instance, options) {
35
+ const checkedOpts = checkCallOptions(options);
36
+ const sealed = resolveSerializer(instance, 'serializeSync');
37
+ if (sealed.isSerializeAsync) {
38
+ const className = instance.constructor.name;
39
+ throw new BakerError(`serializeSync(${className}): DTO has async serialize transforms. Use serializeAsync() instead.`);
40
+ }
41
+ return sealed.serialize(instance, checkedOpts);
42
+ }
43
+ /**
44
+ * Async-asserted serialize. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
45
+ */
46
+ export function serializeAsync(instance, options) {
47
+ const checkedOpts = checkCallOptions(options);
48
+ const sealed = resolveSerializer(instance, 'serializeAsync');
49
+ return sealed.isSerializeAsync
50
+ ? sealed.serialize(instance, checkedOpts)
51
+ : Promise.resolve(sealed.serialize(instance, checkedOpts));
52
+ }
@@ -1,13 +1,18 @@
1
- import type { BakerErrors } from '../errors';
2
- import type { EmittableRule } from '../types';
1
+ import type { BakerIssueSet } from '../errors';
3
2
  import type { RuntimeOptions } from '../interfaces';
4
3
  /**
5
- * DTO-level validation — validates input against a decorated class.
6
- * Sync DTOs return directly, async DTOs return Promise.
4
+ * DTO-level validation — validates `input` against a decorated class's schema.
5
+ * Sync DTOs return directly; async DTOs return Promise. To validate a single primitive without a
6
+ * DTO, call the rule directly (e.g. `isEmail()(value)`).
7
7
  */
8
- export declare function validate<T>(Class: new (...args: any[]) => T, input: unknown, options?: RuntimeOptions): true | BakerErrors | Promise<true | BakerErrors>;
8
+ declare function validate<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): true | BakerIssueSet | Promise<true | BakerIssueSet>;
9
9
  /**
10
- * Ad-hoc validation validates a single value against one or more rules.
11
- * Sync rules return directly, async rules return Promise.
10
+ * Sync-asserted validate. Throws `BakerError` if Class has any async rule/transform
11
+ * on the deserialize/validate side. Use when caller code assumes sync return.
12
12
  */
13
- export declare function validate(input: unknown, ...rules: EmittableRule[]): true | BakerErrors | Promise<true | BakerErrors>;
13
+ declare function validateSync<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): true | BakerIssueSet;
14
+ /**
15
+ * Async-asserted validate. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
16
+ */
17
+ declare function validateAsync<T>(Class: new (...args: never[]) => T, input: unknown, options?: RuntimeOptions): Promise<true | BakerIssueSet>;
18
+ export { validate, validateSync, validateAsync };
@@ -0,0 +1,49 @@
1
+ import { toBakerIssueSet, BakerError } from '../errors.js';
2
+ import { ensureSealed } from '../seal/seal.js';
3
+ import { checkCallOptions } from './check-call-options.js';
4
+ // ─────────────────────────────────────────────────────────────────────────────
5
+ // validate — Public API (§5.3)
6
+ // ─────────────────────────────────────────────────────────────────────────────
7
+ /**
8
+ * DTO-level validation — validates `input` against a decorated class's schema.
9
+ * Sync DTOs return directly; async DTOs return Promise. To validate a single primitive without a
10
+ * DTO, call the rule directly (e.g. `isEmail()(value)`).
11
+ */
12
+ function validate(Class, input, options) {
13
+ const checkedOpts = checkCallOptions(options);
14
+ const sealed = ensureSealed(Class);
15
+ if (sealed.isAsync) {
16
+ return sealed.validate(input, checkedOpts).then((result) => result === null ? true : toBakerIssueSet(result));
17
+ }
18
+ const result = sealed.validate(input, checkedOpts);
19
+ return result === null ? true : toBakerIssueSet(result);
20
+ }
21
+ // ─────────────────────────────────────────────────────────────────────────────
22
+ // W14: strict sync/async variants — explicit intent at call site
23
+ // ─────────────────────────────────────────────────────────────────────────────
24
+ /**
25
+ * Sync-asserted validate. Throws `BakerError` if Class has any async rule/transform
26
+ * on the deserialize/validate side. Use when caller code assumes sync return.
27
+ */
28
+ function validateSync(Class, input, options) {
29
+ const checkedOpts = checkCallOptions(options);
30
+ const sealed = ensureSealed(Class);
31
+ if (sealed.isAsync) {
32
+ throw new BakerError(`validateSync(${Class.name}): DTO has async rules/transforms. Use validateAsync() instead.`);
33
+ }
34
+ const result = sealed.validate(input, checkedOpts);
35
+ return result === null ? true : toBakerIssueSet(result);
36
+ }
37
+ /**
38
+ * Async-asserted validate. Always returns Promise (sync DTOs are wrapped via Promise.resolve).
39
+ */
40
+ function validateAsync(Class, input, options) {
41
+ const checkedOpts = checkCallOptions(options);
42
+ const sealed = ensureSealed(Class);
43
+ if (sealed.isAsync) {
44
+ return sealed.validate(input, checkedOpts).then((r) => r === null ? true : toBakerIssueSet(r));
45
+ }
46
+ const result = sealed.validate(input, checkedOpts);
47
+ return Promise.resolve(result === null ? true : toBakerIssueSet(result));
48
+ }
49
+ export { validate, validateSync, validateAsync };
@@ -16,7 +16,7 @@ export interface SealOptions {
16
16
  stopAtFirstError?: boolean;
17
17
  /**
18
18
  * true: reject undeclared fields. Uses the key set from mergeInheritance(Class) as the allowlist.
19
- * @Exclude fields are also included in the whitelist — their presence is allowed but they are excluded from the result.
19
+ * `@Exclude` fields are also included in the whitelist — their presence is allowed but they are excluded from the result.
20
20
  * @default false
21
21
  */
22
22
  whitelist?: boolean;
@@ -0,0 +1,4 @@
1
+ // ─────────────────────────────────────────────────────────────────────────────
2
+ // SealOptions — seal() global options (§1.4)
3
+ // ─────────────────────────────────────────────────────────────────────────────
4
+ export {};
@@ -0,0 +1,19 @@
1
+ import type { RawClassMeta, SealedExecutors } from './types';
2
+ export declare function getSealed(cls: Function): SealedExecutors<unknown> | undefined;
3
+ /** Same as getSealed but throws if the class is not sealed — for callers that establish the invariant elsewhere. */
4
+ export declare function requireSealed(cls: Function): SealedExecutors<unknown>;
5
+ export declare function setSealed(cls: Function, exec: SealedExecutors<unknown>): void;
6
+ export declare function hasSealedOwn(cls: Function): boolean;
7
+ export declare function deleteSealed(cls: Function): void;
8
+ export declare function deleteRaw(cls: Function): void;
9
+ export declare function getRaw(cls: Function): RawClassMeta | undefined;
10
+ /** Same as getRaw but throws if the class has no @Field decorators — for callers that establish the invariant elsewhere. */
11
+ export declare function requireRaw(cls: Function): RawClassMeta;
12
+ export declare function setRaw(cls: Function, raw: RawClassMeta): void;
13
+ /**
14
+ * True only when cls has its OWN RAW slot. A subclass without its own @Field decorators
15
+ * resolves Class[Symbol.metadata] to the parent's metadata via the class prototype chain;
16
+ * the dual own-check keeps mergeInheritance from double-counting inherited fields.
17
+ */
18
+ export declare function hasRawOwn(cls: Function): boolean;
19
+ export declare function freezeRaw(cls: Function): void;
@@ -0,0 +1,75 @@
1
+ import { RAW, SEALED } from './symbols.js';
2
+ /** Returns the metadata object visible on cls (own or inherited via the class prototype chain). */
3
+ function metaOf(cls) {
4
+ return cls[Symbol.metadata] ?? undefined;
5
+ }
6
+ /** Returns the class's own metadata object, creating one if absent. */
7
+ function ensureOwnMeta(cls) {
8
+ if (!Object.hasOwn(cls, Symbol.metadata)) {
9
+ Object.defineProperty(cls, Symbol.metadata, {
10
+ value: {},
11
+ writable: true,
12
+ configurable: true,
13
+ enumerable: false,
14
+ });
15
+ }
16
+ return cls[Symbol.metadata];
17
+ }
18
+ export function getSealed(cls) {
19
+ return cls[SEALED];
20
+ }
21
+ /** Same as getSealed but throws if the class is not sealed — for callers that establish the invariant elsewhere. */
22
+ export function requireSealed(cls) {
23
+ const v = cls[SEALED];
24
+ if (v === undefined) {
25
+ throw new Error(`${cls.name || '<anonymous>'}: class is not sealed`);
26
+ }
27
+ return v;
28
+ }
29
+ export function setSealed(cls, exec) {
30
+ cls[SEALED] = exec;
31
+ }
32
+ export function hasSealedOwn(cls) {
33
+ return Object.hasOwn(cls, SEALED);
34
+ }
35
+ export function deleteSealed(cls) {
36
+ delete cls[SEALED];
37
+ }
38
+ export function deleteRaw(cls) {
39
+ if (Object.hasOwn(cls, Symbol.metadata)) {
40
+ delete cls[Symbol.metadata][RAW];
41
+ }
42
+ }
43
+ export function getRaw(cls) {
44
+ return metaOf(cls)?.[RAW];
45
+ }
46
+ /** Same as getRaw but throws if the class has no @Field decorators — for callers that establish the invariant elsewhere. */
47
+ export function requireRaw(cls) {
48
+ const v = getRaw(cls);
49
+ if (v === undefined) {
50
+ throw new Error(`${cls.name || '<anonymous>'}: class has no @Field decorators`);
51
+ }
52
+ return v;
53
+ }
54
+ export function setRaw(cls, raw) {
55
+ ensureOwnMeta(cls)[RAW] = raw;
56
+ }
57
+ /**
58
+ * True only when cls has its OWN RAW slot. A subclass without its own @Field decorators
59
+ * resolves Class[Symbol.metadata] to the parent's metadata via the class prototype chain;
60
+ * the dual own-check keeps mergeInheritance from double-counting inherited fields.
61
+ */
62
+ export function hasRawOwn(cls) {
63
+ if (!Object.hasOwn(cls, Symbol.metadata)) {
64
+ return false;
65
+ }
66
+ const meta = cls[Symbol.metadata];
67
+ return meta != null && Object.hasOwn(meta, RAW);
68
+ }
69
+ export function freezeRaw(cls) {
70
+ // Guard on own RAW: an inherited-only subclass must not freeze the parent's RAW.
71
+ if (!hasRawOwn(cls)) {
72
+ return;
73
+ }
74
+ Object.freeze(getRaw(cls));
75
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Global registry — automatically registers classes with at least one decorator attached
3
+ *
4
+ * - Automatically called from ensureMeta()
5
+ * - seal() iterates this Set to seal all DTOs
6
+ * - Metadata is not stored here — used only as an index (which classes are registered)
7
+ */
8
+ export const globalRegistry = new Set();
@@ -0,0 +1,11 @@
1
+ import type { EmittableRule, InternalRule, RulePlan } from './types';
2
+ interface RuleMetadata {
3
+ emit: EmittableRule['emit'];
4
+ ruleName: string;
5
+ requiresType?: EmittableRule['requiresType'];
6
+ constraints?: Record<string, unknown>;
7
+ isAsync?: boolean;
8
+ plan?: RulePlan;
9
+ }
10
+ export declare function defineRuleMetadata(fn: InternalRule, meta: RuleMetadata): void;
11
+ export {};
@@ -0,0 +1,17 @@
1
+ export function defineRuleMetadata(fn, meta) {
2
+ const target = fn;
3
+ target.emit = meta.emit;
4
+ target.ruleName = meta.ruleName;
5
+ if (meta.requiresType !== undefined) {
6
+ target.requiresType = meta.requiresType;
7
+ }
8
+ if (meta.constraints !== undefined) {
9
+ target.constraints = meta.constraints;
10
+ }
11
+ if (meta.isAsync !== undefined) {
12
+ target.isAsync = meta.isAsync;
13
+ }
14
+ if (meta.plan) {
15
+ target.plan = meta.plan;
16
+ }
17
+ }
@@ -0,0 +1,29 @@
1
+ import type { EmitContext, InternalRule, RulePlan, RulePlanCheck, RulePlanExpr } from './types';
2
+ type RulePlanCache = {
3
+ length?: string;
4
+ time?: string;
5
+ };
6
+ declare const planValue: () => RulePlanExpr;
7
+ declare const planLength: (object?: RulePlanExpr) => RulePlanExpr;
8
+ declare const planTime: (object?: RulePlanExpr) => RulePlanExpr;
9
+ declare const planLiteral: (value: number) => RulePlanExpr;
10
+ declare const planCompare: (left: RulePlanExpr, op: "<" | "<=" | ">" | ">=" | "===" | "!==", right: number | RulePlanExpr) => RulePlanCheck;
11
+ declare const planOr: (...checks: RulePlanCheck[]) => RulePlanCheck;
12
+ declare function makePlannedRule(options: {
13
+ name: string;
14
+ requiresType: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object';
15
+ constraints?: Record<string, unknown>;
16
+ plan: RulePlan;
17
+ validate: (value: unknown) => boolean;
18
+ }): InternalRule;
19
+ declare function makeRule(options: {
20
+ name: string;
21
+ validate: (value: unknown) => boolean | Promise<boolean>;
22
+ emit: (varName: string, ctx: EmitContext) => string;
23
+ requiresType?: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object';
24
+ constraints?: Record<string, unknown>;
25
+ isAsync?: boolean;
26
+ plan?: RulePlan;
27
+ }): InternalRule;
28
+ declare function emitRulePlan(varName: string, ctx: EmitContext, ruleName: string, plan: RulePlan, cache?: RulePlanCache, insideTypeGate?: boolean): string;
29
+ export { planValue, planLength, planTime, planLiteral, planCompare, planOr, makePlannedRule, makeRule, emitRulePlan };
@@ -0,0 +1,117 @@
1
+ import { defineRuleMetadata } from './rule-metadata.js';
2
+ const planValue = () => ({ kind: 'value' });
3
+ const planLength = (object = planValue()) => ({
4
+ kind: 'member',
5
+ object,
6
+ property: 'length',
7
+ });
8
+ const planTime = (object = planValue()) => ({
9
+ kind: 'call0',
10
+ object,
11
+ method: 'getTime',
12
+ });
13
+ const planLiteral = (value) => ({ kind: 'literal', value });
14
+ const planCompare = (left, op, right) => ({
15
+ kind: 'compare',
16
+ left,
17
+ op,
18
+ right: typeof right === 'number' ? planLiteral(right) : right,
19
+ });
20
+ const planOr = (...checks) => ({ kind: 'or', checks });
21
+ function makePlannedRule(options) {
22
+ const inner = {
23
+ name: options.name,
24
+ requiresType: options.requiresType,
25
+ plan: options.plan,
26
+ validate: options.validate,
27
+ emit: (varName, ctx) => emitRulePlan(varName, ctx, options.name, options.plan, undefined, ctx.insideTypeGate),
28
+ };
29
+ if (options.constraints !== undefined) {
30
+ inner.constraints = options.constraints;
31
+ }
32
+ return makeRule(inner);
33
+ }
34
+ function makeRule(options) {
35
+ const fn = ((value) => options.validate(value));
36
+ const meta = {
37
+ emit: options.emit,
38
+ ruleName: options.name,
39
+ constraints: options.constraints ?? {},
40
+ };
41
+ if (options.requiresType !== undefined) {
42
+ meta.requiresType = options.requiresType;
43
+ }
44
+ if (options.isAsync !== undefined) {
45
+ meta.isAsync = options.isAsync;
46
+ }
47
+ if (options.plan !== undefined) {
48
+ meta.plan = options.plan;
49
+ }
50
+ defineRuleMetadata(fn, meta);
51
+ return fn;
52
+ }
53
+ function emitRulePlan(varName, ctx, ruleName, plan, cache, insideTypeGate) {
54
+ const failure = insideTypeGate ? stripSelfComparison(plan.failure) : plan.failure;
55
+ return `if (${emitPlanCheck(failure, varName, cache)}) ${ctx.fail(ruleName)};`;
56
+ }
57
+ /** Strip `x !== x` / `getTime() !== getTime()` self-comparison guards — redundant inside type gate */
58
+ function stripSelfComparison(check) {
59
+ if (check.kind === 'compare') {
60
+ return check;
61
+ }
62
+ const filtered = check.checks.filter(c => !isSelfComparison(c));
63
+ if (filtered.length === 0) {
64
+ return check;
65
+ } // safety: don't strip everything
66
+ if (filtered.length === 1) {
67
+ return filtered[0];
68
+ }
69
+ return { kind: check.kind, checks: filtered };
70
+ }
71
+ function isSelfComparison(check) {
72
+ if (check.kind !== 'compare' || check.op !== '!==') {
73
+ return false;
74
+ }
75
+ return exprEqual(check.left, check.right);
76
+ }
77
+ function exprEqual(a, b) {
78
+ if (a.kind !== b.kind) {
79
+ return false;
80
+ }
81
+ switch (a.kind) {
82
+ case 'value':
83
+ return true;
84
+ case 'literal':
85
+ return a.value === b.value;
86
+ case 'member':
87
+ return exprEqual(a.object, b.object);
88
+ case 'call0':
89
+ return a.method === b.method && exprEqual(a.object, b.object);
90
+ default:
91
+ // Compile-time exhaustiveness: adding a RulePlanExpr.kind without a case fails to compile here.
92
+ return a;
93
+ }
94
+ }
95
+ function emitPlanCheck(check, varName, cache) {
96
+ if (check.kind === 'compare') {
97
+ return `${emitPlanExpr(check.left, varName, cache)} ${check.op} ${emitPlanExpr(check.right, varName, cache)}`;
98
+ }
99
+ const joiner = check.kind === 'and' ? ' && ' : ' || ';
100
+ return `(${check.checks.map(part => emitPlanCheck(part, varName, cache)).join(joiner)})`;
101
+ }
102
+ function emitPlanExpr(expr, varName, cache) {
103
+ switch (expr.kind) {
104
+ case 'value':
105
+ return varName;
106
+ case 'literal':
107
+ return String(expr.value);
108
+ case 'member':
109
+ return cache?.length ?? `${emitPlanExpr(expr.object, varName, cache)}.length`;
110
+ case 'call0':
111
+ return cache?.time ?? `${emitPlanExpr(expr.object, varName, cache)}.getTime()`;
112
+ default:
113
+ // Compile-time exhaustiveness: adding a RulePlanExpr.kind without a case fails to compile here.
114
+ return expr;
115
+ }
116
+ }
117
+ export { planValue, planLength, planTime, planLiteral, planCompare, planOr, makePlannedRule, makeRule, emitRulePlan };