arktype 2.1.9 → 2.1.11

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  <h1 align="center">ArkType</h1>
2
- <h3 align="center"><i>Optimized runtime validation for TypeScript syntax</i></h1>
2
+ <h3 align="center"><i>TypeScript's 1:1 validator, optimized from editor to runtime</i></h1>
3
3
 
4
4
  ArkType is a runtime validation library that parses optimized validators from familiar, type-safe syntax.
5
5
 
package/out/config.d.ts CHANGED
@@ -1,11 +1,9 @@
1
- import type { arkKind } from "@ark/schema";
1
+ import type { arkKind, TypeMeta } from "@ark/schema";
2
2
  import type { Ark } from "./keywords/keywords.ts";
3
3
  import { type ArkSchemaConfig } from "@ark/schema/config";
4
4
  import type { anyOrNever } from "@ark/util";
5
- export type TypeMeta = Omit<ArkEnv.meta, "onFail">;
6
- export type TypeMetaInput = string | TypeMeta;
7
5
  export type KeywordConfig = {
8
- [k in keyof Ark.flat as parseConfigurableFlatAlias<k, Ark.flat[k]>]?: TypeMetaInput;
6
+ [k in keyof Ark.flat as parseConfigurableFlatAlias<k, Ark.flat[k]>]?: TypeMeta.Collapsible;
9
7
  };
10
8
  type parseConfigurableFlatAlias<k extends string, v> = [
11
9
  v
@@ -1,7 +1,7 @@
1
- import type { BaseRoot, Disjoint, JsonSchema, Morph, Predicate, StandardSchemaV1, UndeclaredKeyBehavior } from "@ark/schema";
1
+ import type { BaseNode, BaseRoot, Disjoint, JsonSchema, Morph, NodeSelector, Predicate, StandardSchemaV1, TypeMeta, UndeclaredKeyBehavior } from "@ark/schema";
2
2
  import type { anyOrNever, array, Callable, ErrorMessage, inferred, JsonStructure, unset } from "@ark/util";
3
3
  import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipe, inferPipes, InferredMorph, Out, To } from "../attributes.ts";
4
- import type { ArkAmbient, TypeMetaInput } from "../config.ts";
4
+ import type { ArkAmbient } from "../config.ts";
5
5
  import type { type } from "../keywords/keywords.ts";
6
6
  import type { Scope } from "../scope.ts";
7
7
  import type { ArrayType } from "./array.ts";
@@ -174,7 +174,7 @@ export interface Inferred<out t = unknown, $ = {}> {
174
174
  * // error message at root is affected, leading to a misleading description
175
175
  * const nonObject = notOddBox(null) // must be not odd (was null)
176
176
  */
177
- configure<meta extends TypeMetaInput>(meta: meta): this;
177
+ configure: NodeSelector.SelectableFn<TypeMeta.MappableInput, this>;
178
178
  /**
179
179
  * #### add description to shallow references
180
180
  *
@@ -187,7 +187,7 @@ export interface Inferred<out t = unknown, $ = {}> {
187
187
  * // ArkErrors: must be a string like 'a...z' (was "albatross")
188
188
  * const badPattern = aToZ("albatross")
189
189
  */
190
- describe(description: string): this;
190
+ describe: NodeSelector.SelectableFn<string, this>;
191
191
  /**
192
192
  * #### apply undeclared key behavior
193
193
  *
@@ -357,6 +357,16 @@ export interface Inferred<out t = unknown, $ = {}> {
357
357
  * const parseUser = type("string").pipe(s => JSON.parse(s)).to({ name: "string" })
358
358
  */
359
359
  to<const def, r = instantiateType<inferPipe<t, type.infer<def, $>>, $>>(def: type.validate<def, $>): r extends infer _ ? _ : never;
360
+ /**
361
+ * #### query internal node references
362
+ *
363
+ * @experimental filters and returns the Type's internal representation from `@ark/schema`
364
+ *
365
+ * @example
366
+ * // ["blue", "red"]
367
+ * const values = type("'red' | 'blue'").select("unit").map(u => u.unit)
368
+ */
369
+ select: BaseNode["select"];
360
370
  }
361
371
  /** @ts-ignore cast variance */
