@orkestrel/contract 0.0.22 → 0.0.23
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/src/core/index.cjs +183 -27
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +107 -20
- package/dist/src/core/index.d.ts +107 -20
- package/dist/src/core/index.js +180 -28
- package/dist/src/core/index.js.map +1 -1
- package/package.json +8 -8
package/dist/src/core/index.js
CHANGED
|
@@ -12,6 +12,7 @@ var SHAPE_FIELDS = Object.freeze({
|
|
|
12
12
|
"min",
|
|
13
13
|
"max",
|
|
14
14
|
"pattern",
|
|
15
|
+
"date",
|
|
15
16
|
"description"
|
|
16
17
|
]),
|
|
17
18
|
number: Object.freeze([
|
|
@@ -348,7 +349,9 @@ var JSON_SCHEMA_TYPES = Object.freeze([
|
|
|
348
349
|
* lose nothing when a shape drops them: `title`, `$schema`, `$id`, `$comment`,
|
|
349
350
|
* `examples`, `deprecated`, `readOnly`, and `writeOnly`. Every other keyword refuses.
|
|
350
351
|
* `pattern` and `format` are absent because no compiled shape asserts them, so a
|
|
351
|
-
* schema holding one would convert to a shape wider than the schema it came from.
|
|
352
|
+
* schema holding one would convert to a shape wider than the schema it came from. The
|
|
353
|
+
* one exception is `format: 'date'` on a string node, which a strict conversion reads
|
|
354
|
+
* as a date string shape.
|
|
352
355
|
*
|
|
353
356
|
* @example
|
|
354
357
|
* ```ts
|
|
@@ -428,6 +431,7 @@ var CONTRACT_CODES = Object.freeze([
|
|
|
428
431
|
*/
|
|
429
432
|
var FAULT_KINDS = Object.freeze([
|
|
430
433
|
"string",
|
|
434
|
+
"date",
|
|
431
435
|
"number",
|
|
432
436
|
"integer",
|
|
433
437
|
"boolean",
|
|
@@ -454,6 +458,7 @@ var FAULT_CONSTRAINTS = Object.freeze([
|
|
|
454
458
|
"min",
|
|
455
459
|
"max",
|
|
456
460
|
"pattern",
|
|
461
|
+
"date",
|
|
457
462
|
"integer"
|
|
458
463
|
]);
|
|
459
464
|
/**
|
|
@@ -677,6 +682,19 @@ var FORMAT_PATTERNS = Object.freeze({
|
|
|
677
682
|
uri: /^[a-z][a-z0-9+.-]*:\/\//i
|
|
678
683
|
});
|
|
679
684
|
/**
|
|
685
|
+
* Holds `10`, the length of every `YYYY-MM-DD` date a date string shape admits.
|
|
686
|
+
*
|
|
687
|
+
* @remarks
|
|
688
|
+
* A date string shape whose `min` or `max` excludes this length admits no value, so
|
|
689
|
+
* building one refuses with code `range`.
|
|
690
|
+
*
|
|
691
|
+
* @example
|
|
692
|
+
* ```ts
|
|
693
|
+
* DATE_LENGTH // 10
|
|
694
|
+
* ```
|
|
695
|
+
*/
|
|
696
|
+
var DATE_LENGTH = 10;
|
|
697
|
+
/**
|
|
680
698
|
* Caps at `1024` the number of choices one schema field may offer, frozen.
|
|
681
699
|
*
|
|
682
700
|
* @remarks
|
|
@@ -4230,6 +4248,27 @@ function canonicalStringify(value) {
|
|
|
4230
4248
|
}, "canonicalStringify");
|
|
4231
4249
|
}
|
|
4232
4250
|
/**
|
|
4251
|
+
* Computes the number of days in a month of the Gregorian calendar.
|
|
4252
|
+
*
|
|
4253
|
+
* @remarks
|
|
4254
|
+
* February has 29 days in a year divisible by 4, except a century year not divisible
|
|
4255
|
+
* by 400. The caller supplies a month from 1 to 12.
|
|
4256
|
+
*
|
|
4257
|
+
* @param year - The calendar year
|
|
4258
|
+
* @param month - The month, from 1 for January to 12 for December
|
|
4259
|
+
* @returns The days in that month, from 28 to 31
|
|
4260
|
+
*
|
|
4261
|
+
* @example
|
|
4262
|
+
* ```ts
|
|
4263
|
+
* computeMonthLength(2024, 2) // 29
|
|
4264
|
+
* computeMonthLength(1900, 2) // 28
|
|
4265
|
+
* ```
|
|
4266
|
+
*/
|
|
4267
|
+
function computeMonthLength(year, month) {
|
|
4268
|
+
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
4269
|
+
return month === 2 ? 28 + INTRINSICS.numeric(leap) : 31 - INTRINSICS.numeric(month === 4 || month === 6 || month === 9 || month === 11);
|
|
4270
|
+
}
|
|
4271
|
+
/**
|
|
4233
4272
|
* Checks whether a supported ISO-8601 date or date-time names a real instant.
|
|
4234
4273
|
*
|
|
4235
4274
|
* @remarks
|
|
@@ -4260,9 +4299,7 @@ function matchesISOInstant(value) {
|
|
|
4260
4299
|
const month = INTRINSICS.numeric(monthText);
|
|
4261
4300
|
const day = INTRINSICS.numeric(dayText);
|
|
4262
4301
|
if (month < 1 || month > 12) return false;
|
|
4263
|
-
|
|
4264
|
-
const limit = month === 2 ? 28 + INTRINSICS.numeric(leap) : 31 - INTRINSICS.numeric(month === 4 || month === 6 || month === 9 || month === 11);
|
|
4265
|
-
if (day < 1 || day > limit) return false;
|
|
4302
|
+
if (day < 1 || day > computeMonthLength(year, month)) return false;
|
|
4266
4303
|
const hourText = match?.[4];
|
|
4267
4304
|
const minuteText = match?.[5];
|
|
4268
4305
|
const secondText = match?.[6];
|
|
@@ -4279,6 +4316,27 @@ function matchesISOInstant(value) {
|
|
|
4279
4316
|
return isSuccess(outcome) && outcome.value;
|
|
4280
4317
|
}
|
|
4281
4318
|
/**
|
|
4319
|
+
* Checks whether a string is exactly a calendar-valid `YYYY-MM-DD` date.
|
|
4320
|
+
*
|
|
4321
|
+
* @remarks
|
|
4322
|
+
* Refuses a date-time, surrounding whitespace, and a date the Gregorian calendar
|
|
4323
|
+
* lacks, such as `2026-02-29`. Backs a date string shape and {@link classifyFormat}'s
|
|
4324
|
+
* `date`.
|
|
4325
|
+
*
|
|
4326
|
+
* @param value - The candidate string
|
|
4327
|
+
* @returns True if `value` is a calendar-valid `YYYY-MM-DD`; false otherwise
|
|
4328
|
+
* @throws The exact value thrown by a redirected pattern dispatch
|
|
4329
|
+
*
|
|
4330
|
+
* @example
|
|
4331
|
+
* ```ts
|
|
4332
|
+
* matchesCalendarDate('2024-02-29') // true
|
|
4333
|
+
* matchesCalendarDate('2026-02-29') // false
|
|
4334
|
+
* ```
|
|
4335
|
+
*/
|
|
4336
|
+
function matchesCalendarDate(value) {
|
|
4337
|
+
return matchesPattern(/^\d{4}-\d{2}-\d{2}$/, value) && matchesISOInstant(value);
|
|
4338
|
+
}
|
|
4339
|
+
/**
|
|
4282
4340
|
* Classifies an already-bounded string against the pattern-only and
|
|
4283
4341
|
* calendar-checked {@link SchemaFormat} vocabulary.
|
|
4284
4342
|
*
|
|
@@ -4300,7 +4358,7 @@ function matchesISOInstant(value) {
|
|
|
4300
4358
|
function classifyFormat(value) {
|
|
4301
4359
|
if (matchesPattern(FORMAT_PATTERNS.uuid, value)) return "uuid";
|
|
4302
4360
|
if (matchesPattern(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/, value) && matchesISOInstant(value)) return "date-time";
|
|
4303
|
-
if (
|
|
4361
|
+
if (matchesCalendarDate(value)) return "date";
|
|
4304
4362
|
if (matchesPattern(/^\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/, value) && matchesISOInstant(`1970-01-01T${value}`)) return "time";
|
|
4305
4363
|
if (matchesPattern(FORMAT_PATTERNS.email, value)) return "email";
|
|
4306
4364
|
if (matchesPattern(FORMAT_PATTERNS.uri, value)) return "uri";
|
|
@@ -4517,8 +4575,10 @@ function preview(value) {
|
|
|
4517
4575
|
* OBTAIN the string — the reporter coerces through `parseString`, the auditor
|
|
4518
4576
|
* demands a primitive string — and share every constraint afterwards through
|
|
4519
4577
|
* this helper, so a constraint cannot drift between them.
|
|
4520
|
-
* Faults come out in declaration order — `min`, then `max`, then
|
|
4578
|
+
* Faults come out in declaration order — `date`, then `min`, then `max`, then
|
|
4521
4579
|
* `pattern` — because a report is read top to bottom and its order is public.
|
|
4580
|
+
* `expected` is `'date'` on a date string shape and `'string'` otherwise, as
|
|
4581
|
+
* {@link buildNumberFaults} names `'integer'`.
|
|
4522
4582
|
*
|
|
4523
4583
|
* The declaration's pattern is applied through an OWNED stateless rebuild
|
|
4524
4584
|
* ({@link readPattern}) asked through {@link matchesPattern}, so the shape's own
|
|
@@ -4565,11 +4625,20 @@ function preview(value) {
|
|
|
4565
4625
|
*/
|
|
4566
4626
|
function buildStringFaults(shape, value, path, pattern) {
|
|
4567
4627
|
return readValue(() => {
|
|
4628
|
+
const dated = INTRINSICS.own(shape, "date") && shape.date === true;
|
|
4629
|
+
const expected = dated ? "date" : "string";
|
|
4568
4630
|
const faults = [];
|
|
4631
|
+
if (dated && !matchesCalendarDate(value)) faults[faults.length] = {
|
|
4632
|
+
reason: "constraint",
|
|
4633
|
+
path,
|
|
4634
|
+
expected,
|
|
4635
|
+
constraint: "date",
|
|
4636
|
+
received: preview(value)
|
|
4637
|
+
};
|
|
4569
4638
|
if (INTRINSICS.own(shape, "min") && shape.min !== void 0 && !matchesStringLength(value, shape.min, void 0)) faults[faults.length] = {
|
|
4570
4639
|
reason: "constraint",
|
|
4571
4640
|
path,
|
|
4572
|
-
expected
|
|
4641
|
+
expected,
|
|
4573
4642
|
constraint: "min",
|
|
4574
4643
|
limit: shape.min,
|
|
4575
4644
|
received: preview(value)
|
|
@@ -4577,7 +4646,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
4577
4646
|
if (INTRINSICS.own(shape, "max") && shape.max !== void 0 && !matchesStringLength(value, void 0, shape.max)) faults[faults.length] = {
|
|
4578
4647
|
reason: "constraint",
|
|
4579
4648
|
path,
|
|
4580
|
-
expected
|
|
4649
|
+
expected,
|
|
4581
4650
|
constraint: "max",
|
|
4582
4651
|
limit: shape.max,
|
|
4583
4652
|
received: preview(value)
|
|
@@ -4588,7 +4657,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
4588
4657
|
faults[faults.length] = {
|
|
4589
4658
|
reason: "constraint",
|
|
4590
4659
|
path,
|
|
4591
|
-
expected
|
|
4660
|
+
expected,
|
|
4592
4661
|
constraint: "pattern",
|
|
4593
4662
|
...limit === void 0 ? {} : { limit },
|
|
4594
4663
|
received: preview(value)
|
|
@@ -4735,7 +4804,8 @@ function selectClosestFaults(reports) {
|
|
|
4735
4804
|
* A structural mapping used by {@link compileReporter} to fill a `Fault`'s
|
|
4736
4805
|
* `expected` field and by {@link compileAuditor} to fill an `AuditFault`'s:
|
|
4737
4806
|
* most shapes map to their own `category` (`numberShape` maps to
|
|
4738
|
-
* `'integer'` when `integer: true`, else `'number'
|
|
4807
|
+
* `'integer'` when `integer: true`, else `'number'`, and `stringShape` maps to
|
|
4808
|
+
* `'date'` when `date: true`, else `'string'`); `optionalShape` /
|
|
4739
4809
|
* `nullableShape` project through to their inner shape's kind, and `rawShape`
|
|
4740
4810
|
* (an arbitrary embedded schema with no fixed kind) projects to `'json'`.
|
|
4741
4811
|
*
|
|
@@ -4758,7 +4828,7 @@ function selectClosestFaults(reports) {
|
|
|
4758
4828
|
function shapeToKind(shape) {
|
|
4759
4829
|
return readValue(() => {
|
|
4760
4830
|
switch (shape.category) {
|
|
4761
|
-
case "string": return "string";
|
|
4831
|
+
case "string": return INTRINSICS.own(shape, "date") && shape.date === true ? "date" : "string";
|
|
4762
4832
|
case "number": return INTRINSICS.own(shape, "integer") && shape.integer === true ? "integer" : "number";
|
|
4763
4833
|
case "boolean": return "boolean";
|
|
4764
4834
|
case "null": return "null";
|
|
@@ -5032,7 +5102,7 @@ function renderList(entries) {
|
|
|
5032
5102
|
*
|
|
5033
5103
|
* @remarks
|
|
5034
5104
|
* An object lists its required keys first and marks the rest with `?`, at most `FAULT_LIST_LIMIT`
|
|
5035
|
-
* of them. A leaf or array keeps its numeric bounds and
|
|
5105
|
+
* of them. A leaf or array keeps its numeric bounds, pattern, and format, a union joins its forms with ` | `,
|
|
5036
5106
|
* and a schema that says nothing reads `any`. Each nested form takes half the room of its parent, and
|
|
5037
5107
|
* below 16 characters a form stops at its type, so a schema that holds itself still ends. Text past
|
|
5038
5108
|
* `room` is cut and closes with `…`. A type word outside JSON Schema's vocabulary is encoded.
|
|
@@ -5069,7 +5139,8 @@ function renderForm(schema, room = 160) {
|
|
|
5069
5139
|
"maximum",
|
|
5070
5140
|
"minItems",
|
|
5071
5141
|
"maxItems",
|
|
5072
|
-
"pattern"
|
|
5142
|
+
"pattern",
|
|
5143
|
+
"format"
|
|
5073
5144
|
].includes(key)).map(([key, value]) => `${key} ${preview(value)}`);
|
|
5074
5145
|
return truncateFaultText(forms !== void 0 ? deep ? forms.map((form) => renderForm(form, inner)).join(" | ") : "any" : enumeration !== void 0 ? enumeration.map((value) => preview(value)).join(" | ") : listed.length > 0 ? `{${[...listed, ...left > 0 ? [`and ${String(left)} more`] : []].join(", ")}}` : [
|
|
5075
5146
|
type === void 0 ? entries.length > 0 ? "object" : "any" : JSON_SCHEMA_TYPES.some((name) => name === type) ? type : preview(type),
|
|
@@ -5542,6 +5613,7 @@ var ShapePass = class ShapePass {
|
|
|
5542
5613
|
"min",
|
|
5543
5614
|
"max",
|
|
5544
5615
|
"pattern",
|
|
5616
|
+
"date",
|
|
5545
5617
|
"description",
|
|
5546
5618
|
"integer",
|
|
5547
5619
|
"additionalProperties",
|
|
@@ -5824,6 +5896,7 @@ var ShapePass = class ShapePass {
|
|
|
5824
5896
|
if (current.category === "string") {
|
|
5825
5897
|
if (current.min !== void 0 && typeof current.min !== "number") return this.#refuse("validateShape: string min must be a number", "min");
|
|
5826
5898
|
if (current.max !== void 0 && typeof current.max !== "number") return this.#refuse("validateShape: string max must be a number", "max");
|
|
5899
|
+
if (current.date !== void 0 && typeof current.date !== "boolean") return this.#refuse("validateShape: string date must be a boolean", "date");
|
|
5827
5900
|
if (current.pattern !== void 0) {
|
|
5828
5901
|
const pattern = current.pattern;
|
|
5829
5902
|
if (!isRegExp(pattern)) return this.#refuse("validateShape: string pattern must be a RegExp", "pattern");
|
|
@@ -5948,6 +6021,21 @@ var ShapePass = class ShapePass {
|
|
|
5948
6021
|
shape: "string"
|
|
5949
6022
|
}
|
|
5950
6023
|
});
|
|
6024
|
+
if (this.#domain === void 0 && current.date === true && current.pattern !== void 0) this.#domain = new ContractError("validateShape: a string shape declares date or pattern, never both", {
|
|
6025
|
+
code: "pattern",
|
|
6026
|
+
context: {
|
|
6027
|
+
path: pathOf(this.#path),
|
|
6028
|
+
shape: "string"
|
|
6029
|
+
}
|
|
6030
|
+
});
|
|
6031
|
+
if (this.#domain === void 0 && current.date === true && (typeof current.min === "number" && current.min > 10 || typeof current.max === "number" && current.max < 10)) this.#domain = new ContractError(`validateShape: a date string shape needs min and max to admit length 10`, {
|
|
6032
|
+
code: "range",
|
|
6033
|
+
context: {
|
|
6034
|
+
path: pathOf(this.#path),
|
|
6035
|
+
shape: "string",
|
|
6036
|
+
limit: 10
|
|
6037
|
+
}
|
|
6038
|
+
});
|
|
5951
6039
|
}
|
|
5952
6040
|
#inspect(source) {
|
|
5953
6041
|
if (this.#structure !== void 0) return true;
|
|
@@ -6812,11 +6900,13 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6812
6900
|
const min = this.#captureField(source, "min", path);
|
|
6813
6901
|
const max = this.#captureField(source, "max", path);
|
|
6814
6902
|
const pattern = this.#capturePattern(source, path);
|
|
6903
|
+
const date = this.#captureField(source, "date", path);
|
|
6815
6904
|
const description = this.#captureField(source, "description", path);
|
|
6816
6905
|
const fields = {
|
|
6817
6906
|
category: "string",
|
|
6818
6907
|
...min === void 0 ? {} : { min },
|
|
6819
6908
|
...max === void 0 ? {} : { max },
|
|
6909
|
+
...date === void 0 ? {} : { date },
|
|
6820
6910
|
...description === void 0 ? {} : { description }
|
|
6821
6911
|
};
|
|
6822
6912
|
if (pattern === void 0) return fields;
|
|
@@ -8995,6 +9085,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8995
9085
|
...owned.min !== void 0 ? { minLength: owned.min } : {},
|
|
8996
9086
|
...owned.max !== void 0 ? { maxLength: owned.max } : {},
|
|
8997
9087
|
...text !== void 0 ? { pattern: text } : {},
|
|
9088
|
+
...owned.date === true ? { format: "date" } : {},
|
|
8998
9089
|
...owned.description !== void 0 ? { description: owned.description } : {}
|
|
8999
9090
|
});
|
|
9000
9091
|
}
|
|
@@ -9096,11 +9187,12 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9096
9187
|
return compiled;
|
|
9097
9188
|
}
|
|
9098
9189
|
#stringGuard(owned) {
|
|
9099
|
-
|
|
9190
|
+
const guard = stringOf(INTRINSICS.rebase({
|
|
9100
9191
|
...owned.min === void 0 ? {} : { min: owned.min },
|
|
9101
9192
|
...owned.max === void 0 ? {} : { max: owned.max },
|
|
9102
9193
|
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
9103
9194
|
}, null));
|
|
9195
|
+
return owned.date === true ? whereOf(guard, matchesCalendarDate) : guard;
|
|
9104
9196
|
}
|
|
9105
9197
|
static #acceptShape(value) {
|
|
9106
9198
|
return isSuccess(attempt(() => {
|
|
@@ -9223,7 +9315,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9223
9315
|
const owned = this.#node(index);
|
|
9224
9316
|
switch (owned.category) {
|
|
9225
9317
|
case "string": {
|
|
9226
|
-
if (owned.min === void 0 && owned.max === void 0 && owned.pattern === void 0) return parseString;
|
|
9318
|
+
if (owned.min === void 0 && owned.max === void 0 && owned.pattern === void 0 && owned.date !== true) return parseString;
|
|
9227
9319
|
const guard = this.#stringGuard(owned);
|
|
9228
9320
|
return (value) => {
|
|
9229
9321
|
const parsed = parseString(value);
|
|
@@ -9428,13 +9520,14 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9428
9520
|
case "string": {
|
|
9429
9521
|
const node = owned;
|
|
9430
9522
|
const declared = owned.pattern;
|
|
9431
|
-
const
|
|
9523
|
+
const kind = owned.date === true ? "date" : "string";
|
|
9524
|
+
const refined = owned.min !== void 0 || owned.max !== void 0 || declared !== void 0 || owned.date === true;
|
|
9432
9525
|
const pattern = declared === void 0 ? void 0 : ownPattern(declared, "compileAuditor");
|
|
9433
9526
|
return (value, path) => {
|
|
9434
9527
|
if (!isString(value)) return [{
|
|
9435
9528
|
reason: "type",
|
|
9436
9529
|
path,
|
|
9437
|
-
expected:
|
|
9530
|
+
expected: kind,
|
|
9438
9531
|
received: preview(value)
|
|
9439
9532
|
}];
|
|
9440
9533
|
return refined ? buildStringFaults(node, value, path, pattern) : [];
|
|
@@ -9712,14 +9805,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9712
9805
|
case "string": {
|
|
9713
9806
|
const node = owned;
|
|
9714
9807
|
const declared = owned.pattern;
|
|
9715
|
-
const
|
|
9808
|
+
const kind = owned.date === true ? "date" : "string";
|
|
9809
|
+
const refined = owned.min !== void 0 || owned.max !== void 0 || declared !== void 0 || owned.date === true;
|
|
9716
9810
|
const pattern = declared === void 0 ? void 0 : ownPattern(declared, "compileReporter");
|
|
9717
9811
|
return (value, path) => {
|
|
9718
9812
|
const parsed = parseString(value);
|
|
9719
9813
|
if (parsed === void 0) return [{
|
|
9720
9814
|
reason: "type",
|
|
9721
9815
|
path,
|
|
9722
|
-
expected:
|
|
9816
|
+
expected: kind,
|
|
9723
9817
|
received: preview(value)
|
|
9724
9818
|
}];
|
|
9725
9819
|
return refined ? buildStringFaults(node, parsed, path, pattern) : [];
|
|
@@ -9982,6 +10076,13 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9982
10076
|
const owned = this.#node(index);
|
|
9983
10077
|
switch (owned.category) {
|
|
9984
10078
|
case "string": {
|
|
10079
|
+
if (owned.date === true) return (random) => {
|
|
10080
|
+
const year = 2e3 + INTRINSICS.floor(drawRandom(random, "string") * 100);
|
|
10081
|
+
const month = 1 + INTRINSICS.floor(drawRandom(random, "string") * 12);
|
|
10082
|
+
const length = computeMonthLength(year, month);
|
|
10083
|
+
const day = 1 + INTRINSICS.floor(drawRandom(random, "string") * length);
|
|
10084
|
+
return `${year}-${month < 10 ? "0" : ""}${month}-${day < 10 ? "0" : ""}${day}`;
|
|
10085
|
+
};
|
|
9985
10086
|
const min = owned.min ?? 0;
|
|
9986
10087
|
const max = owned.max ?? INTRINSICS.max(min, 12);
|
|
9987
10088
|
const length = INTRINSICS.max(min, INTRINSICS.min(max, 8));
|
|
@@ -11188,7 +11289,7 @@ var SchemaShaper = class SchemaShaper {
|
|
|
11188
11289
|
for (const index of iterateIndices(() => keys.length)) {
|
|
11189
11290
|
const key = keys[index];
|
|
11190
11291
|
if (key === void 0) continue;
|
|
11191
|
-
if (!INTRINSICS.reflect.apply(INTRINSICS.some, STRICT_SCHEMA_KEYWORDS, [(keyword) => keyword === key])) throw this.#refusal("a strict conversion refuses a keyword it cannot honour", key);
|
|
11292
|
+
if (!(INTRINSICS.reflect.apply(INTRINSICS.some, STRICT_SCHEMA_KEYWORDS, [(keyword) => keyword === key]) || key === "format" && schema.format === "date")) throw this.#refusal("a strict conversion refuses a keyword it cannot honour", key);
|
|
11192
11293
|
}
|
|
11193
11294
|
this.#inspectAnnotations(schema);
|
|
11194
11295
|
if (INTRINSICS.own(schema, "default") && !isJSONValue(schema.default)) throw this.#refusal("a default must be a JSON value", "default");
|
|
@@ -11197,7 +11298,11 @@ var SchemaShaper = class SchemaShaper {
|
|
|
11197
11298
|
const hasForm = INTRINSICS.own(schema, "enum") || INTRINSICS.own(schema, "oneOf") || INTRINSICS.own(schema, "anyOf");
|
|
11198
11299
|
const object = types.length === 0 && INTRINSICS.own(schema, "properties") ? ["object"] : types;
|
|
11199
11300
|
const effective = hasForm ? [] : object;
|
|
11200
|
-
this.#confine(schema, [
|
|
11301
|
+
this.#confine(schema, [
|
|
11302
|
+
"minLength",
|
|
11303
|
+
"maxLength",
|
|
11304
|
+
"format"
|
|
11305
|
+
], this.#names(effective, "string"));
|
|
11201
11306
|
this.#confine(schema, ["minimum", "maximum"], this.#names(effective, "number", "integer"));
|
|
11202
11307
|
this.#confine(schema, [
|
|
11203
11308
|
"items",
|
|
@@ -11383,8 +11488,12 @@ var SchemaShaper = class SchemaShaper {
|
|
|
11383
11488
|
const type = isString(category) && INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
11384
11489
|
if (type === "string") {
|
|
11385
11490
|
const bounds = deriveLengthBounds(INTRINSICS.own(schema, "minLength") ? schema.minLength : void 0, INTRINSICS.own(schema, "maxLength") ? schema.maxLength : void 0);
|
|
11491
|
+
const formatted = this.#strict && INTRINSICS.own(schema, "format") && schema.format === "date";
|
|
11492
|
+
const dated = formatted && (bounds.min ?? 0) <= 10 && (bounds.max ?? 10) >= 10;
|
|
11493
|
+
if (formatted && !dated) throw this.#refusal(`a date length range must admit length 10`, "format");
|
|
11386
11494
|
return stringShape(INTRINSICS.rebase({
|
|
11387
11495
|
...bounds,
|
|
11496
|
+
...dated ? { date: true } : {},
|
|
11388
11497
|
...description === void 0 ? {} : { description }
|
|
11389
11498
|
}, null));
|
|
11390
11499
|
}
|
|
@@ -11584,10 +11693,12 @@ function auditFaultShape() {
|
|
|
11584
11693
|
* A supplied pattern is captured by source and flags. The shape exposes a fresh
|
|
11585
11694
|
* frozen zero-state `RegExp` on every `pattern` read, so neither the caller's
|
|
11586
11695
|
* original nor a value read from the shape can drift later compiled artifacts.
|
|
11696
|
+
* `date: true` restricts the string to a calendar-valid `YYYY-MM-DD`, as
|
|
11697
|
+
* {@link dateShape} builds it.
|
|
11587
11698
|
*
|
|
11588
|
-
* @param options - Optional length (`min` / `max`), `pattern`, and `description`
|
|
11699
|
+
* @param options - Optional length (`min` / `max`), `pattern`, `date`, and `description`
|
|
11589
11700
|
* @returns A string shape
|
|
11590
|
-
* @throws {ContractError} When a present bound is invalid, `pattern` is not a `RegExp`, or `pattern`
|
|
11701
|
+
* @throws {ContractError} When a present bound is invalid, `pattern` is not a `RegExp`, `pattern` has flags, `date` is not a boolean, or `date` meets `pattern` or a bound that excludes length 10
|
|
11591
11702
|
*
|
|
11592
11703
|
* @example
|
|
11593
11704
|
* ```ts
|
|
@@ -11600,6 +11711,7 @@ function stringShape(options) {
|
|
|
11600
11711
|
"min",
|
|
11601
11712
|
"max",
|
|
11602
11713
|
"pattern",
|
|
11714
|
+
"date",
|
|
11603
11715
|
"description"
|
|
11604
11716
|
], "stringShape", "string");
|
|
11605
11717
|
const pattern = safe?.pattern;
|
|
@@ -11652,6 +11764,7 @@ function stringShape(options) {
|
|
|
11652
11764
|
...safe?.min === void 0 ? {} : { min: safe.min },
|
|
11653
11765
|
...safe?.max === void 0 ? {} : { max: safe.max },
|
|
11654
11766
|
...patternSnapshot === void 0 ? {} : { pattern: new INTRINSICS.pattern(patternSnapshot.source) },
|
|
11767
|
+
...safe?.date === void 0 || safe.date === false ? {} : { date: safe.date },
|
|
11655
11768
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
11656
11769
|
};
|
|
11657
11770
|
if (patternSnapshot === void 0 && !ShapePass.polluted()) return ShapePass.seal(shape);
|
|
@@ -11735,6 +11848,42 @@ function integerShape(options) {
|
|
|
11735
11848
|
}, "integerShape");
|
|
11736
11849
|
}
|
|
11737
11850
|
/**
|
|
11851
|
+
* Builds a calendar-date {@link StringShape} — forces `date: true`.
|
|
11852
|
+
*
|
|
11853
|
+
* @remarks
|
|
11854
|
+
* The guard and the parser admit exactly a calendar-valid `YYYY-MM-DD` and coerce
|
|
11855
|
+
* nothing, so `2026-02-30`, `2026-1-10`, and a date-time all refuse. The emitted JSON
|
|
11856
|
+
* Schema carries `"format": "date"`. Options other than `min`, `max`, and `description`
|
|
11857
|
+
* refuse, because a dropped `pattern` would widen the shape.
|
|
11858
|
+
*
|
|
11859
|
+
* @param options - Optional length bounds and `description` (no `date` or `pattern` key)
|
|
11860
|
+
* @returns A date string shape
|
|
11861
|
+
* @throws {ContractError} When an option key is unknown, or a present bound is invalid or excludes length 10
|
|
11862
|
+
*
|
|
11863
|
+
* @example
|
|
11864
|
+
* ```ts
|
|
11865
|
+
* createContract(dateShape()).is('2024-02-29') // true
|
|
11866
|
+
* ```
|
|
11867
|
+
*/
|
|
11868
|
+
function dateShape(options) {
|
|
11869
|
+
return contain(() => {
|
|
11870
|
+
const safe = readOptions(options, [
|
|
11871
|
+
"min",
|
|
11872
|
+
"max",
|
|
11873
|
+
"description"
|
|
11874
|
+
], "dateShape", "string");
|
|
11875
|
+
refuseOptionKeys(options, [
|
|
11876
|
+
"min",
|
|
11877
|
+
"max",
|
|
11878
|
+
"description"
|
|
11879
|
+
], "dateShape", "string");
|
|
11880
|
+
return stringShape({
|
|
11881
|
+
...safe,
|
|
11882
|
+
date: true
|
|
11883
|
+
});
|
|
11884
|
+
}, "dateShape");
|
|
11885
|
+
}
|
|
11886
|
+
/**
|
|
11738
11887
|
* Builds a {@link BooleanShape}.
|
|
11739
11888
|
*
|
|
11740
11889
|
* @param options - Optional `description`
|
|
@@ -12291,15 +12440,18 @@ function rawShape(schema) {
|
|
|
12291
12440
|
*
|
|
12292
12441
|
* STRICT: `{ strict: true }` is the door for a foreign schema. It refuses, with
|
|
12293
12442
|
* code `structure`, every node the lenient walk would widen, every keyword it
|
|
12294
|
-
* would ignore (`pattern`, `format
|
|
12295
|
-
* `propertyNames`, `prefixItems`, and anything else outside
|
|
12443
|
+
* would ignore (`pattern`, every `format` but `'date'` on a string node, `nullable`,
|
|
12444
|
+
* `patternProperties`, `propertyNames`, `prefixItems`, and anything else outside
|
|
12296
12445
|
* {@link STRICT_SCHEMA_KEYWORDS}), a malformed keyword or annotation, a boolean schema, an `enum` entry
|
|
12297
|
-
* that is neither a finite primitive nor `null`, a contradictory bound, a
|
|
12446
|
+
* that is neither a finite primitive nor `null`, a contradictory bound, a date format whose
|
|
12447
|
+
* length bounds exclude length 10, a `required` name not in
|
|
12298
12448
|
* `properties`, breadth past {@link INFER_BREADTH_LIMIT}, and a `default` with no
|
|
12299
12449
|
* optional property to fill. An object with no `additionalProperties` closes
|
|
12300
12450
|
* rather than opens, and `oneOf` keeps exactly-one, because a strict walk reads
|
|
12301
12451
|
* every keyword that could keep two arms apart. Below a `oneOf` that object refuses
|
|
12302
|
-
* instead, since closing it could change which arms match.
|
|
12452
|
+
* instead, since closing it could change which arms match. A strict conversion is the
|
|
12453
|
+
* one walk that asserts a `format`: it reads `format: 'date'` on a string node as a
|
|
12454
|
+
* date string shape, so a compiled {@link dateShape} round-trips.
|
|
12303
12455
|
*
|
|
12304
12456
|
* @param schema - The JSON Schema value to convert
|
|
12305
12457
|
* @param options - Optional `strict`, default `false`
|
|
@@ -12343,6 +12495,6 @@ function schemaToShape(schema, options) {
|
|
|
12343
12495
|
});
|
|
12344
12496
|
}, "schemaToShape");
|
|
12345
12497
|
}
|
|
12346
|
-
export { CHOICE_LIMIT, CLONE_NODE_LIMIT, COMPILE_DEPTH_LIMIT, COMPILE_NODE_LIMIT, CONTRACT_CODES, CONTRACT_ERROR_BRAND, ContractCompiler, ContractError, FAULT_CONSTRAINTS, FAULT_KINDS, FAULT_LIMIT, FAULT_LIST_LIMIT, FAULT_SCHEMA_LIMIT, FAULT_TEXT_LIMIT, FORMAT_MAX_LENGTH, FORMAT_PATTERNS, GENERATION_ATTEMPT_LIMIT, GUARD_DEPTH_LIMIT, INFER_BREADTH_LIMIT, INFER_DEPTH_LIMIT, INFER_ENUM_LIMIT, INTRINSICS, JSONCloner, JSON_SCHEMA_TYPES, NAME_LIMIT, NODE_LIMIT, PRESENCE_MASK_LIMIT, PREVIEW_LIMIT, SHAPE_FIELDS, STRICT_SCHEMA_KEYWORDS, STRING_LIMIT, SchemaCloner, ShapeCloner, ShapeValidator, TEXT_LIMIT, admitMember, admitValue, admitVisited, andOf, appendEntries, arrayOf, arrayShape, attempt, auditFaultShape, booleanShape, boundsOf, buildArrayFaults, buildNumberFaults, buildPositions, buildSampleMemo, buildStringFaults, canonicalStringify, classifyFormat, cloneJSONRecord, cloneJSONValue, cloneSchema, cloneShape, collectEntries, collectMembers, comparePrimitives, compileAuditor, compileGenerator, compileGuard, compileParser, compileReporter, compileSchema, complementOf, computeSurrogatePairs, contain, createContract, deriveLengthBounds, deriveRangeBounds, describeFaultLine, describeFaults, drawRandom, encodeLeaf, encodeParts, enumOf, enumerableKeys, enumerableSymbolCount, errorToMessage, escapeRegExp, extractForms, extractPath, extractSchema, failure, holds, inferPrimitiveEnum, instanceOf, integerShape, intersectionOf, isArray, isArrayBuffer, isArrayBufferView, isAsyncFunction, isAsyncGeneratorFunction, isAsyncIterable, isAuditFault, isBigInt, isBigInt64Array, isBigUint64Array, isBoolean, isBoundedJSONRecord, isBoundedJSONValue, isConstructor, isContractError, isDataView, isDate, isDefined, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isError, isFailure, isFalse, isFault, isFaultPath, isFiniteNumber, isFloat32Array, isFloat64Array, isFunction, isGeneratorFunction, isInstance, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isJSONPrimitive, isJSONValue, isLiteralValue, isMap, isNonEmptyArray, isNonEmptyMap, isNonEmptyObject, isNonEmptySet, isNonEmptyString, isNonNegativeInteger, isNonNegativeNumber, isNull, isNullableBoolean, isNullableNumber, isNullableString, isNumber, isObject, isPromise, isPromiseLike, isRecord, isRegExp, isSet, isSharedArrayBuffer, isString, isSuccess, isSymbol, isTrue, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isZeroArg, isZeroArgAsync, isZeroArgAsyncGenerator, isZeroArgGenerator, iterateIndices, jsonShape, keyOf, lazyOf, limitEntries, literalOf, literalShape, mapOf, matchOf, matchesExactKeys, matchesISOInstant, matchesJSONDepth, matchesJSONValue, matchesLosslessRepair, matchesMember, matchesPattern, matchesRecordBrand, matchesStringLength, matchesUndefinedField, matchesVisited, notOf, nullShape, nullableOf, nullableShape, numberShape, objectOf, objectShape, omitOf, omitVisited, oneOfShape, optionalOf, optionalShape, orOf, ownPattern, ownShape, parseArray, parseArrayField, parseBoolean, parseBooleanField, parseEnum, parseEnumField, parseEnumValues, parseInteger, parseIntegerField, parseJSON, parseJSONAs, parseJSONValue, parseJSONValueField, parseNull, parseNullField, parseNumber, parseNumberField, parseRecord, parseRecordField, parseString, parseStringField, pathOf, pickOf, pinMembers, preview, previewParts, rawShape, readArrayEntries, readGuardShape, readIndexView, readMapEntries, readOptions, readPattern, readPatternFlags, readPatternSource, readPresence, readSampleMemo, readSetEntries, readValue, recordOf, recordShape, refuseExpansion, refuseOptionKeys, renderForm, renderList, renderPath, resolveField, retainDepth, samplesToFormat, samplesToSchema, sanitizeBudget, sanitizeDepth, schemaToObject, schemaToParameters, schemaToShape, seededRandom, selectClosestFaults, setOf, shapeShape, shapeToKind, sortValues, stringOf, stringShape, stringToFormat, success, transformOf, truncateFaultText, tupleOf, unifySchemas, unionOf, unionShape, validateShape, valueToSchema, whereOf };
|
|
12498
|
+
export { CHOICE_LIMIT, CLONE_NODE_LIMIT, COMPILE_DEPTH_LIMIT, COMPILE_NODE_LIMIT, CONTRACT_CODES, CONTRACT_ERROR_BRAND, ContractCompiler, ContractError, DATE_LENGTH, FAULT_CONSTRAINTS, FAULT_KINDS, FAULT_LIMIT, FAULT_LIST_LIMIT, FAULT_SCHEMA_LIMIT, FAULT_TEXT_LIMIT, FORMAT_MAX_LENGTH, FORMAT_PATTERNS, GENERATION_ATTEMPT_LIMIT, GUARD_DEPTH_LIMIT, INFER_BREADTH_LIMIT, INFER_DEPTH_LIMIT, INFER_ENUM_LIMIT, INTRINSICS, JSONCloner, JSON_SCHEMA_TYPES, NAME_LIMIT, NODE_LIMIT, PRESENCE_MASK_LIMIT, PREVIEW_LIMIT, SHAPE_FIELDS, STRICT_SCHEMA_KEYWORDS, STRING_LIMIT, SchemaCloner, ShapeCloner, ShapeValidator, TEXT_LIMIT, admitMember, admitValue, admitVisited, andOf, appendEntries, arrayOf, arrayShape, attempt, auditFaultShape, booleanShape, boundsOf, buildArrayFaults, buildNumberFaults, buildPositions, buildSampleMemo, buildStringFaults, canonicalStringify, classifyFormat, cloneJSONRecord, cloneJSONValue, cloneSchema, cloneShape, collectEntries, collectMembers, comparePrimitives, compileAuditor, compileGenerator, compileGuard, compileParser, compileReporter, compileSchema, complementOf, computeMonthLength, computeSurrogatePairs, contain, createContract, dateShape, deriveLengthBounds, deriveRangeBounds, describeFaultLine, describeFaults, drawRandom, encodeLeaf, encodeParts, enumOf, enumerableKeys, enumerableSymbolCount, errorToMessage, escapeRegExp, extractForms, extractPath, extractSchema, failure, holds, inferPrimitiveEnum, instanceOf, integerShape, intersectionOf, isArray, isArrayBuffer, isArrayBufferView, isAsyncFunction, isAsyncGeneratorFunction, isAsyncIterable, isAuditFault, isBigInt, isBigInt64Array, isBigUint64Array, isBoolean, isBoundedJSONRecord, isBoundedJSONValue, isConstructor, isContractError, isDataView, isDate, isDefined, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isError, isFailure, isFalse, isFault, isFaultPath, isFiniteNumber, isFloat32Array, isFloat64Array, isFunction, isGeneratorFunction, isInstance, isInt16Array, isInt32Array, isInt8Array, isInteger, isIterable, isJSONPrimitive, isJSONValue, isLiteralValue, isMap, isNonEmptyArray, isNonEmptyMap, isNonEmptyObject, isNonEmptySet, isNonEmptyString, isNonNegativeInteger, isNonNegativeNumber, isNull, isNullableBoolean, isNullableNumber, isNullableString, isNumber, isObject, isPromise, isPromiseLike, isRecord, isRegExp, isSet, isSharedArrayBuffer, isString, isSuccess, isSymbol, isTrue, isUint16Array, isUint32Array, isUint8Array, isUint8ClampedArray, isUndefined, isWeakMap, isWeakSet, isZeroArg, isZeroArgAsync, isZeroArgAsyncGenerator, isZeroArgGenerator, iterateIndices, jsonShape, keyOf, lazyOf, limitEntries, literalOf, literalShape, mapOf, matchOf, matchesCalendarDate, matchesExactKeys, matchesISOInstant, matchesJSONDepth, matchesJSONValue, matchesLosslessRepair, matchesMember, matchesPattern, matchesRecordBrand, matchesStringLength, matchesUndefinedField, matchesVisited, notOf, nullShape, nullableOf, nullableShape, numberShape, objectOf, objectShape, omitOf, omitVisited, oneOfShape, optionalOf, optionalShape, orOf, ownPattern, ownShape, parseArray, parseArrayField, parseBoolean, parseBooleanField, parseEnum, parseEnumField, parseEnumValues, parseInteger, parseIntegerField, parseJSON, parseJSONAs, parseJSONValue, parseJSONValueField, parseNull, parseNullField, parseNumber, parseNumberField, parseRecord, parseRecordField, parseString, parseStringField, pathOf, pickOf, pinMembers, preview, previewParts, rawShape, readArrayEntries, readGuardShape, readIndexView, readMapEntries, readOptions, readPattern, readPatternFlags, readPatternSource, readPresence, readSampleMemo, readSetEntries, readValue, recordOf, recordShape, refuseExpansion, refuseOptionKeys, renderForm, renderList, renderPath, resolveField, retainDepth, samplesToFormat, samplesToSchema, sanitizeBudget, sanitizeDepth, schemaToObject, schemaToParameters, schemaToShape, seededRandom, selectClosestFaults, setOf, shapeShape, shapeToKind, sortValues, stringOf, stringShape, stringToFormat, success, transformOf, truncateFaultText, tupleOf, unifySchemas, unionOf, unionShape, validateShape, valueToSchema, whereOf };
|
|
12347
12499
|
|
|
12348
12500
|
//# sourceMappingURL=index.js.map
|