arktype 3.0.0-beta.1 → 3.0.0-rc.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.
- package/README.md +48 -144
- package/out/_internal/regex/charset.d.ts +24 -0
- package/out/_internal/regex/escape.d.ts +29 -0
- package/out/_internal/regex/execArray.d.ts +24 -0
- package/out/_internal/regex/group.d.ts +34 -0
- package/out/_internal/regex/index.d.ts +2 -0
- package/out/_internal/regex/internal.d.ts +9 -0
- package/out/_internal/regex/parse.d.ts +10 -0
- package/out/_internal/regex/quantify.d.ts +59 -0
- package/out/_internal/regex/regex.d.ts +38 -0
- package/out/_internal/regex/state.d.ts +383 -0
- package/out/_internal/schema/codecs.d.ts +2 -0
- package/out/_internal/schema/config.d.ts +103 -0
- package/out/_internal/schema/constraint.d.ts +78 -0
- package/out/_internal/schema/errors.d.ts +1 -0
- package/out/_internal/schema/generic.d.ts +85 -0
- package/out/_internal/schema/i18n.d.ts +3 -0
- package/out/_internal/schema/index.d.ts +54 -0
- package/out/_internal/schema/internal.d.ts +15 -0
- package/out/_internal/schema/intrinsic.d.ts +28 -0
- package/out/_internal/schema/kinds.d.ts +76 -0
- package/out/_internal/schema/module.d.ts +23 -0
- package/out/_internal/schema/mutatesInput.d.ts +2 -0
- package/out/_internal/schema/node.d.ts +349 -0
- package/out/_internal/schema/parse.d.ts +77 -0
- package/out/_internal/schema/parseSchema.d.ts +116 -0
- package/out/_internal/schema/predicate.d.ts +46 -0
- package/out/_internal/schema/refinements/after.d.ts +32 -0
- package/out/_internal/schema/refinements/before.d.ts +32 -0
- package/out/_internal/schema/refinements/divisor.d.ts +41 -0
- package/out/_internal/schema/refinements/exactLength.d.ts +43 -0
- package/out/_internal/schema/refinements/kinds.d.ts +40 -0
- package/out/_internal/schema/refinements/max.d.ts +33 -0
- package/out/_internal/schema/refinements/maxLength.d.ts +32 -0
- package/out/_internal/schema/refinements/min.d.ts +33 -0
- package/out/_internal/schema/refinements/minLength.d.ts +32 -0
- package/out/_internal/schema/refinements/notEqual.d.ts +38 -0
- package/out/_internal/schema/refinements/notEqualLength.d.ts +38 -0
- package/out/_internal/schema/refinements/pattern.d.ts +43 -0
- package/out/_internal/schema/refinements/range.d.ts +101 -0
- package/out/_internal/schema/roots/alias.d.ts +49 -0
- package/out/_internal/schema/roots/basis.d.ts +12 -0
- package/out/_internal/schema/roots/domain.d.ts +45 -0
- package/out/_internal/schema/roots/intersection.d.ts +72 -0
- package/out/_internal/schema/roots/morph.d.ts +59 -0
- package/out/_internal/schema/roots/proto.d.ts +42 -0
- package/out/_internal/schema/roots/root.d.ts +163 -0
- package/out/_internal/schema/roots/union.d.ts +146 -0
- package/out/_internal/schema/roots/unit.d.ts +36 -0
- package/out/_internal/schema/runtime.d.ts +29 -0
- package/out/_internal/schema/runtimeLean.d.ts +2 -0
- package/out/_internal/schema/runtimeNone.d.ts +2 -0
- package/out/_internal/schema/runtimeNoneLean.d.ts +2 -0
- package/out/_internal/schema/scope.d.ts +203 -0
- package/out/_internal/schema/shared/codecs.d.ts +5 -0
- package/out/_internal/schema/shared/compile.d.ts +144 -0
- package/out/_internal/schema/shared/configured.d.ts +14 -0
- package/out/_internal/schema/shared/declare.d.ts +100 -0
- package/out/_internal/schema/shared/disjoint.d.ts +45 -0
- package/out/_internal/schema/shared/enWriters.d.ts +30 -0
- package/out/_internal/schema/shared/errorModes.d.ts +25 -0
- package/out/_internal/schema/shared/errorWriters.d.ts +34 -0
- package/out/_internal/schema/shared/errors.d.ts +264 -0
- package/out/_internal/schema/shared/implement.d.ts +154 -0
- package/out/_internal/schema/shared/inPlace.d.ts +8 -0
- package/out/_internal/schema/shared/issueFeatures.d.ts +38 -0
- package/out/_internal/schema/shared/issues.d.ts +261 -0
- package/out/_internal/schema/shared/issuesLean.d.ts +80 -0
- package/out/_internal/schema/shared/issuesNone.d.ts +22 -0
- package/out/_internal/schema/shared/issuesNoneLean.d.ts +20 -0
- package/out/_internal/schema/shared/issuesUndetailed.d.ts +25 -0
- package/out/_internal/schema/shared/jsonSchema.d.ts +153 -0
- package/out/_internal/schema/shared/listed.d.ts +15 -0
- package/out/_internal/schema/shared/locale.d.ts +149 -0
- package/out/_internal/schema/shared/registry.d.ts +16 -0
- package/out/_internal/schema/shared/render.d.ts +3 -0
- package/out/_internal/schema/shared/rootApply.d.ts +19 -0
- package/out/_internal/schema/shared/sets.d.ts +26 -0
- package/out/_internal/schema/shared/standardFailure.d.ts +22 -0
- package/out/_internal/schema/shared/standardSchema.d.ts +124 -0
- package/out/_internal/schema/shared/toJsonSchema.d.ts +229 -0
- package/out/_internal/schema/shared/transform.d.ts +35 -0
- package/out/_internal/schema/shared/traversal.d.ts +288 -0
- package/out/_internal/schema/shared/utils.d.ts +59 -0
- package/out/_internal/schema/structure/index.d.ts +43 -0
- package/out/_internal/schema/structure/optional.d.ts +61 -0
- package/out/_internal/schema/structure/prop.d.ts +45 -0
- package/out/_internal/schema/structure/required.d.ts +49 -0
- package/out/_internal/schema/structure/sequence.d.ts +127 -0
- package/out/_internal/schema/structure/shared.d.ts +3 -0
- package/out/_internal/schema/structure/structure.d.ts +178 -0
- package/out/_internal/schema/units.d.ts +123 -0
- package/out/_internal/sets/algebra.d.ts +26 -0
- package/out/_internal/sets/constraint.d.ts +19 -0
- package/out/_internal/sets/engine.d.ts +2 -0
- package/out/_internal/sets/formats.d.ts +23 -0
- package/out/_internal/sets/fromJsonSchema.d.ts +15 -0
- package/out/_internal/sets/implement.d.ts +16 -0
- package/out/_internal/sets/index.d.ts +2 -0
- package/out/_internal/sets/internal.d.ts +20 -0
- package/out/_internal/sets/intersections.d.ts +5 -0
- package/out/_internal/sets/kinds.d.ts +3 -0
- package/out/_internal/sets/refinements.d.ts +14 -0
- package/out/_internal/sets/roots/alias.d.ts +3 -0
- package/out/_internal/sets/roots/domain.d.ts +3 -0
- package/out/_internal/sets/roots/intersection.d.ts +4 -0
- package/out/_internal/sets/roots/morph.d.ts +4 -0
- package/out/_internal/sets/roots/proto.d.ts +3 -0
- package/out/_internal/sets/roots/union.d.ts +16 -0
- package/out/_internal/sets/roots/unit.d.ts +3 -0
- package/out/_internal/sets/structure/index.d.ts +3 -0
- package/out/_internal/sets/structure/prop.d.ts +8 -0
- package/out/_internal/sets/structure/sequence.d.ts +3 -0
- package/out/_internal/sets/structure/structure.d.ts +3 -0
- package/out/_internal/sets/toJsonSchema.d.ts +25 -0
- package/out/_internal/util/arrays.d.ts +130 -0
- package/out/_internal/util/clone.d.ts +4 -0
- package/out/_internal/util/describe.d.ts +25 -0
- package/out/_internal/util/domain.d.ts +49 -0
- package/out/_internal/util/errors.d.ts +29 -0
- package/out/_internal/util/facts.d.ts +22 -0
- package/out/_internal/util/flatMorph.d.ts +41 -0
- package/out/_internal/util/formats.d.ts +8 -0
- package/out/_internal/util/functions.d.ts +33 -0
- package/out/_internal/util/generics.d.ts +65 -0
- package/out/_internal/util/get.d.ts +9 -0
- package/out/_internal/util/hkt.d.ts +27 -0
- package/out/_internal/util/i18n.d.ts +12 -0
- package/out/_internal/util/index.d.ts +28 -0
- package/out/_internal/util/internal.d.ts +2 -0
- package/out/_internal/util/intersections.d.ts +50 -0
- package/out/_internal/util/isomorphic.d.ts +8 -0
- package/out/_internal/util/keys.d.ts +18 -0
- package/out/_internal/util/lazily.d.ts +7 -0
- package/out/_internal/util/numbers.d.ts +82 -0
- package/out/_internal/util/objectKinds.d.ts +157 -0
- package/out/_internal/util/path.d.ts +16 -0
- package/out/_internal/util/primitive.d.ts +16 -0
- package/out/_internal/util/records.d.ts +173 -0
- package/out/_internal/util/registry.d.ts +13 -0
- package/out/_internal/util/scanner.d.ts +110 -0
- package/out/_internal/util/serialize.d.ts +40 -0
- package/out/_internal/util/strings.d.ts +35 -0
- package/out/_internal/util/traits.d.ts +89 -0
- package/out/_internal/util/unionToTuple.d.ts +14 -0
- package/out/_internal/util/weak.d.ts +8 -0
- package/out/attributes.d.ts +15 -11
- package/out/checkers.d.ts +1 -1
- package/out/chunk-22RQSVV3.js +0 -0
- package/out/chunk-3HZC67MT.js +965 -0
- package/out/chunk-3KBOPH3J.js +1564 -0
- package/out/chunk-3SH2BQ4T.js +15 -0
- package/out/chunk-46PLKNON.js +126 -0
- package/out/{chunk-G45P2CLE.js → chunk-4FTHWK63.js} +6 -1
- package/out/{chunk-UK5UGFOX.js → chunk-4HREHNIV.js} +23 -11
- package/out/chunk-4NZCLVBD.js +14 -0
- package/out/chunk-6FMBO47M.js +7 -0
- package/out/chunk-7TALAWYX.js +10 -0
- package/out/{chunk-DLWCO4RV.js → chunk-BCYKO54Z.js} +11 -2
- package/out/chunk-BDJVONP7.js +168 -0
- package/out/{chunk-E3DZXEZK.js → chunk-C2JOL5M2.js} +4 -2
- package/out/{chunk-2U2AWCI3.js → chunk-C2V6QPE3.js} +126 -97
- package/out/chunk-C3B4IFQL.js +6408 -0
- package/out/chunk-CWSF57VM.js +10 -0
- package/out/chunk-F5BACUJS.js +7 -0
- package/out/{chunk-EVCML554.js → chunk-FWRSDUJ2.js} +14 -5
- package/out/chunk-GID3FSQA.js +161 -0
- package/out/chunk-I2KLQ2HA.js +22 -0
- package/out/chunk-K7SLP5D2.js +84 -0
- package/out/chunk-KQPSKGUI.js +9 -0
- package/out/chunk-KXOC3JEX.js +8234 -0
- package/out/chunk-KXOSUYRA.js +405 -0
- package/out/chunk-LHNEY6QW.js +0 -0
- package/out/chunk-MTWKGAGO.js +323 -0
- package/out/chunk-NU73GNCX.js +170 -0
- package/out/{chunk-RAZ3TXOE.js → chunk-P5W67FQH.js} +34 -19
- package/out/chunk-PDLTEX6Z.js +1118 -0
- package/out/{chunk-VV6YDN2D.js → chunk-QUS4IAXJ.js} +304 -263
- package/out/chunk-R7WXIH5P.js +94 -0
- package/out/chunk-TACLEIDM.js +95 -0
- package/out/chunk-UJJGG4VQ.js +71 -0
- package/out/chunk-US6BWOGJ.js +109 -0
- package/out/chunk-VMMMOO4R.js +945 -0
- package/out/chunk-VYMXHSFR.js +439 -0
- package/out/chunk-W2XOI5DY.js +141 -0
- package/out/chunk-WC5L5YRP.js +25 -0
- package/out/{chunk-NRIGTMPM.js → chunk-WKOSL2G6.js} +4 -1
- package/out/chunk-XLG66ZDF.js +0 -0
- package/out/{chunk-5U2ZMDNI.js → chunk-XNUAQSJ4.js} +52 -30
- package/out/chunk-ZRYEHGSG.js +79 -0
- package/out/cli.d.ts +28 -0
- package/out/cli.js +636 -0
- package/out/codecs.d.ts +6 -5
- package/out/codecs.js +23 -7
- package/out/config.d.ts +5 -17
- package/out/config.js +11 -2
- package/out/declare.d.ts +40 -12
- package/out/deferred.d.ts +2 -2
- package/out/errors.d.ts +1 -1
- package/out/errors.js +10 -2
- package/out/fn.d.ts +20 -13
- package/out/fromJsonSchema.d.ts +18 -0
- package/out/generic.d.ts +9 -6
- package/out/i18n.d.ts +2 -2
- package/out/i18n.js +9 -2
- package/out/index.d.ts +16 -7
- package/out/index.js +25 -8
- package/out/internal.d.ts +4 -0
- package/out/internal.js +41 -17
- package/out/island.js +22 -6
- package/out/islandDependency.js +42 -18
- package/out/jsonSchema/infer.d.ts +805 -0
- package/out/jsonSchema/validate.d.ts +416 -0
- package/out/keywords/Array.d.ts +2 -2
- package/out/keywords/builtins.d.ts +3 -3
- package/out/keywords/constructors.d.ts +1 -1
- package/out/keywords/functions.d.ts +5 -192
- package/out/keywords/keywords.d.ts +3 -3
- package/out/keywords/number.d.ts +2 -2
- package/out/keywords/phrases.d.ts +4 -191
- package/out/keywords/string.d.ts +3 -3
- package/out/keywords/ts.d.ts +18 -18
- package/out/keywords/ts.js +19 -4
- package/out/locale.d.ts +6 -2
- package/out/locales/de.js +9 -2
- package/out/locales/en.js +21 -5
- package/out/match.d.ts +13 -10
- package/out/module.d.ts +2 -2
- package/out/mutatesInput.js +18 -3
- package/out/nary.d.ts +220 -194
- package/out/parser/ast/bounds.d.ts +2 -2
- package/out/parser/ast/default.d.ts +2 -2
- package/out/parser/ast/divisor.d.ts +2 -2
- package/out/parser/ast/generic.d.ts +2 -2
- package/out/parser/ast/infer.d.ts +1 -1
- package/out/parser/ast/keyof.d.ts +2 -2
- package/out/parser/ast/utils.d.ts +1 -1
- package/out/parser/ast/validate.d.ts +2 -2
- package/out/parser/definition.d.ts +4 -4
- package/out/parser/objectLiteral.d.ts +2 -2
- package/out/parser/property.d.ts +2 -2
- package/out/parser/reduce/dynamic.d.ts +2 -2
- package/out/parser/reduce/shared.d.ts +1 -1
- package/out/parser/reduce/static.d.ts +1 -1
- package/out/parser/shift/operand/enclosed.d.ts +2 -2
- package/out/parser/shift/operand/genericArgs.d.ts +2 -2
- package/out/parser/shift/operand/operand.d.ts +1 -1
- package/out/parser/shift/operand/unenclosed.d.ts +2 -2
- package/out/parser/shift/operand/unresolvable.d.ts +2 -2
- package/out/parser/shift/operator/bounds.d.ts +2 -2
- package/out/parser/shift/operator/brand.d.ts +2 -2
- package/out/parser/shift/operator/default.d.ts +4 -4
- package/out/parser/shift/operator/divisor.d.ts +1 -1
- package/out/parser/shift/operator/operator.d.ts +1 -1
- package/out/parser/shift/tokens.d.ts +1 -1
- package/out/parser/string.d.ts +2 -2
- package/out/parser/tupleExpressions.d.ts +6 -8
- package/out/parser/tupleLiteral.d.ts +2 -2
- package/out/precompiled.d.ts +1 -1
- package/out/precompiled.js +16 -2
- package/out/regex.d.ts +1 -0
- package/out/regex.js +2 -0
- package/out/runtime.d.ts +10 -8
- package/out/runtime.js +6 -4
- package/out/runtimeDevelopment.js +70 -52
- package/out/schema.d.ts +1 -0
- package/out/schema.js +15 -0
- package/out/schemaInternal.d.ts +1 -0
- package/out/schemaInternal.js +1401 -0
- package/out/schemaIssueFeatures.d.ts +1 -0
- package/out/schemaRuntime.d.ts +1 -0
- package/out/schemaRuntime.js +7 -0
- package/out/schemaRuntimeLean.d.ts +1 -0
- package/out/schemaRuntimeLean.js +1870 -0
- package/out/schemaRuntimeNone.d.ts +1 -0
- package/out/schemaRuntimeNone.js +1774 -0
- package/out/schemaRuntimeNoneLean.d.ts +1 -0
- package/out/schemaRuntimeNoneLean.js +1767 -0
- package/out/scope.d.ts +3 -4
- package/out/shared/issueFeatures.d.ts +38 -0
- package/out/shared/issueFeatures.js +40 -0
- package/out/standalone/artifact.d.ts +6 -6
- package/out/standalone/baking.d.ts +2 -2
- package/out/standalone/combinators.d.ts +1 -1
- package/out/standalone/compilers.d.ts +1 -1
- package/out/standalone/fallbacks.d.ts +1 -1
- package/out/standalone/keys.d.ts +1 -1
- package/out/standalone/sites.d.ts +3 -3
- package/out/standalone/units.d.ts +1 -1
- package/out/standalone/values.d.ts +1 -1
- package/out/type.d.ts +6 -6
- package/out/util.d.ts +1 -0
- package/out/util.js +137 -0
- package/out/variants/array.d.ts +6 -6
- package/out/variants/base.d.ts +61 -10
- package/out/variants/date.d.ts +5 -5
- package/out/variants/instantiate.d.ts +1 -1
- package/out/variants/number.d.ts +6 -6
- package/out/variants/object.d.ts +25 -12
- package/out/variants/string.d.ts +8 -8
- package/package.json +183 -8
- package/skills/arktype/SKILL.md +93 -93
- package/skills/arktype/references/migrating.md +144 -158
- package/skills/arktype/references/precompile.md +7 -7
- package/out/chunk-4CUA5XIP.js +0 -7
- package/out/chunk-GPNK5353.js +0 -2529
- package/out/chunk-MLKGABMK.js +0 -9
- package/out/chunk-PRTMQCHS.js +0 -3
- package/out/chunk-R5G57XC7.js +0 -7
- package/out/chunk-SWOOCAHN.js +0 -4
- package/out/chunk-TAHPKDNM.js +0 -3
- package/out/chunk-UETB6YE2.js +0 -14
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
import type { anyOrNever, array, JsonPrimitive, noSuggest, NonNegativeIntegerLiteral } from "../_internal/util/index.js";
|
|
2
|
+
export type inferJsonSchema<schema, root = schema> = {
|
|
3
|
+
root: root;
|
|
4
|
+
schema: schema;
|
|
5
|
+
} extends {
|
|
6
|
+
root: schema;
|
|
7
|
+
schema: root;
|
|
8
|
+
} ? schema extends unknown ? inferSchema<schema, schema, never> : never : inferSchema<schema, root, never>;
|
|
9
|
+
/** a schema whose keywords are all optional may be `{}`, so infers `unknown` */
|
|
10
|
+
export type inferWidened<schema> = [
|
|
11
|
+
schema
|
|
12
|
+
] extends [anyOrNever] ? schema : unknown;
|
|
13
|
+
export type inferSchema<schema, root, seen> = {} extends schema ? inferWidened<schema> : schema extends StringSchema ? schema["type"] extends "string" ? string : unknown : schema extends NumberSchema ? number : schema extends BooleanSchema ? boolean : schema extends NullSchema ? null : schema extends ObjectSchema ? inferObject<schema, root> : schema extends ArraySchema ? inferArray<schema, root, seen> : schema extends RefOnlySchema ? inferRef<schema["$ref"], root, seen> : schema extends boolean ? schema extends true ? unknown : never : (schema extends RefNode ? draftsOf<root> : "2020-12") extends "2020-12" ? inferNode<schema, schema extends IdentifiedSchema ? rootOf<schema, root> : root, keyof schema & PartKeyword, seen> : inferLegacyRef<schema, root, seen>;
|
|
14
|
+
/** before 2019-09, a `$ref`'s siblings are ignored; a widened `$schema` may
|
|
15
|
+
* also be 2020-12, where a standalone `nullable` widens the target with `null` */
|
|
16
|
+
export type inferLegacyRef<schema, root, seen> = applyLegacyNullable<schema, root, inferRef<schema["$ref" & keyof schema], resourceOf<schema, root>, seen>>;
|
|
17
|
+
export type applyLegacyNullable<schema, root, t> = "2020-12" extends draftsOf<root> ? schema extends NonNullableNode | DefinedTypedSchema ? t : applyNullable<schema, t> : t;
|
|
18
|
+
/** excludes `nullable`, including from an index signature that may hold it,
|
|
19
|
+
* which an optional `nullable?: never` wouldn't */
|
|
20
|
+
export interface NonNullableNode {
|
|
21
|
+
[k: `nullable${string}`]: never;
|
|
22
|
+
}
|
|
23
|
+
/** keywords whose absence allows a `type` to be inferred directly */
|
|
24
|
+
export interface Unmodified extends NonNullableNode {
|
|
25
|
+
$ref?: never;
|
|
26
|
+
$id?: Anchor;
|
|
27
|
+
id?: Anchor;
|
|
28
|
+
const?: never;
|
|
29
|
+
enum?: never;
|
|
30
|
+
allOf?: never;
|
|
31
|
+
anyOf?: never;
|
|
32
|
+
oneOf?: never;
|
|
33
|
+
not?: never;
|
|
34
|
+
if?: never;
|
|
35
|
+
}
|
|
36
|
+
/** an `$id` that doesn't start a new resource */
|
|
37
|
+
export type Anchor = `#${string}`;
|
|
38
|
+
export type IdentifiedSchema = {
|
|
39
|
+
$id: unknown;
|
|
40
|
+
} | {
|
|
41
|
+
id: string;
|
|
42
|
+
};
|
|
43
|
+
/** the resource local refs resolve against. Before 2019-09, a `$ref`'s
|
|
44
|
+
* siblings (including its identifier) are ignored */
|
|
45
|
+
export type rootOf<schema extends IdentifierKeywords, root> = schema extends RefNode ? draftsOf<root> extends "2020-12" ? resourceRootOf<schema, root> : draftsOf<root> extends SiblinglessRefDraft ? root : sameRoot<resourceRootOf<schema, root>, root> : resourceRootOf<schema, root>;
|
|
46
|
+
export interface RefNode {
|
|
47
|
+
$ref: unknown;
|
|
48
|
+
}
|
|
49
|
+
export interface IdentifierKeywords {
|
|
50
|
+
$id?: unknown;
|
|
51
|
+
id?: unknown;
|
|
52
|
+
}
|
|
53
|
+
/** draft-04 (and earlier) identifies a resource with `id`, later drafts with
|
|
54
|
+
* `$id`, so where either may apply, a node identified by only one of them
|
|
55
|
+
* has refs that can't be resolved */
|
|
56
|
+
export type resourceRootOf<schema extends IdentifierKeywords, root> = draftsOf<root> extends "2020-12" ? identifiedRootOf<"$id", schema, root> : draftsOf<root> extends "draft-04" ? identifiedRootOf<"id", schema, root> : "draft-04" extends draftsOf<root> ? sameRoot<identifiedRootOf<"id", schema, root>, identifiedRootOf<"$id", schema, root>> : identifiedRootOf<"$id", schema, root>;
|
|
57
|
+
/** a node without the identifier indexes it as `unknown` like a widened one */
|
|
58
|
+
export type identifiedRootOf<key extends keyof IdentifierKeywords, schema extends IdentifierKeywords, root> = string extends schema[key] ? [
|
|
59
|
+
schema,
|
|
60
|
+
root
|
|
61
|
+
] extends [root, schema] ? root : schema extends {
|
|
62
|
+
[_ in key]: unknown;
|
|
63
|
+
} ? UnresolvedRef : root : schema[key] extends Anchor ? root : resourceIn<schema, root>;
|
|
64
|
+
/** an embedded resource is read in its document's dialect unless it declares
|
|
65
|
+
* its own */
|
|
66
|
+
export type resourceIn<schema, root> = "$schema" extends keyof schema ? schema : draftsOf<root> extends "2020-12" ? schema : schema & {
|
|
67
|
+
$schema: root["$schema" & keyof root];
|
|
68
|
+
};
|
|
69
|
+
/** a root that differs between the dialects a document may be read as can't
|
|
70
|
+
* be resolved */
|
|
71
|
+
export type sameRoot<resource, root> = [
|
|
72
|
+
resource,
|
|
73
|
+
root
|
|
74
|
+
] extends [root, resource] ? root : UnresolvedRef;
|
|
75
|
+
/** the dialects a document may be read as: without `$schema`, it is read as
|
|
76
|
+
* 2020-12 (as is any other known `$schema`), and a widened one may be any */
|
|
77
|
+
export type draftsOf<root> = "$schema" extends keyof root ? draftOf<root["$schema" & keyof root]> : "2020-12";
|
|
78
|
+
export type draftOf<uri> = uri extends Draft04Uri ? "draft-04" : uri extends LegacyDraftUri ? "draft-07" : uri extends string ? {} extends Record<uri, 0> ? Draft : "2020-12" : unknown extends uri ? Draft : "2020-12";
|
|
79
|
+
/** dialects that differ in how they identify resources and read `$ref` */
|
|
80
|
+
export type Draft = "draft-04" | "draft-07" | "2020-12";
|
|
81
|
+
export type SiblinglessRefDraft = "draft-04" | "draft-07";
|
|
82
|
+
export type Draft04Uri = `${string}json-schema.org/draft-0${0 | 1 | 2 | 3 | 4 | 5}/${string}`;
|
|
83
|
+
/** draft-06 and draft-07, which ignore a `$ref`'s siblings like draft-04 */
|
|
84
|
+
export type LegacyDraftUri = `${string}json-schema.org/draft-0${string}`;
|
|
85
|
+
export interface StringSchema extends Unmodified {
|
|
86
|
+
type: "string";
|
|
87
|
+
}
|
|
88
|
+
export interface NumberSchema extends Unmodified {
|
|
89
|
+
type: "number" | "integer";
|
|
90
|
+
}
|
|
91
|
+
export interface BooleanSchema extends Unmodified {
|
|
92
|
+
type: "boolean";
|
|
93
|
+
}
|
|
94
|
+
export interface NullSchema extends Unmodified {
|
|
95
|
+
type: "null";
|
|
96
|
+
}
|
|
97
|
+
export interface ObjectSchema extends Unmodified {
|
|
98
|
+
type: "object";
|
|
99
|
+
}
|
|
100
|
+
export interface ArraySchema extends Unmodified {
|
|
101
|
+
type: "array";
|
|
102
|
+
}
|
|
103
|
+
/** a `$ref` without siblings that would need to be intersected with its target */
|
|
104
|
+
export interface RefOnlySchema extends NonNullableNode {
|
|
105
|
+
$ref: unknown;
|
|
106
|
+
$id?: Anchor;
|
|
107
|
+
type?: never;
|
|
108
|
+
properties?: never;
|
|
109
|
+
required?: never;
|
|
110
|
+
additionalProperties?: never;
|
|
111
|
+
patternProperties?: never;
|
|
112
|
+
items?: never;
|
|
113
|
+
prefixItems?: never;
|
|
114
|
+
const?: never;
|
|
115
|
+
enum?: never;
|
|
116
|
+
allOf?: never;
|
|
117
|
+
anyOf?: never;
|
|
118
|
+
oneOf?: never;
|
|
119
|
+
then?: never;
|
|
120
|
+
not?: never;
|
|
121
|
+
}
|
|
122
|
+
export type inferNode<schema, root, keys extends keyof schema & PartKeyword, seen> = schema extends UnmodifiedNode ? inferParts<schema, root, keys, seen> : applyModifiers<schema, inferParts<schema, root, keys, seen>>;
|
|
123
|
+
/** a single kind or a common pair is intersected directly, which is cheaper
|
|
124
|
+
* than collecting every part for intersectParts */
|
|
125
|
+
export type inferParts<schema, root, keys extends keyof schema & PartKeyword, seen> = PartKinds[keys] extends "base" ? inferBase<schema, root, seen> : PartKinds[keys] extends "$ref" ? inferRef<schema[keys], root, seen> : PartKinds[keys] extends "anyOf" ? inferBranches<schema[keys], root, seen> : PartKinds[keys] extends "oneOf" ? inferBranches<schema[keys], root, seen> : PartKinds[keys] extends "const" ? inferConst<schema[keys]> : PartKinds[keys] extends "enum" ? inferEnum<schema[keys]> : PartKinds[keys] extends "allOf" ? intersectBranches<schema[keys], root, seen> : PartKinds[keys] extends "base" | "enum" ? filterBase<schema, root, seen, inferEnum<schema["enum" & keyof schema]>> : PartKinds[keys] extends "base" | "const" ? filterBase<schema, root, seen, inferConst<schema["const" & keyof schema]>> : PartKinds[keys] extends "base" | "oneOf" ? intersect<inferBase<schema, root, seen>, inferBranches<schema["oneOf" & keyof schema], root, seen>> : PartKinds[keys] extends "base" | "anyOf" ? intersect<inferBase<schema, root, seen>, inferBranches<schema["anyOf" & keyof schema], root, seen>> : PartKinds[keys] extends "base" | "allOf" ? intersect<inferBase<schema, root, seen>, intersectBranches<schema["allOf" & keyof schema], root, seen>> : PartKinds[keys] extends "then" ? inferConditional<schema, root, seen> : PartKinds[keys] extends "base" | "then" ? intersect<inferBase<schema, root, seen>, inferConditional<schema, root, seen>> : keepsValues<schema> extends true ? intersect<"const" extends keyof schema ? inferConst<schema["const" & keyof schema]> : unknown, "enum" extends keyof schema ? inferEnum<schema["enum" & keyof schema]> : unknown> : intersectParts<{
|
|
126
|
+
[k in keys as PartKinds[k]]: inferPart<k, schema, root, seen>;
|
|
127
|
+
}>;
|
|
128
|
+
/** whether `const` or `enum` values are kept unfiltered, since they're
|
|
129
|
+
* structured and filtering them against parts that may recurse back to the
|
|
130
|
+
* node can't be deferred */
|
|
131
|
+
export type keepsValues<schema> = [
|
|
132
|
+
schema
|
|
133
|
+
] extends [PrimitiveValuesSchema] ? false : containsRef<schema>;
|
|
134
|
+
/** a schema whose `const` and `enum` values are intersected without
|
|
135
|
+
* reading the other parts */
|
|
136
|
+
export interface PrimitiveValuesSchema {
|
|
137
|
+
const?: JsonPrimitive;
|
|
138
|
+
enum?: readonly JsonPrimitive[];
|
|
139
|
+
[k: string]: unknown;
|
|
140
|
+
}
|
|
141
|
+
/** whether a ref in `nodes` or their inferred subschemas could recurse */
|
|
142
|
+
export type containsRef<nodes> = [
|
|
143
|
+
nodes
|
|
144
|
+
] extends [never] ? false : true extends (nodes extends {
|
|
145
|
+
$ref: unknown;
|
|
146
|
+
} ? true : false) ? true : containsRef<subschemasOf<nodes>>;
|
|
147
|
+
export type subschemasOf<node> = node extends SubschemaNode ? (node[SubschemaKeyword & keyof node] extends infer subschema ? subschema extends array ? subschema[number] : subschema : never) | (node[SubschemaRecordKeyword & keyof node] extends infer record ? record[keyof record] : never) : never;
|
|
148
|
+
export type intersectParts<parts> = parts extends ({
|
|
149
|
+
base?: infer base;
|
|
150
|
+
$ref?: infer ref;
|
|
151
|
+
const?: infer constValue;
|
|
152
|
+
enum?: infer enumValue;
|
|
153
|
+
allOf?: infer allOf;
|
|
154
|
+
anyOf?: infer anyOf;
|
|
155
|
+
oneOf?: infer oneOf;
|
|
156
|
+
then?: infer conditional;
|
|
157
|
+
}) ? [
|
|
158
|
+
parts[keyof parts]
|
|
159
|
+
] extends [JsonPrimitive] ? unknown extends parts[keyof parts] ? filterValues<intersect<intersect<base, ref>, intersect<intersect<allOf, anyOf>, intersect<oneOf, conditional>>>, intersect<constValue, enumValue>> : base & ref & constValue & enumValue & allOf & anyOf & oneOf & conditional : filterValues<intersect<intersect<base, ref>, intersect<intersect<allOf, anyOf>, intersect<oneOf, conditional>>>, intersect<constValue, enumValue>> : never;
|
|
160
|
+
export type inferPart<k extends keyof schema, schema, root, seen> = k extends BaseKeyword ? inferBase<schema, root, seen> : k extends "$ref" ? inferRef<schema[k], root, seen> : k extends "anyOf" | "oneOf" ? inferBranches<schema[k], root, seen> : k extends "const" ? inferConst<schema[k]> : k extends "enum" ? inferEnum<schema[k]> : k extends "allOf" ? intersectBranches<schema[k], root, seen> : inferConditional<schema, root, seen>;
|
|
161
|
+
/** object and array keywords only constrain their own domain, so without a
|
|
162
|
+
* `type` every other JSON value is valid */
|
|
163
|
+
export type inferBase<schema, root, seen> = schema extends TypeListSchema ? applyNullable<schema, inferTypeName<schema["type"][number], schema, root, seen>> : schema extends TypedSchema ? applyNullable<schema, inferTypeName<schema["type"], schema, root, seen>> : schema extends ImpliedObjectSchema ? schema extends ImpliedArraySchema ? inferArray<schema, root, seen, inferObject<schema, root> | string | number | boolean | null> : inferObject<schema, root> | string | number | boolean | null | unknown[] : schema extends ImpliedArraySchema ? inferArray<schema, root, seen, string | number | boolean | null | object> : unknown;
|
|
164
|
+
export interface TypedSchema {
|
|
165
|
+
type: unknown;
|
|
166
|
+
}
|
|
167
|
+
/** a schema whose `type` is definitely present, so `nullable` adds `null` to
|
|
168
|
+
* that `type` rather than unioning the whole schema with `null` */
|
|
169
|
+
export interface DefinedTypedSchema {
|
|
170
|
+
type: {};
|
|
171
|
+
}
|
|
172
|
+
export interface TypeListSchema {
|
|
173
|
+
type: array;
|
|
174
|
+
}
|
|
175
|
+
export type ImpliedObjectSchema = PropertiesSchema | {
|
|
176
|
+
required: unknown;
|
|
177
|
+
} | {
|
|
178
|
+
additionalProperties: unknown;
|
|
179
|
+
} | PatternPropertiesSchema;
|
|
180
|
+
export type ImpliedArraySchema = ItemsSchema | {
|
|
181
|
+
prefixItems: unknown;
|
|
182
|
+
};
|
|
183
|
+
/** JSON has no `undefined`, so a value including it is an optional keyword
|
|
184
|
+
* that may be absent. A widened `object` may also be an array, which an
|
|
185
|
+
* inferred `object` excludes */
|
|
186
|
+
export type inferConst<value> = value extends undefined ? unknown : object extends value ? string extends value ? value : value | unknown[] : value;
|
|
187
|
+
export type inferEnum<values> = values extends array ? object extends values[number] ? inferConst<values[number]> : values[number] : unknown;
|
|
188
|
+
export type PartKinds = {
|
|
189
|
+
type: "base";
|
|
190
|
+
properties: "base";
|
|
191
|
+
required: "base";
|
|
192
|
+
additionalProperties: "base";
|
|
193
|
+
patternProperties: "base";
|
|
194
|
+
items: "base";
|
|
195
|
+
prefixItems: "base";
|
|
196
|
+
$ref: "$ref";
|
|
197
|
+
const: "const";
|
|
198
|
+
enum: "enum";
|
|
199
|
+
allOf: "allOf";
|
|
200
|
+
anyOf: "anyOf";
|
|
201
|
+
oneOf: "oneOf";
|
|
202
|
+
then: "then";
|
|
203
|
+
};
|
|
204
|
+
export type PartKeyword = keyof PartKinds;
|
|
205
|
+
export type BaseKeyword = {
|
|
206
|
+
[k in PartKeyword]: PartKinds[k] extends "base" ? k : never;
|
|
207
|
+
}[PartKeyword];
|
|
208
|
+
export type inferConditional<schema, root, seen> = schema extends ConditionalSchema ? schema["if"] extends true ? inferSchema<schema["then"], root, seen> : schema["if"] extends false ? inferSchema<schema["else"], root, seen> : inferSchema<schema["then" | "else"], root, seen> : unknown;
|
|
209
|
+
/** an `if` that may be undefined may be absent, leaving `then` and `else`
|
|
210
|
+
* with no effect */
|
|
211
|
+
export interface ConditionalSchema {
|
|
212
|
+
if: {};
|
|
213
|
+
then: unknown;
|
|
214
|
+
else: unknown;
|
|
215
|
+
}
|
|
216
|
+
export type applyModifiers<schema, t> = schema extends NonNullableNode | DefinedTypedSchema ? applyNotModifier<schema, t> : applyNullable<schema, applyNotModifier<schema, t>>;
|
|
217
|
+
export type applyNotModifier<schema, t> = schema extends NegatedSchema ? skipsNot<schema["not"], schema> extends true ? t : applyNot<schema["not"], t> : t;
|
|
218
|
+
export interface UnmodifiedNode extends NonNullableNode {
|
|
219
|
+
not?: never;
|
|
220
|
+
[k: string]: unknown;
|
|
221
|
+
}
|
|
222
|
+
export interface NegatedSchema {
|
|
223
|
+
not: unknown;
|
|
224
|
+
}
|
|
225
|
+
/** whether a `not` that excludes structured values is skipped, since
|
|
226
|
+
* excluding them from parts that may recurse back to the node can't be
|
|
227
|
+
* deferred */
|
|
228
|
+
export type skipsNot<negated, schema> = negated extends object ? keyof negated extends NegatableKeyword ? [
|
|
229
|
+
rejectedValues<negated>
|
|
230
|
+
] extends [JsonPrimitive] ? false : containsRef<schema> : false : false;
|
|
231
|
+
/** an undefined `not` is absent, and one with other keywords excludes nothing */
|
|
232
|
+
export type applyNot<negated, t> = negated extends object ? keyof negated extends NegatableKeyword ? t extends object ? excludeRejected<t, rejectedValues<negated>> : t extends JsonPrimitive ? excludeRejected<t, rejectedValues<negated>> : jsonValuesWithout<rejectedValues<negated>> : t : negated extends true ? never : t;
|
|
233
|
+
/** an element that may be `any` may be any value, though TS relates `[any]`
|
|
234
|
+
* to `readonly [1]`, so only a rejected domain excludes it */
|
|
235
|
+
export type excludeRejected<t, rejected> = t extends rejected ? t extends array ? true extends mayBeAny<t[number]> ? array extends rejected ? never : t : never : never : t;
|
|
236
|
+
export type mayBeAny<t> = 0 extends 1 & t ? true : t extends array ? mayBeAny<t[number]> : false;
|
|
237
|
+
/** the JSON values a negation doesn't reject, where it rejects at least one
|
|
238
|
+
* whole domain. Non-array objects have no clean display, so keeping them
|
|
239
|
+
* without arrays keeps everything */
|
|
240
|
+
export type jsonValuesWithout<rejected> = (string extends rejected ? never : string) | (number extends rejected ? never : number) | (boolean extends rejected ? never : boolean) | (null extends rejected ? never : null) | (array extends rejected ? JsonObject extends rejected ? never : unknown : JsonObject extends rejected ? unknown[] : object | unknown[]) extends infer kept ? [
|
|
241
|
+
string | number | boolean | null | object
|
|
242
|
+
] extends [kept] ? unknown : kept : never;
|
|
243
|
+
export type NegatableKeyword = ExactKeyword | AnnotationKeyword;
|
|
244
|
+
export type ExactKeyword = "type" | "const" | "enum";
|
|
245
|
+
export type AnnotationKeyword = "title" | "description" | "default" | "examples" | "deprecated" | "readOnly" | "writeOnly" | "$comment";
|
|
246
|
+
/** values rejected by a negation whichever member of each keyword's union is
|
|
247
|
+
* present, where an undefined member is an absent keyword */
|
|
248
|
+
export type rejectedValues<negated> = {
|
|
249
|
+
[k in keyof negated]-?: k extends ExactKeyword ? rejectedBy<k, negated[k]> : never;
|
|
250
|
+
}[keyof negated] extends (_: infer values) => void ? values : never;
|
|
251
|
+
/** a widened value may be any value, so it provably rejects nothing */
|
|
252
|
+
export type rejectedBy<keyword, value> = unknown extends value ? (_: never) => void : value extends undefined ? (_: unknown) => void : (_: rejects<keyword, value>) => void;
|
|
253
|
+
export type rejects<keyword, value> = keyword extends "const" ? jsonUnitOf<value> : value extends array ? number extends value["length"] ? never : {
|
|
254
|
+
[i in keyof value]: rejectedByEach<keyword extends "type" ? "type" : "const", value[i]>;
|
|
255
|
+
}[number] : keyword extends "type" ? domainOf<value> : never;
|
|
256
|
+
/** values rejected whichever member of a union the value is */
|
|
257
|
+
export type rejectedByEach<keyword, value> = unknown extends value ? never : (value extends unknown ? (_: rejects<keyword, value>) => void : never) extends (_: infer values) => void ? values : never;
|
|
258
|
+
/** a literal that is the only JSON value of its type (TS object types are
|
|
259
|
+
* open, so an object is never exact) */
|
|
260
|
+
export type jsonUnitOf<value> = value extends string | number ? {} extends {
|
|
261
|
+
[_ in value]: 0;
|
|
262
|
+
} ? never : value : value extends boolean | null ? value : value extends array ? number extends value["length"] ? never : [value] extends ([
|
|
263
|
+
{
|
|
264
|
+
[i in keyof value]-?: rejectedByEach<"const", value[i]>;
|
|
265
|
+
}
|
|
266
|
+
]) ? value : never : never;
|
|
267
|
+
export type domainOf<name> = name extends keyof TypeDomains ? TypeDomains[name] : never;
|
|
268
|
+
/** JSON values of each type name, except `integer`, which TS can't express */
|
|
269
|
+
export type TypeDomains = {
|
|
270
|
+
string: string;
|
|
271
|
+
number: number;
|
|
272
|
+
boolean: boolean;
|
|
273
|
+
null: null;
|
|
274
|
+
object: JsonObject;
|
|
275
|
+
array: array;
|
|
276
|
+
};
|
|
277
|
+
/** JSON's non-array objects (TS `object` includes arrays) */
|
|
278
|
+
export type JsonObject = object & {
|
|
279
|
+
length?: never;
|
|
280
|
+
};
|
|
281
|
+
/** OpenAPI 3.0's `nullable` adds `null` to the `type` beside it (so other
|
|
282
|
+
* keywords still filter it, like ajv), or unions the whole schema with `null`
|
|
283
|
+
* when no `type` is present. `nullable` might be `true` (a widened `boolean`
|
|
284
|
+
* or an optional keyword) */
|
|
285
|
+
export type applyNullable<schema, t> = true extends schema["nullable" & keyof schema] ? t | null : t;
|
|
286
|
+
/** a widened name (including `any`) may be any type */
|
|
287
|
+
export type inferTypeName<name, schema, root, seen> = unknown extends name ? unknown : name extends keyof PrimitiveTypes ? PrimitiveTypes[name] : name extends "object" ? inferObject<schema, root> : name extends "array" ? inferArray<schema, root, seen> : unknown;
|
|
288
|
+
export type PrimitiveTypes = {
|
|
289
|
+
string: string;
|
|
290
|
+
number: number;
|
|
291
|
+
integer: number;
|
|
292
|
+
boolean: boolean;
|
|
293
|
+
null: null;
|
|
294
|
+
};
|
|
295
|
+
export type intersectBranches<branches, root, seen> = intersectLimited<limitUnions<branches, root>, root, seen>;
|
|
296
|
+
export type intersectLimited<branches, root, seen> = branches extends readonly [infer head, ...infer tail] ? tail extends readonly [] ? inferSchema<head, root, seen> : "19" extends keyof tail ? intersectAll<intersectBranchPairs<branches, root, seen, []>> : intersectRest<tail, root, seen, inferSchema<head, root, seen>> : unknown;
|
|
297
|
+
export type intersectBranchPairs<branches, root, seen, paired extends array> = branches extends readonly [infer l, infer r, ...infer rest] ? intersectBranchPairs<rest, root, seen, [
|
|
298
|
+
...paired,
|
|
299
|
+
intersect<inferSchema<l, root, seen>, inferSchema<r, root, seen>>
|
|
300
|
+
]> : branches extends readonly [infer last, ...unknown[]] ? [
|
|
301
|
+
...paired,
|
|
302
|
+
inferSchema<last, root, seen>
|
|
303
|
+
] : paired;
|
|
304
|
+
/** intersect neighboring pairs until one result remains, so the results of
|
|
305
|
+
* a wide allOf (more than 20 branches) aren't each nested in the one before */
|
|
306
|
+
export type intersectAll<results extends array> = results extends [infer result] ? result : intersectAll<intersectPairs<results, []>>;
|
|
307
|
+
export type intersectPairs<results extends array, paired extends array> = results extends [infer l, infer r, ...infer rest] ? intersectPairs<rest, [...paired, intersect<l, r>]> : [...paired, ...results];
|
|
308
|
+
export type intersectRest<branches, root, seen, result> = branches extends readonly [infer head, ...infer tail] ? tail extends readonly [] ? intersect<result, inferSchema<head, root, seen>> : intersectRest<tail, root, seen, intersect<result, inferSchema<head, root, seen>>> : result;
|
|
309
|
+
/** each branch that infers a union multiplies the members of the result, so
|
|
310
|
+
* past a few of them, a branch's union keywords are ignored. A nested allOf
|
|
311
|
+
* shares the count, and past it a `$ref` that may lead to more of them is
|
|
312
|
+
* inferred without union keywords at every depth */
|
|
313
|
+
export type limitUnions<branches, root> = [
|
|
314
|
+
branches[number & keyof branches]
|
|
315
|
+
] extends [UnionFreeBranch] ? branches : "3" extends keyof branches ? limitedUnions<branches, root> : [branches[number & keyof branches]] extends [FlatBranch] ? branches : limitedUnions<branches, root>;
|
|
316
|
+
export type limitedUnions<branches, root> = limitUnionsIn<branches, root, [], [], false> extends ([
|
|
317
|
+
infer limited,
|
|
318
|
+
unknown,
|
|
319
|
+
true
|
|
320
|
+
]) ? limited : branches;
|
|
321
|
+
/** the limited branches, the union branches counted so far and whether any
|
|
322
|
+
* branch was changed (an unchanged list is kept, so it's inferred once) */
|
|
323
|
+
export type limitUnionsIn<branches, root, limited extends array, unions extends 1[], changed extends boolean> = branches extends readonly [infer head, ...infer tail] ? head extends NestedBranch ? limitUnionsIn<head["allOf"], root, [], unions, false> extends ([
|
|
324
|
+
infer nested,
|
|
325
|
+
infer nestedUnions extends 1[],
|
|
326
|
+
infer nestedChanged
|
|
327
|
+
]) ? nestedChanged extends true ? limitUnionsIn<tail, root, [
|
|
328
|
+
...limited,
|
|
329
|
+
{
|
|
330
|
+
[k in keyof head]: k extends "allOf" ? nested : head[k];
|
|
331
|
+
}
|
|
332
|
+
], nestedUnions, true> : limitUnionsIn<tail, root, [...limited, head], nestedUnions, changed> : never : mayInferUnion<head, root> extends true ? unions["length"] extends MaxUnionBranches ? limitUnionsIn<tail, root, [
|
|
333
|
+
...limited,
|
|
334
|
+
withoutUnions<head>
|
|
335
|
+
], unions, true> : limitUnionsIn<tail, root, [...limited, head], [...unions, 1], changed> : limitUnionsIn<tail, root, [...limited, head], unions, changed> : [limited, unions, changed];
|
|
336
|
+
/** an allOf branch whose own allOf branches are combined with the others */
|
|
337
|
+
export interface NestedBranch {
|
|
338
|
+
allOf: array;
|
|
339
|
+
anyOf?: never;
|
|
340
|
+
oneOf?: never;
|
|
341
|
+
then?: never;
|
|
342
|
+
$ref?: never;
|
|
343
|
+
$id?: Anchor;
|
|
344
|
+
id?: Anchor;
|
|
345
|
+
}
|
|
346
|
+
export type MaxUnionBranches = 3;
|
|
347
|
+
/** a ref's target or a nested allOf may lead to a union keyword */
|
|
348
|
+
export type mayInferUnion<branch, root> = branch extends UnionBranch | AllOfSchema ? true : branch extends {
|
|
349
|
+
$ref: infer ref extends string;
|
|
350
|
+
} ? resolveRef<ref, root> extends UnionBranch | AllOfSchema | RefSchema ? true : false : false;
|
|
351
|
+
export type UnionBranch = {
|
|
352
|
+
anyOf: unknown;
|
|
353
|
+
} | {
|
|
354
|
+
oneOf: unknown;
|
|
355
|
+
} | ConditionalSchema;
|
|
356
|
+
export interface AllOfSchema {
|
|
357
|
+
allOf: unknown;
|
|
358
|
+
}
|
|
359
|
+
export interface RefSchema {
|
|
360
|
+
$ref: string;
|
|
361
|
+
}
|
|
362
|
+
export type UnionKeyword = "anyOf" | "oneOf" | "then" | "else";
|
|
363
|
+
/** a branch whose refs and nested allOf branches are also inferred without
|
|
364
|
+
* union keywords */
|
|
365
|
+
export type withoutUnions<branch> = branch extends array ? branch : branch extends object ? {
|
|
366
|
+
[k in keyof branch as k extends UnionKeyword ? never : k]: k extends ("$ref") ? UnionlessRef<branch[k]> : k extends "allOf" ? withoutUnionsIn<branch[k]> : branch[k];
|
|
367
|
+
} : branch;
|
|
368
|
+
export type withoutUnionsIn<branches> = {
|
|
369
|
+
[i in keyof branches]: withoutUnions<branches[i]>;
|
|
370
|
+
};
|
|
371
|
+
/** a `$ref` whose target is inferred without union keywords */
|
|
372
|
+
export interface UnionlessRef<ref> {
|
|
373
|
+
unionless: ref;
|
|
374
|
+
}
|
|
375
|
+
/** a branch without a nested allOf, which can't add to the union-producing
|
|
376
|
+
* branches of a list */
|
|
377
|
+
export interface FlatBranch {
|
|
378
|
+
allOf?: never;
|
|
379
|
+
[k: string]: unknown;
|
|
380
|
+
}
|
|
381
|
+
/** a branch that can't infer a union (UnionBranch checks `then` and `else`
|
|
382
|
+
* exactly) */
|
|
383
|
+
export interface UnionFreeBranch {
|
|
384
|
+
anyOf?: never;
|
|
385
|
+
oneOf?: never;
|
|
386
|
+
then?: never;
|
|
387
|
+
allOf?: never;
|
|
388
|
+
$ref?: never;
|
|
389
|
+
[k: string]: unknown;
|
|
390
|
+
}
|
|
391
|
+
/** an optional keyword may be absent, so its value includes `undefined` and contributes nothing */
|
|
392
|
+
export type inferBranches<branches, root, seen> = undefined extends branches ? unknown : inferSchema<branches[number & keyof branches], root, seen>;
|
|
393
|
+
/** JSON domains are disjoint, but TS doesn't reduce `string & { a: 1 }`,
|
|
394
|
+
* and its `object` includes arrays */
|
|
395
|
+
export type intersect<l, r> = unknown extends l ? unknown extends r ? l & r : r : unknown extends r ? l : l | r extends JsonPrimitive ? l & r : intersectDomains<l, r> | never;
|
|
396
|
+
export type intersectDomains<l, r> = l extends unknown ? r extends unknown ? l extends object ? r extends object ? intersectObjects<l, r> : never : r extends object ? never : l & r : never : never;
|
|
397
|
+
/** an object without numeric keys can't be an array, so it isn't related to
|
|
398
|
+
* one, which would resolve the members of a recursive object while its index
|
|
399
|
+
* signature is still being inferred (TS would then see it without them) */
|
|
400
|
+
export type intersectObjects<l, r> = [
|
|
401
|
+
keyof l
|
|
402
|
+
] extends [never] ? number extends keyof r ? r extends array ? never : r : r : [keyof r] extends [never] ? number extends keyof l ? l extends array ? never : l : l : (keyof l | keyof r) & number extends never ? [
|
|
403
|
+
keyof l & keyof r
|
|
404
|
+
] extends [never] ? {
|
|
405
|
+
[k in keyof (l & r)]: (l & r)[k];
|
|
406
|
+
} & unknown : mergeObjects<l, r, keyof l & keyof r> : l extends array ? r extends array ? intersectArrays<l, r> : never : r extends array ? never : mergeObjects<stringKeyed<l>, stringKeyed<r>, keyof stringKeyed<l> & keyof stringKeyed<r>>;
|
|
407
|
+
/** a numeric key is the same property as its string spelling */
|
|
408
|
+
export type stringKeyed<o> = {
|
|
409
|
+
[k in keyof o as k extends number ? `${k}` : k]: o[k];
|
|
410
|
+
};
|
|
411
|
+
/** a key of both objects (including one matched by an index signature) has
|
|
412
|
+
* the intersection of both values. Keys and modifiers come from the objects'
|
|
413
|
+
* shapes, since TS would reduce `l & r` itself by resolving every property
|
|
414
|
+
* type, which a recursive property can't allow */
|
|
415
|
+
export type mergeObjects<l, r, sharedKey extends keyof l & keyof r> = {
|
|
416
|
+
[k in keyof (shapeOf<l> & shapeOf<r>)]: k extends sharedKey ? intersectValues<l[k], r[k]> : k extends keyof l ? l[k] : r[k & keyof r];
|
|
417
|
+
} & unknown;
|
|
418
|
+
/** equivalent values (like the same recursive `$ref`) aren't intersected,
|
|
419
|
+
* which would build a new type at every level of the recursion */
|
|
420
|
+
export type intersectValues<l, r> = [
|
|
421
|
+
l,
|
|
422
|
+
r
|
|
423
|
+
] extends [r, l] ? Exclude<l, undefined> : Exclude<intersect<l, r>, undefined>;
|
|
424
|
+
export type shapeOf<o> = {
|
|
425
|
+
[k in keyof o]: 0;
|
|
426
|
+
};
|
|
427
|
+
export type filterBase<schema, root, seen, values> = unknown extends values ? inferBase<schema, root, seen> : [values] extends [JsonPrimitive] ? intersect<inferBase<schema, root, seen>, values> : containsRef<schema> extends true ? values : filterValues<inferBase<schema, root, seen>, values>;
|
|
428
|
+
/** a `const` or `enum` value is a literal, which the other parts filter */
|
|
429
|
+
export type filterValues<base, values> = unknown extends values ? base : [values] extends [JsonPrimitive] ? intersect<base, values> : filterValue<base, values> | never;
|
|
430
|
+
/** an array or object literal is kept if the base admits it, and otherwise
|
|
431
|
+
* intersected with it */
|
|
432
|
+
export type filterValue<base, value> = value extends JsonPrimitive ? intersect<base, value> : unknown extends base ? value : [value] extends [base] ? value : intersect<base, value>;
|
|
433
|
+
/** a tuple's fixed elements are keys like `"0"`, and `[]` has a literal length */
|
|
434
|
+
export type intersectArrays<l extends array, r extends array> = number extends r["length"] ? "0" extends keyof r ? intersectTuples<l, r> : [l] extends [readonly subtypeElementOf<r>[]] ? l : number extends l["length"] ? "0" extends keyof l ? intersectTuples<l, r> : intersect<l[number], r[number]>[] : intersectTuples<l, r> : number extends l["length"] ? "0" extends keyof l ? intersectTuples<l, r> : [r] extends [readonly subtypeElementOf<l>[]] ? r : intersectTuples<l, r> : intersectTuples<l, r>;
|
|
435
|
+
/** an element type that admits a TS array may still exclude JSON arrays (e.g.
|
|
436
|
+
* `object`), so it only shows a subtype if it is `unknown` */
|
|
437
|
+
export type subtypeElementOf<t extends array> = readonly never[] extends t[number] ? unknown extends t[number] ? unknown : never : t[number];
|
|
438
|
+
/** a readonly tuple is the wider type, so the result is readonly if either side is */
|
|
439
|
+
export type intersectTuples<l extends array, r extends array> = intersectElements<l, r, [l | r] extends [unknown[]] ? false : true, [], []>;
|
|
440
|
+
/** intersect element by element with the stricter optionality and
|
|
441
|
+
* closedness, where a missing required element makes the tuple `never` */
|
|
442
|
+
export type intersectElements<l extends array, r extends array, isReadonly extends boolean, required extends array, optional extends array> = [
|
|
443
|
+
elementOf<l>,
|
|
444
|
+
elementOf<r>
|
|
445
|
+
] extends ([
|
|
446
|
+
[
|
|
447
|
+
infer lKind,
|
|
448
|
+
infer lElement,
|
|
449
|
+
infer lTail extends array
|
|
450
|
+
],
|
|
451
|
+
[
|
|
452
|
+
infer rKind,
|
|
453
|
+
infer rElement,
|
|
454
|
+
infer rTail extends array
|
|
455
|
+
]
|
|
456
|
+
]) ? lKind | rKind extends "rest" ? finishTuple<isReadonly, required, optional, intersect<lElement, rElement>[]> : "closed" extends lKind | rKind ? "required" extends lKind | rKind ? never : finishTuple<isReadonly, required, optional, []> : [intersect<lElement, rElement>] extends [infer element] ? [
|
|
457
|
+
element
|
|
458
|
+
] extends [never] ? "required" extends lKind | rKind ? never : finishTuple<isReadonly, required, optional, []> : "required" extends lKind | rKind ? intersectElements<lTail, rTail, isReadonly, [
|
|
459
|
+
...required,
|
|
460
|
+
element
|
|
461
|
+
], optional> : intersectElements<lTail, rTail, isReadonly, required, [
|
|
462
|
+
...optional,
|
|
463
|
+
element
|
|
464
|
+
]> : never : never;
|
|
465
|
+
/** the kind, type and remaining elements of a tuple's first element */
|
|
466
|
+
export type elementOf<t extends array> = t extends readonly [infer head, ...infer tail] ? ["required", head, tail] : "0" extends keyof t ? t extends readonly [(infer head)?, ...infer tail] ? [
|
|
467
|
+
"optional",
|
|
468
|
+
head,
|
|
469
|
+
tail
|
|
470
|
+
] : never : t extends readonly [] ? ["closed", never, t] : ["rest", t[number], t];
|
|
471
|
+
export type finishTuple<isReadonly extends boolean, required extends array, optional extends array, rest extends array> = isReadonly extends true ? readonly [...required, ...{
|
|
472
|
+
[i in keyof optional]?: optional[i];
|
|
473
|
+
}, ...rest] : [...required, ...{
|
|
474
|
+
[i in keyof optional]?: optional[i];
|
|
475
|
+
}, ...rest];
|
|
476
|
+
export type resourceOf<schema, root> = schema extends IdentifiedSchema ? rootOf<schema, root> : root;
|
|
477
|
+
export type SubschemaKeyword = "items" | "prefixItems" | "additionalItems" | "additionalProperties" | "allOf" | "anyOf" | "oneOf" | "then" | "else";
|
|
478
|
+
export type SubschemaRecordKeyword = "properties" | "patternProperties";
|
|
479
|
+
/** a node with a required subschema keyword (optional ones, as on a
|
|
480
|
+
* hand-written schema type, may be absent, so they contribute nothing) */
|
|
481
|
+
export type SubschemaNode = {
|
|
482
|
+
[k in SubschemaKeyword | SubschemaRecordKeyword]: {
|
|
483
|
+
[_ in k]: unknown;
|
|
484
|
+
};
|
|
485
|
+
}[SubschemaKeyword | SubschemaRecordKeyword];
|
|
486
|
+
export type inferRef<ref, root, seen> = ref extends seen ? unknown : ref extends string ? [
|
|
487
|
+
seen
|
|
488
|
+
] extends [never] ? inferSchema<resolveRef<ref, root>, root, ref> : inferSchema<resolveRef<ref, root>, root, trackedRefs<ref, root, seen>> : ref extends EmbeddedRef<infer target, infer resource> ? inferSchema<target, resource, seen> : ref extends UnionlessRef<infer unionless> ? inferUnionlessRef<unionless, root, seen> : unknown;
|
|
489
|
+
export type inferUnionlessRef<ref, root, seen> = ref extends string ? ref extends seen ? unknown : inferSchema<withoutUnions<resolveRef<ref, root>>, root, seen | ref> : ref extends EmbeddedRef<infer target, infer resource> ? inferSchema<withoutUnions<target>, resource, seen> : unknown;
|
|
490
|
+
/** a ref can only be reached again through a ref its target can reach, so
|
|
491
|
+
* a target that branches into several refs drops the others, letting each
|
|
492
|
+
* acyclic target be inferred once however many paths lead to it */
|
|
493
|
+
export type trackedRefs<ref extends string, root, seen> = isUnion<targetRefs<ref, root>> extends false ? seen | ref : Extract<seen | ref, reachableRefs<targetRefs<ref, root>, root, never>> extends infer tracked ? ref extends tracked ? cycleRefs<tracked, Extract<seen, number>> : tracked : never;
|
|
494
|
+
/** a cycle's refs are tracked by path, and a graph of cycles has
|
|
495
|
+
* exponentially many, so a path that branches a few times within cycles
|
|
496
|
+
* cuts every ref */
|
|
497
|
+
export type cycleRefs<tracked, depth> = [
|
|
498
|
+
depth
|
|
499
|
+
] extends [never] ? tracked | 1 : depth extends 4 ? string : tracked | NextCycleDepth[depth & keyof NextCycleDepth];
|
|
500
|
+
export type NextCycleDepth = [never, 2, 3, 4];
|
|
501
|
+
export type isUnion<t, members = t> = t extends unknown ? [
|
|
502
|
+
members
|
|
503
|
+
] extends [t] ? false : true : never;
|
|
504
|
+
/** refs reachable through `frontier`. Refs across a resource boundary may
|
|
505
|
+
* be spelled the same as these, so a boundary makes every ref reachable */
|
|
506
|
+
export type reachableRefs<frontier extends string, root, reached> = [
|
|
507
|
+
frontier
|
|
508
|
+
] extends [never] ? reached : string extends frontier ? string : reachableRefs<Exclude<frontier extends unknown ? targetRefs<frontier, root> : never, reached | frontier>, root, reached | frontier>;
|
|
509
|
+
/** the refs a target's own eagerly inferred subschemas contain */
|
|
510
|
+
export type targetRefs<ref extends string, root> = eagerRefsIn<resolveRef<ref, root>, never>;
|
|
511
|
+
/** an `any` node infers `any` without reading its refs */
|
|
512
|
+
export type eagerRefsIn<nodes, refs> = [
|
|
513
|
+
nodes
|
|
514
|
+
] extends [anyOrNever] ? refs : eagerRefsIn<eagerSubschemas<nodes>, refs | refsOf<nodes>>;
|
|
515
|
+
export type refsOf<node> = node extends ResourceNode ? string : node extends {
|
|
516
|
+
$ref: infer ref;
|
|
517
|
+
} ? ref extends string ? ref : string : never;
|
|
518
|
+
/** a widened subschema infers `unknown` without reading its refs */
|
|
519
|
+
export type eagerSubschemas<node> = node extends EagerNode ? node[EagerKeyword & keyof node] extends infer subschema ? subschema extends array ? number extends subschema["length"] ? {} extends subschema[number] ? never : subschema[number] : subschema[number] : {} extends subschema ? never : subschema : never : never;
|
|
520
|
+
/** a node with a required eager keyword (optional ones, as on a hand-written
|
|
521
|
+
* schema type, may be absent, so they contribute nothing) */
|
|
522
|
+
export type EagerNode = {
|
|
523
|
+
[k in EagerKeyword]: {
|
|
524
|
+
[_ in k]: unknown;
|
|
525
|
+
};
|
|
526
|
+
}[EagerKeyword];
|
|
527
|
+
export type EagerKeyword = "allOf" | "anyOf" | "oneOf" | "then" | "else" | "prefixItems" | "items" | "additionalItems";
|
|
528
|
+
export type resolveRef<ref extends string, root> = ref extends "#" ? root : ref extends (`#/${infer container extends DefinitionsKeyword}/${infer name}`) ? resolveDefinition<root, container, name> : ref extends `#/${infer path}` ? resolvePointer<root, path> : UnresolvedRef;
|
|
529
|
+
export type DefinitionsKeyword = "$defs" | "definitions";
|
|
530
|
+
export type resolveDefinition<root, container extends string, name extends string> = root extends {
|
|
531
|
+
[_ in container]?: infer definitions;
|
|
532
|
+
} ? name extends keyof definitions ? name extends EncodedName ? resolvePointer<root, `${container}/${name}`> : definitions[name] : resolvePointer<root, `${container}/${name}`> : UnresolvedRef;
|
|
533
|
+
/** a name that must be decoded before it can be compared to a key */
|
|
534
|
+
export type EncodedName = `${string}${"/" | "~" | "%"}${string}`;
|
|
535
|
+
/** a percent-encoded fragment isn't decoded, so its ref infers as `unknown` */
|
|
536
|
+
export type resolvePointer<root, path extends string> = path extends `${string}%${string}` ? UnresolvedRef : walkPointer<root, path>;
|
|
537
|
+
/** a pointer may pass through an embedded resource, which its target's refs
|
|
538
|
+
* resolve against */
|
|
539
|
+
export type walkPointer<node, path extends string, root = node, resource = never> = path extends `${infer segment}/${infer rest}` ? walkPointer<childOf<node, unescapeSegment<segment>>, rest, root, resourceAfter<node, root, resource>> : targetIn<schemaAt<childOf<node, unescapeSegment<path>>>, resourceAfter<node, root, resource>>;
|
|
540
|
+
export type resourceAfter<node, root, resource> = node extends ResourceNode ? rootOf<node, [resource] extends [never] ? root : resource> : resource;
|
|
541
|
+
/** a schema with a string identifier (in a record of schemas, `$id` would be a schema) */
|
|
542
|
+
export type ResourceNode = {
|
|
543
|
+
$id: string;
|
|
544
|
+
} | {
|
|
545
|
+
id: string;
|
|
546
|
+
};
|
|
547
|
+
export type targetIn<target, resource> = [
|
|
548
|
+
resource
|
|
549
|
+
] extends [never] ? target : [target] extends [UnresolvedRef] ? target : EmbeddedTarget<target, resource>;
|
|
550
|
+
/** a `$ref` whose target is inferred with its refs resolved against `resource` */
|
|
551
|
+
export interface EmbeddedTarget<target, resource> {
|
|
552
|
+
$ref: EmbeddedRef<target, resource>;
|
|
553
|
+
}
|
|
554
|
+
export interface EmbeddedRef<target, resource> {
|
|
555
|
+
target: target;
|
|
556
|
+
resource: resource;
|
|
557
|
+
}
|
|
558
|
+
export type childOf<node, key extends string> = node extends array ? key extends keyof node & `${number}` ? node[key] : number extends node["length"] ? key extends NonNegativeIntegerLiteral ? node[number] : UnresolvedRef : UnresolvedRef : node extends object ? key extends keyof node ? node[key] : key extends `${infer i extends number}` ? i extends keyof node ? node[i] : UnresolvedRef : UnresolvedRef : unknown extends node ? node : UnresolvedRef;
|
|
559
|
+
/** a pointer must end on a schema (an object or boolean) */
|
|
560
|
+
export type schemaAt<target> = target extends array ? UnresolvedRef : target extends object | boolean ? target : unknown extends target ? target : UnresolvedRef;
|
|
561
|
+
/** the target of a `$ref` that doesn't point to anything (infers as `unknown`) */
|
|
562
|
+
export type UnresolvedRef = noSuggest<"unresolvedRef">;
|
|
563
|
+
/** unescape `~1` then `~0`, each exactly once (RFC 6901) */
|
|
564
|
+
export type unescapeSegment<segment extends string> = segment extends `${string}~${string}` ? replaceAll<replaceAll<segment, "~1", "/">, "~0", "~"> : segment;
|
|
565
|
+
export type replaceAll<s extends string, from extends string, to extends string, result extends string = ""> = s extends `${infer head}${from}${infer tail}` ? replaceAll<tail, from, to, `${result}${head}${to}`> : `${result}${s}`;
|
|
566
|
+
export type inferObject<schema, root> = schema extends PropertiesSchema ? schema extends RequiredSchema ? inferRequiredProperties<schema["properties"], schema["required"], schema, root> : schema extends IndexedSchema ? inferIndexedObject<schema["properties"], never, schema, root> : inferOptionalProperties<schema["properties"], schema, root> : "properties" extends keyof schema ? object : schema extends RequiredSchema ? inferRequiredProperties<{}, schema["required"], schema, root> : schema extends IndexedSchema ? inferIndexedObject<{}, never, schema, root> : object;
|
|
567
|
+
export interface PropertiesSchema {
|
|
568
|
+
properties: object;
|
|
569
|
+
}
|
|
570
|
+
/** only a tuple's entries are definitely present, since an array may be empty */
|
|
571
|
+
export interface RequiredSchema {
|
|
572
|
+
required: readonly [string, ...string[]] | readonly [...string[], string];
|
|
573
|
+
}
|
|
574
|
+
export type inferOptionalProperties<props, schema, root> = UndeclaredKeyObject extends {
|
|
575
|
+
[k in keyof props]?: 0;
|
|
576
|
+
} ? [
|
|
577
|
+
keyof props
|
|
578
|
+
] extends [never] ? object : inferRecordObject<props, never, schema, root> : {
|
|
579
|
+
-readonly [k in keyof props]?: inferSchema<props[k], root, never>;
|
|
580
|
+
};
|
|
581
|
+
/** an object with a key that no literal key of `properties` declares, which
|
|
582
|
+
* is only assignable to their optional template if they have an index
|
|
583
|
+
* signature (or no keys) */
|
|
584
|
+
export type UndeclaredKeyObject = {
|
|
585
|
+
[undeclaredKey]: never;
|
|
586
|
+
};
|
|
587
|
+
/** a key no JSON property can have */
|
|
588
|
+
export declare const undeclaredKey: unique symbol;
|
|
589
|
+
/** `properties` with an index signature may be empty, so a key they don't
|
|
590
|
+
* declare takes the index value, which includes the record's values */
|
|
591
|
+
export type inferRecordObject<props, requiredKey extends PropertyKey, schema, root> = props extends unknown ? inferRecordProperties<literalTemplateOf<props> & {
|
|
592
|
+
[k in requiredKey]: 0;
|
|
593
|
+
} & {
|
|
594
|
+
[k in string]: 0;
|
|
595
|
+
}, props, keyof literalTemplateOf<props>, schema, root> : never;
|
|
596
|
+
export type literalTemplateOf<props> = {
|
|
597
|
+
[k in keyof props as {} extends Record<k, 0> ? never : k]?: 0;
|
|
598
|
+
};
|
|
599
|
+
export type inferRecordProperties<template, props, declaredKey, schema, root> = {
|
|
600
|
+
-readonly [k in keyof template]: k extends declaredKey ? inferSchema<props[k & keyof props], root, never> : indexValue<schema, root> | inferSchema<props[keyof props], root, never>;
|
|
601
|
+
} & unknown;
|
|
602
|
+
/** each member of a `required` union requires its own keys, and a union
|
|
603
|
+
* entry requires one of its keys, while an entry that isn't a literal (like
|
|
604
|
+
* `any`, `string` or a template) may be any key, so it requires nothing */
|
|
605
|
+
export type inferRequiredProperties<props, required extends array<string>, schema, root> = number extends required["length"] ? {} extends required ? inferOptionalObject<props, schema, root> : inferRequiredProperties<props, fixedEntriesOf<required, []>, schema, root> : required extends unknown ? UndeclaredKeyObject extends {
|
|
606
|
+
[k in required[number]]?: 0;
|
|
607
|
+
} ? inferRequiredChoices<props, requiredChoices<required, never>, schema, root> : entryArraysOf<required> extends arrayOf<required[number]> ? inferRequiredKeys<props, required[number], schema, root> : inferRequiredChoices<props, requiredChoices<required, never>, schema, root> : never;
|
|
608
|
+
/** each entry as an array, which is only assignable to a member of
|
|
609
|
+
* `arrayOf<required[number]>` if the entry is a single key */
|
|
610
|
+
export type entryArraysOf<required extends array> = required["length"] extends 1 ? required[number][] : {
|
|
611
|
+
[i in keyof required]: required[i][];
|
|
612
|
+
}[number];
|
|
613
|
+
export type arrayOf<t> = t extends unknown ? t[] : never;
|
|
614
|
+
/** each way to pick one key from every union entry */
|
|
615
|
+
export type requiredChoices<required, choice> = required extends readonly [infer head, ...infer tail] ? [
|
|
616
|
+
head
|
|
617
|
+
] extends [never] ? requiredChoices<tail, choice> : head extends unknown ? requiredChoices<tail, {} extends Record<head & string, 0> ? choice : choice | head> : never : [choice];
|
|
618
|
+
export type inferRequiredChoices<props, choices, schema, root> = choices extends [infer requiredKey extends string] ? inferRequiredKeys<props, requiredKey, schema, root> : never;
|
|
619
|
+
export type inferRequiredKeys<props, requiredKey extends string, schema, root> = schema extends IndexedSchema ? inferIndexedObject<props, requiredKey, schema, root> : props extends unknown ? [
|
|
620
|
+
requiredKey,
|
|
621
|
+
keyof props
|
|
622
|
+
] extends [keyof props, requiredKey] ? {
|
|
623
|
+
-readonly [k in keyof props]: inferSchema<props[k], root, never>;
|
|
624
|
+
} : UndeclaredKeyObject extends {
|
|
625
|
+
[k in keyof props]?: 0;
|
|
626
|
+
} ? [
|
|
627
|
+
keyof props
|
|
628
|
+
] extends [never] ? inferProperties<templateOf<props, requiredKey, never>, props, root> : inferRecordObject<props, requiredKey, schema, root> : inferProperties<templateOf<props, requiredKey, never>, props, root> : never;
|
|
629
|
+
/** a rest element may be empty, so only the fixed entries around it are required */
|
|
630
|
+
export type fixedEntriesOf<required, entries extends array<string>> = required extends readonly [infer head extends string, ...infer tail] ? fixedEntriesOf<tail, [...entries, head]> : required extends readonly [...infer init, infer last extends string] ? fixedEntriesOf<init, [...entries, last]> : entries;
|
|
631
|
+
export type inferOptionalObject<props, schema, root> = schema extends IndexedSchema ? inferIndexedObject<props, never, schema, root> : inferOptionalProperties<props, schema, root>;
|
|
632
|
+
/** an object indexed by `additionalProperties` (each member of a
|
|
633
|
+
* `properties` union is indexed separately, since its declared types are
|
|
634
|
+
* part of the index) */
|
|
635
|
+
export type inferIndexedObject<props, requiredKey extends PropertyKey, schema, root> = props extends unknown ? schema extends AdditionalPropertiesSchema ? inferOpenObject<props, requiredKey, schema, root> : inferPatternedObject<props, requiredKey, schema, root> : never;
|
|
636
|
+
/** an object indexed by `patternProperties`, unless they are all `false` */
|
|
637
|
+
export type inferPatternedObject<props, requiredKey extends PropertyKey, schema, root> = schema extends PatternPropertiesSchema ? schema extends FalsePatternsSchema ? inferClosedProperties<props, requiredKey, root> : inferOpenObject<props, requiredKey, schema, root> : inferClosedProperties<props, requiredKey, root>;
|
|
638
|
+
export interface FalsePatternsSchema {
|
|
639
|
+
patternProperties: FalsePatterns;
|
|
640
|
+
}
|
|
641
|
+
export interface FalsePatterns {
|
|
642
|
+
[pattern: string]: false;
|
|
643
|
+
}
|
|
644
|
+
export type inferOpenObject<props, requiredKey extends PropertyKey, schema, root> = UndeclaredKeyObject extends {
|
|
645
|
+
[k in keyof props]?: 0;
|
|
646
|
+
} ? [
|
|
647
|
+
keyof props
|
|
648
|
+
] extends [never] ? inferIndexedProperties<templateOf<props, requiredKey, string>, props, schema, root> : inferRecordObject<props, requiredKey, schema, root> : inferIndexedProperties<templateOf<props, requiredKey, string>, props, schema, root>;
|
|
649
|
+
/** an object whose keys and modifiers match the inferred object */
|
|
650
|
+
export type templateOf<props, requiredKey extends PropertyKey, indexKey extends string> = {
|
|
651
|
+
[k in keyof props]?: 0;
|
|
652
|
+
} & {
|
|
653
|
+
[k in requiredKey]: 0;
|
|
654
|
+
} & {
|
|
655
|
+
[k in indexKey]: 0;
|
|
656
|
+
};
|
|
657
|
+
export type inferProperties<template, props, root> = {
|
|
658
|
+
-readonly [k in keyof template]: k extends keyof props ? inferSchema<props[k], root, never> : unknown;
|
|
659
|
+
} & unknown;
|
|
660
|
+
export type inferClosedProperties<props, requiredKey extends PropertyKey, root> = [
|
|
661
|
+
keyof props | requiredKey
|
|
662
|
+
] extends [never] ? object : inferProperties<templateOf<props, requiredKey, never>, props, root>;
|
|
663
|
+
/** a TS index signature also applies to declared keys, so its value
|
|
664
|
+
* includes their types */
|
|
665
|
+
export type inferIndexedProperties<template, props, schema, root> = {
|
|
666
|
+
-readonly [k in keyof template]: string extends k ? indexValue<schema, root> | inferSchema<props[keyof props], root, never> : k extends keyof props ? inferSchema<props[k], root, never> : indexValue<schema, root>;
|
|
667
|
+
} & unknown;
|
|
668
|
+
export type IndexedSchema = AdditionalPropertiesSchema | PatternPropertiesSchema;
|
|
669
|
+
export interface AdditionalPropertiesSchema {
|
|
670
|
+
additionalProperties: object;
|
|
671
|
+
}
|
|
672
|
+
export interface PatternPropertiesSchema {
|
|
673
|
+
patternProperties: unknown;
|
|
674
|
+
}
|
|
675
|
+
export interface ClosedSchema {
|
|
676
|
+
additionalProperties: false;
|
|
677
|
+
}
|
|
678
|
+
/** the value of a key not in `properties` */
|
|
679
|
+
export type indexValue<schema, root> = schema extends PatternPropertiesSchema ? inferValues<schema["patternProperties"], root> | (schema extends EveryKeyPatternSchema ? never : additionalValue<schema, root>) : additionalValue<schema, root>;
|
|
680
|
+
/** the empty pattern matches every key, so `additionalProperties` applies
|
|
681
|
+
* to none (arktype writes a string index beside declared keys this way) */
|
|
682
|
+
export interface EveryKeyPatternSchema {
|
|
683
|
+
patternProperties: {
|
|
684
|
+
"": unknown;
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
export type additionalValue<schema, root> = schema extends AdditionalPropertiesSchema ? inferSchema<schema["additionalProperties"], root, never> : schema extends ClosedSchema ? never : unknown;
|
|
688
|
+
/** the union of the types inferred from each record of schemas */
|
|
689
|
+
export type inferValues<schemas, root> = schemas extends unknown ? inferSchema<schemas[keyof schemas], root, never> : never;
|
|
690
|
+
/** `domains` are the other values an untyped schema allows, added after a
|
|
691
|
+
* tuple's elements so they don't cost a union's level of instantiation depth */
|
|
692
|
+
export type inferArray<schema, root, seen, domains = never> = schema extends PrefixItemsSchema ? schema extends ItemsListSchema ? inferArray<Omit<schema, "prefixItems">, root, seen, domains> : [Extract<schema["items" & keyof schema], array>] extends [never] ? inferTupleSchema<schema["prefixItems"], minItemsOf<schema>, schema extends RestItemsSchema ? schema["items"] : true, root, seen, domains> : unknown[] | domains : schema extends UnprefixedItemsSchema ? {} extends schema["items"] ? inferWidened<schema["items"]>[] | domains : schema["items"] extends array ? inferTupleSchema<schema["items"], minItemsOf<schema>, schema extends AdditionalItemsSchema ? schema["additionalItems"] : true, root, seen, domains> : schema["items"] extends false ? [] | domains : inferSchema<schema["items"], root, never>[] | domains : schema extends OptionalPrefixItemsSchema ? inferOptionalPrefix<schema, root, seen, domains> : unknown[] | domains;
|
|
693
|
+
export interface PrefixItemsSchema {
|
|
694
|
+
prefixItems: array;
|
|
695
|
+
}
|
|
696
|
+
export interface ItemsListSchema {
|
|
697
|
+
items: array;
|
|
698
|
+
}
|
|
699
|
+
/** an undefined `prefixItems` is absent */
|
|
700
|
+
export interface UnprefixedItemsSchema {
|
|
701
|
+
items: unknown;
|
|
702
|
+
prefixItems?: never;
|
|
703
|
+
}
|
|
704
|
+
/** a `prefixItems` that may be undefined (a weak type, so a schema without
|
|
705
|
+
* the keyword doesn't match) */
|
|
706
|
+
export interface OptionalPrefixItemsSchema {
|
|
707
|
+
prefixItems?: unknown;
|
|
708
|
+
}
|
|
709
|
+
/** an absent `prefixItems` leaves `items` to every element, so only an
|
|
710
|
+
* `items` schema (other than `true`) keeps both readings */
|
|
711
|
+
export type inferOptionalPrefix<schema extends OptionalPrefixItemsSchema, root, seen, domains> = schema extends RestItemsSchema ? schema["items"] extends false ? inferPresentPrefix<schema, root, seen, domains> : schema["items"] extends true ? unknown[] | domains : inferPresentPrefix<schema, root, seen, domains> | inferArray<Omit<schema, "prefixItems">, root, seen, domains> : unknown[] | domains;
|
|
712
|
+
export type inferPresentPrefix<schema extends OptionalPrefixItemsSchema & RestItemsSchema, root, seen, domains> = Exclude<schema["prefixItems"], undefined> extends infer items extends array ? inferTupleSchema<items, minItemsOf<schema>, schema["items"], root, seen, domains> : unknown[] | domains;
|
|
713
|
+
export interface ItemsSchema {
|
|
714
|
+
items: unknown;
|
|
715
|
+
}
|
|
716
|
+
/** a rest that may be undefined may be absent, leaving the array open */
|
|
717
|
+
export interface RestItemsSchema {
|
|
718
|
+
items: {};
|
|
719
|
+
}
|
|
720
|
+
export interface AdditionalItemsSchema {
|
|
721
|
+
additionalItems: {};
|
|
722
|
+
}
|
|
723
|
+
export type minItemsOf<schema> = schema extends MinItemsSchema ? schema["minItems"] : 0;
|
|
724
|
+
export interface MinItemsSchema {
|
|
725
|
+
minItems: number;
|
|
726
|
+
}
|
|
727
|
+
/** a rest schema with subschemas may nest tuples, so its elements are an
|
|
728
|
+
* `X[]`, which TS defers, after a prefix inferred on its own (spreading
|
|
729
|
+
* the prefix's mapped type would cost a level of depth) */
|
|
730
|
+
export type inferTupleSchema<items extends array, minItems extends number, rest, root, seen, domains> = items extends unknown ? rest extends false ? inferPrefix<items, minItems, root, seen, domains> : [keyof rest & (EagerKeyword | "$ref")] extends [never] ? restShape<present<items>, minItems, rest[]> extends (infer shape extends array) ? withDomains<inferOptionalElements<shape, root, seen>, domains> : never : restShape<present<items>, minItems, []> extends (infer shape extends array) ? inferOptionalElements<shape, root, seen> extends (infer prefix extends array) ? withDomains<[...prefix, ...inferSchema<rest, root, seen>[]], domains> : never : never : never;
|
|
731
|
+
/** a hand-written optional element may be absent, leaving its position to
|
|
732
|
+
* the rest, so it may be any value */
|
|
733
|
+
export type present<items extends array> = undefined extends items[number] ? widenOptional<items> : items;
|
|
734
|
+
export type widenOptional<items extends array> = {
|
|
735
|
+
[i in keyof items]-?: [items[i]] extends [anyOrNever] ? items[i] : undefined extends items[i] ? true : items[i];
|
|
736
|
+
};
|
|
737
|
+
/** a widened minItems may be 0. Without a rest, a hand-written optional
|
|
738
|
+
* element is only absent from a shorter array, so one that minItems
|
|
739
|
+
* requires is present */
|
|
740
|
+
export type inferPrefix<items extends array, minItems extends number, root, seen, domains> = minItems extends 0 ? withDomains<inferOptionalElements<optionalShape<items>, root, seen>, domains> : number extends minItems ? withDomains<inferOptionalElements<optionalShape<items>, root, seen>, domains> : `${minItems}` extends keyof items ? withDomains<inferOptionalElements<partialShape<requiredShape<items>, digitPrefix<`${minItems}`, []>>, root, seen>, domains> : withDomains<{
|
|
741
|
+
-readonly [i in keyof items]-?: inferSchema<items[i] & {}, root, seen>;
|
|
742
|
+
}, domains>;
|
|
743
|
+
export type requiredShape<items extends array> = undefined extends items[number] ? {
|
|
744
|
+
[i in keyof items]-?: items[i];
|
|
745
|
+
} : items;
|
|
746
|
+
/** an untyped schema's other `domains` are added once its tuple is inferred,
|
|
747
|
+
* so they don't cost a union's level of instantiation depth */
|
|
748
|
+
export type withDomains<tuple, domains> = [
|
|
749
|
+
domains
|
|
750
|
+
] extends [never] ? tuple : tuple extends unknown ? tuple | domains : never;
|
|
751
|
+
/** elements are inferred in a single mapped type, since every type a level
|
|
752
|
+
* of nesting passes through costs instantiation depth. An optional
|
|
753
|
+
* element's type includes `undefined`, which isn't a schema */
|
|
754
|
+
export type inferOptionalElements<shape, root, seen> = {
|
|
755
|
+
-readonly [i in keyof shape]: inferSchema<shape[i] & {}, root, seen>;
|
|
756
|
+
};
|
|
757
|
+
/** a widened minItems may be 0. A conditional, so the tuple is built before
|
|
758
|
+
* its elements are inferred */
|
|
759
|
+
export type restShape<items extends array, minItems extends number, rest extends array> = minItems extends 0 ? [...optionalShape<items>, ...rest] : number extends minItems ? [...optionalShape<items>, ...rest] : `${minItems}` extends keyof items ? [
|
|
760
|
+
...partialShape<items, digitPrefix<`${minItems}`, []>>,
|
|
761
|
+
...rest
|
|
762
|
+
] : [...items, ...rest];
|
|
763
|
+
/** a tuple of `unknown` whose length is the number represented by `digits` */
|
|
764
|
+
export type digitPrefix<digits extends string, prefix extends array> = digits extends `${infer digit extends keyof DigitPrefixes}${infer rest}` ? digitPrefix<rest, [
|
|
765
|
+
...prefix,
|
|
766
|
+
...prefix,
|
|
767
|
+
...prefix,
|
|
768
|
+
...prefix,
|
|
769
|
+
...prefix,
|
|
770
|
+
...prefix,
|
|
771
|
+
...prefix,
|
|
772
|
+
...prefix,
|
|
773
|
+
...prefix,
|
|
774
|
+
...prefix,
|
|
775
|
+
...DigitPrefixes[digit]
|
|
776
|
+
]> : prefix;
|
|
777
|
+
export type DigitPrefixes = {
|
|
778
|
+
"0": [];
|
|
779
|
+
"1": [unknown];
|
|
780
|
+
"2": [unknown, unknown];
|
|
781
|
+
"3": [unknown, unknown, unknown];
|
|
782
|
+
"4": [unknown, unknown, unknown, unknown];
|
|
783
|
+
"5": [unknown, unknown, unknown, unknown, unknown];
|
|
784
|
+
"6": [unknown, unknown, unknown, unknown, unknown, unknown];
|
|
785
|
+
"7": [unknown, unknown, unknown, unknown, unknown, unknown, unknown];
|
|
786
|
+
"8": [unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown];
|
|
787
|
+
"9": [
|
|
788
|
+
unknown,
|
|
789
|
+
unknown,
|
|
790
|
+
unknown,
|
|
791
|
+
unknown,
|
|
792
|
+
unknown,
|
|
793
|
+
unknown,
|
|
794
|
+
unknown,
|
|
795
|
+
unknown,
|
|
796
|
+
unknown
|
|
797
|
+
];
|
|
798
|
+
};
|
|
799
|
+
export type optionalShape<items extends array> = {
|
|
800
|
+
-readonly [i in keyof items]?: items[i];
|
|
801
|
+
};
|
|
802
|
+
export type partialShape<items extends array, prefix extends array> = items extends readonly [...prefix, ...infer optional] ? items extends readonly [...infer required, ...optional] ? [
|
|
803
|
+
...required,
|
|
804
|
+
...optionalShape<optional>
|
|
805
|
+
] : never : never;
|