362
372
  interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => distill.Out<t> | ArkEnv.onFail>, Inferred<t, $> {
@@ -383,7 +393,7 @@ interface Type<out t = unknown, $ = {}> extends Callable<(data: unknown) => dist
383
393
  /**
384
394
  * #### union with the parsed Type
385
395
  *
386
- * ⚠️ a union that could apply different morphs to the same data is a ParseError ([docs](https://arktype.io/docs/expressions/union-morphs))
396
+ * ⚠️ a union that could apply different morphs to the same data is a ParseError ({@link https://arktype.io/docs/expressions#union-morphs | docs})
387
397
  *
388
398
  * @example
389
399
  * // Type<string | { box: string }>
@@ -1,7 +1,6 @@
1
- import { type BaseParseContext, type BaseRoot, type Morph, type Predicate, type unwrapDefault } from "@ark/schema";
1
+ import { type BaseParseContext, type BaseRoot, type Morph, type Predicate, type TypeMeta, type unwrapDefault } from "@ark/schema";
2
2
  import { type array, type BuiltinObjectKind, type Constructor, type Domain, type show } from "@ark/util";
3
3
  import type { defaultFor, distill, inferIntersection, inferMorphOut, inferPipe, inferPredicate, Out, withDefault } from "../attributes.ts";
4
- import type { TypeMetaInput } from "../config.ts";
5
4
  import type { type } from "../keywords/keywords.ts";
6
5
  import type { inferDefinition, validateDefinition } from "./definition.ts";
7
6
  import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.ts";
@@ -17,7 +16,7 @@ export type validatePrefixExpression<def extends IndexZeroExpression, $, args> =
17
16
  export type validateIndexOneExpression<def extends IndexOneExpression, $, args> = def[1] extends TuplePostfixOperator ? readonly [validateDefinition<def[0], $, args>, def[1]] : readonly [
18
17
  validateDefinition<def[0], $, args>,
19
18
  def["length"] extends 2 ? writeMissingRightOperandMessage<def[1]> : def[1],
20
- def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "|>" ? validateDefinition<def[2], $, args> : def[1] extends "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ? TypeMetaInput : validateDefinition<def[2], $, args>
19
+ def[1] extends "|" ? validateDefinition<def[2], $, args> : def[1] extends "&" ? validateDefinition<def[2], $, args> : def[1] extends ":" ? Predicate<type.infer.Out<def[0], $, args>> : def[1] extends "=>" ? Morph<type.infer.Out<def[0], $, args>> : def[1] extends "|>" ? validateDefinition<def[2], $, args> : def[1] extends "=" ? defaultFor<type.infer.In<def[0], $, args>> : def[1] extends "@" ? TypeMeta.MappableInput : validateDefinition<def[2], $, args>
21
20
  ];
22
21
  export type UnparsedTupleExpressionInput = {
23
22
  instanceof: Constructor;
@@ -34,7 +34,7 @@ export const parseNarrowTuple = (def, ctx) => {
34
34
  }
35
35
  return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
36
36
  };
37
- const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
37
+ const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureDescendants(def[2], "shallow");
38
38
  const defineIndexOneParsers = (parsers) => parsers;
39
39
  const postfixParsers = defineIndexOneParsers({
40
40
  "[]": parseArrayTuple,
@@ -101,7 +101,7 @@ const appendVariadicElement = (base, element) => {
101
101
  return base;
102
102
  };
103
103
  const appendSpreadBranch = (base, branch) => {
104
- const spread = branch.firstReferenceOfKind("sequence");
104
+ const spread = branch.select({ method: "find", kind: "sequence" });
105
105
  if (!spread) {
106
106
  // the only array with no sequence reference is unknown[]
107
107
  return appendVariadicElement(base, $ark.intrinsic.unknown);
package/out/scope.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { BaseScope, type AliasDefEntry, type ArkSchemaRegistry, type ArkSchemaScopeConfig, type BaseNode, type BaseParseContext, type BaseParseContextInput, type BaseParseOptions, type BaseRoot, type GenericAst, type GenericParamAst, type GenericParamDef, type NodeKind, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type ResolvedScopeConfig, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
1
+ import { BaseScope, type AliasDefEntry, type ArkSchemaRegistry, type ArkSchemaScopeConfig, type BaseNode, type BaseParseContext, type BaseParseContextInput, type BaseParseOptions, type BaseRoot, type GenericAst, type GenericParamAst, type GenericParamDef, type NodeKind, type NodeSchema, type PreparsedNodeResolution, type PrivateDeclaration, type RootKind, type RootSchema, type arkKind, type exportedNameOf, type nodeOfKind, type reducibleKindOf, type toInternalScope, type writeDuplicateAliasError } from "@ark/schema";
2
2
  import { type Brand, type ErrorType, type JsonStructure, type anyOrNever, type array, type flattenListable, type noSuggest } from "@ark/util";
3
- import type { TypeMetaInput } from "./config.ts";
4
3
  import { type GenericDeclaration, type GenericParser, type ParameterString, type baseGenericConstraints, type parseGenericParams, type parseValidGenericParams } from "./generic.ts";
5
4
  import type { Ark, type } from "./keywords/keywords.ts";
6
5
  import { InternalMatchParser, type MatchParser } from "./match.ts";
@@ -9,7 +8,7 @@ import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
9
8
  import { type inferDefinition } from "./parser/definition.ts";
10
9
  import type { ParsedOptionalProperty } from "./parser/property.ts";
11
10
  import type { ParsedDefaultableProperty } from "./parser/shift/operator/default.ts";
12
- import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
11
+ import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser, type ValueOfTypeParser } from "./type.ts";
13
12
  /** The convenience properties attached to `scope` */
14
13
  export type ScopeParserAttachments = Omit<ScopeParser, never>;
15
14
  export interface ArkScopeConfig extends ArkSchemaScopeConfig {
@@ -68,11 +67,7 @@ export declare const $arkTypeRegistry: ArkTypeRegistry;
68
67
  export interface InternalScope {
69
68
  constructor: typeof InternalScope;
70
69
  }
71
- interface ResolvedTypeScopeConfig extends ResolvedScopeConfig {
72
- keywords?: Record<string, TypeMetaInput>;
73
- }
74
70
  export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
75
- resolvedConfig: ResolvedTypeScopeConfig;
76
71
  get ambientAttachments(): Ark.boundTypeAttachments<$> | undefined;
77
72
  protected preparseOwnAliasEntry(alias: string, def: unknown): AliasDefEntry;
78
73
  parseGenericParams(def: string, opts: BaseParseOptions): array<GenericParamDef>;
@@ -80,6 +75,7 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
80
75
  protected preparseOwnDefinitionFormat(def: unknown, opts: BaseParseOptions): BaseRoot | BaseParseContextInput;
81
76
  parseOwnDefinitionFormat(def: unknown, ctx: BaseParseContext): BaseRoot;
82
77
  unit: UnitTypeParser<$>;
78
+ valueOf: ValueOfTypeParser<$>;
83
79
  enumerated: EnumeratedTypeParser<$>;
84
80
  instanceOf: InstanceOfTypeParser<$>;
85
81
  match: InternalMatchParser;
@@ -113,6 +109,8 @@ export interface Scope<$ = {}> {
113
109
  node<kinds extends NodeKind | array<RootKind>>(kinds: kinds, schema: NodeSchema<flattenListable<kinds>>, opts?: BaseParseOptions): nodeOfKind<reducibleKindOf<flattenListable<kinds>>>;
114
110
  unit: UnitTypeParser<$>;
115
111
  enumerated: EnumeratedTypeParser<$>;
112
+ valueOf: ValueOfTypeParser<$>;
113
+ instanceOf: InstanceOfTypeParser<$>;
116
114
  type: TypeParser<$>;
117
115
  match: MatchParser<$>;
118
116
  declare: DeclarationParser<$>;
package/out/scope.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { $ark, BaseScope, hasArkKind, parseGeneric } from "@ark/schema";
2
- import { flatMorph, isArray, isThunk, throwParseError } from "@ark/util";
2
+ import { enumValues, flatMorph, isArray, isThunk, throwParseError } from "@ark/util";
3
3
  import { parseGenericParamName } from "./generic.js";
4
4
  import { InternalMatchParser } from "./match.js";
5
5
  import { shallowDefaultableMessage, shallowOptionalMessage } from "./parser/ast/validate.js";
@@ -81,6 +81,7 @@ export class InternalScope extends BaseScope {
81
81
  return result;
82
82
  }
83
83
  unit = value => this.units([value]);
84
+ valueOf = tsEnum => this.units(enumValues(tsEnum));
84
85
  enumerated = (...values) => this.units(values);
85
86
  instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
86
87
  match = new InternalMatchParser(this);
package/out/type.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { ArkErrors, BaseRoot, type BaseParseOptions, type Morph, type Predicate, type RootSchema } from "@ark/schema";
1
+ import { ArkErrors, BaseRoot, type BaseParseOptions, type Morph, type Predicate, type RootSchema, type TypeMeta } from "@ark/schema";
2
2
  import { Callable, Hkt, type Constructor, type array, type conform } from "@ark/util";
3
3
  import type { distill } from "./attributes.ts";
4
- import type { TypeMetaInput } from "./config.ts";
5
4
  import type { Generic, GenericParser, ParameterString, baseGenericConstraints, parseValidGenericParams, validateParameterString } from "./generic.ts";
6
5
  import type { Ark, keywords, type } from "./keywords/keywords.ts";
7
6
  import type { MatchParser } from "./match.ts";
@@ -38,7 +37,7 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
38
37
  *
39
38
  * @example type("string", "|", { foo: "number" })
40
39
  */
41
- <const zero, const one, const rest extends array, r = type.instantiate<[zero, one, ...rest], $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, ArgTwoOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "|>" ? [type.validate<rest[0], $>] : one extends "@" ? [TypeMetaInput] : [type.validate<rest[0], $>] : []): r extends infer _ ? _ : never;
40
+ <const zero, const one, const rest extends array, r = type.instantiate<[zero, one, ...rest], $>>(_0: zero extends IndexZeroOperator ? zero : type.validate<zero, $>, _1: zero extends "keyof" ? type.validate<one, $> : zero extends "instanceof" ? conform<one, Constructor> : zero extends "===" ? conform<one, unknown> : conform<one, ArgTwoOperator>, ..._2: zero extends "===" ? rest : zero extends "instanceof" ? conform<rest, readonly Constructor[]> : one extends TupleInfixOperator ? one extends ":" ? [Predicate<distill.In<type.infer<zero, $>>>] : one extends "=>" ? [Morph<distill.Out<type.infer<zero, $>>, unknown>] : one extends "|>" ? [type.validate<rest[0], $>] : one extends "@" ? [TypeMeta.MappableInput] : [type.validate<rest[0], $>] : []): r extends infer _ ? _ : never;
42
41
  /**
43
42
  * An alias of the {@link ArkErrors} class, an instance of which is returned when a {@link Type}
44
43
  * is invoked with invalid input.
@@ -81,6 +80,13 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
81
80
  * @example const argsForm = type('===', 'foo', 'bar', obj)
82
81
  */
83
82
  enumerated: EnumeratedTypeParser<$>;
83
+ /**
84
+ * Create a {@link Type} that is satisfied only by one of the Object.values() of the argument passed to this function.
85
+ *
86
+ * ⚠️ For TypeScript enum compatibility, values at numeric keys with corresponding numeric values will not be included.
87
+ * @example const myEnum = type.valueOf(myTsEnum)
88
+ */
89
+ valueOf: ValueOfTypeParser<$>;
84
90
  /**
85
91
  * Create a {@link Type} that is satisfied only by a value of a specific class.
86
92
  * @example const array = type.instanceOf(Array)
@@ -96,6 +102,7 @@ export type DeclarationParser<$> = <preinferred>() => {
96
102
  export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
97
103
  export type InstanceOfTypeParser<$> = <const t extends object>(ctor: Constructor<t>) => Type<t, $>;
98
104
  export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
105
+ export type ValueOfTypeParser<$> = <const o extends object>(o: o) => Type<o[keyof o], $>;
99
106
  export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
100
107
  export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
101
108
  export type Type<t = unknown, $ = {}> = instantiateType<t, $>;
package/out/type.js CHANGED
@@ -17,7 +17,8 @@ export class InternalTypeParser extends Callable {
17
17
  keywords: $.ambient,
18
18
  unit: $.unit,
19
19
  enumerated: $.enumerated,
20
- instanceOf: $.instanceOf
20
+ instanceOf: $.instanceOf,
21
+ valueOf: $.valueOf
21
22
  },
22
23
  // also won't be defined during bootstrapping
23
24
  $.ambientAttachments);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "arktype",
3
- "description": "Optimized runtime validation for TypeScript syntax",
4
- "version": "2.1.9",
3
+ "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
+ "version": "2.1.11",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
@@ -34,8 +34,8 @@
34
34
  "out"
35
35
  ],
36
36
  "dependencies": {
37
- "@ark/util": "0.44.4",
38
- "@ark/schema": "0.44.4"
37
+ "@ark/util": "0.45.1",
38
+ "@ark/schema": "0.45.1"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"