arktype 2.0.0-rc.16 → 2.0.0-rc.18

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.
@@ -111,8 +111,8 @@ export type ExactlyLength<rule> = {
111
111
  export type AttributeInferenceBehavior = "brand" | "associate";
112
112
  export type associateAttributes<t, attributes extends Attributes> = attachAttributes<t, attributes, "associate">;
113
113
  export type brandAttributes<t, attributes extends Attributes> = attachAttributes<t, attributes, "brand">;
114
- type attachAttributes<t, attributes extends Attributes, behavior extends AttributeInferenceBehavior = "associate"> = t extends InferredMorph<infer i, infer o> ? (In: leftIfEqual<i, _attachAttributes<i, attributes, behavior>>) => o : leftIfEqual<t, _attachAttributes<t, attributes, behavior>>;
115
- type _attachAttributes<t, attributes extends Attributes, behavior extends AttributeInferenceBehavior> = t extends null | undefined ? t : t extends of<infer base, infer existingAttributes> ? "brand" extends keyof t[attributesKey] | behavior ? brandMultiple<base, existingAttributes & attributes> : associateMultiple<base, existingAttributes & attributes> : extractIfSingleAttributeEntry<attributes> extends (AttributeEntry<infer kind, infer value>) ? "brand" extends behavior ? brandSingle<t, attributes, kind, value> : associateSingle<t, attributes, kind, value> : "brand" extends behavior ? brandMultiple<t, attributes> : associateMultiple<t, attributes>;
114
+ type attachAttributes<t, attributes extends Attributes, behavior extends AttributeInferenceBehavior = "associate", unmorphedT = Exclude<t, InferredMorph>> = t extends InferredMorph<infer i, infer o> ? (In: leftIfEqual<i, _attachAttributes<i, attributes, behavior>>) => o : leftIfEqual<unmorphedT, _attachAttributes<unmorphedT, attributes, behavior>>;
115
+ type _attachAttributes<t, attributes extends Attributes, behavior extends AttributeInferenceBehavior, distributed = t> = distributed extends null | undefined ? distributed : distributed extends of<infer base, infer existingAttributes> ? "brand" extends keyof distributed[attributesKey] | behavior ? brandMultiple<base, existingAttributes & attributes> : associateMultiple<base, existingAttributes & attributes> : extractIfSingleAttributeEntry<attributes> extends (AttributeEntry<infer kind, infer value>) ? "brand" extends behavior ? brandSingle<t, attributes, kind, value> : associateSingle<t, attributes, kind, value> : "brand" extends behavior ? brandMultiple<t, attributes> : associateMultiple<t, attributes>;
116
116
  type associateMultiple<t, attributes extends Attributes> = [
117
117
  t,
118
118
  string
@@ -162,6 +162,10 @@ type maxLengthSchemaToConstraint<schema, rule> = schema extends {
162
162
  exclusive: true;
163
163
  } ? LessThanLength<rule> : AtMostLength<rule>;
164
164
  export type associateAttributesFromSchema<t, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = associateAttributes<t, schemaToAttributes<kind, schema>>;
165
+ export type associateAttributesFromStringSchema<t extends string, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = conform<associateAttributes<t, schemaToAttributes<kind, schema>>, string>;
166
+ export type associateAttributesFromNumberSchema<t extends number, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = conform<associateAttributes<t, schemaToAttributes<kind, schema>>, number>;
167
+ export type associateAttributesFromDateSchema<t extends Date, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = conform<associateAttributes<t, schemaToAttributes<kind, schema>>, Date>;
168
+ export type associateAttributesFromArraySchema<t extends readonly unknown[], kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = conform<associateAttributes<t, schemaToAttributes<kind, schema>>, readonly unknown[]>;
165
169
  export type schemaToAttributes<kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = normalizePrimitiveConstraintRoot<schema> extends infer rule ? kind extends "pattern" ? Matching<rule> : kind extends "divisor" ? DivisibleBy<rule> : kind extends "min" ? number.minSchemaToConstraint<schema, rule> : kind extends "max" ? number.maxSchemaToConstraint<schema, rule> : kind extends "minLength" ? minLengthSchemaToConstraint<schema, rule> : kind extends "maxLength" ? maxLengthSchemaToConstraint<schema, rule> : kind extends "exactLength" ? ExactlyLength<rule> : kind extends "after" ? Date.afterSchemaToConstraint<schema, rule> : kind extends "before" ? Date.beforeSchemaToConstraint<schema, rule> : Anonymous : never;
166
170
  export type distill<t, opts extends distill.Options = {}> = finalizeDistillation<t, _distill<t, opts>>;
167
171
  export declare namespace distill {
@@ -26,6 +26,8 @@ export declare namespace Ark {
26
26
  interface typeAttachments extends arkTsKeywords.$ {
27
27
  Key: arkBuiltins.$["Key"];
28
28
  Record: arkTsGenerics.$["Record"];
29
+ Date: arkPrototypes.$["Date"];
30
+ Array: arkPrototypes.$["Array"]["root"];
29
31
  }
30
32
  interface boundTypeAttachments<$> extends Omit<BoundModule<typeAttachments, $>, arkKind> {
31
33
  }
@@ -7,7 +7,9 @@ import { arkTsGenerics, arkTsKeywords, object, unknown } from "./ts.js";
7
7
  $arkTypeRegistry.typeAttachments = {
8
8
  ...arkTsKeywords,
9
9
  Key: arkBuiltins.Key,
10
- Record: arkTsGenerics.Record
10
+ Record: arkTsGenerics.Record,
11
+ Array: arkPrototypes.Array.root,
12
+ Date: arkPrototypes.Date
11
13
  };
12
14
  export const ark = scope({
13
15
  ...arkTsKeywords,
@@ -1,13 +1,13 @@
1
1
  import type { ExactLength, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
2
- import type { associateAttributesFromSchema } from "../attributes.ts";
2
+ import type { associateAttributesFromArraySchema } from "../attributes.ts";
3
3
  import type { ObjectType } from "./object.ts";
4
4
  interface Type<
5
5
  /** @ts-ignore cast variance */
6
6
  out t extends readonly unknown[] = readonly unknown[], $ = {}> extends ObjectType<t, $> {
7
- atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "minLength", schema>, $>;
8
- atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "maxLength", schema>, $>;
9
- moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
10
- lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
11
- exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<associateAttributesFromSchema<t, "exactLength", schema>, $>;
7
+ atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromArraySchema<t, "minLength", schema>, $>;
8
+ atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromArraySchema<t, "maxLength", schema>, $>;
9
+ moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromArraySchema<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
10
+ lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromArraySchema<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
11
+ exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<associateAttributesFromArraySchema<t, "exactLength", schema>, $>;
12
12
  }
13
13
  export type { Type as ArrayType };
@@ -1,11 +1,11 @@
1
1
  import type { ExclusiveDateRangeSchema, exclusivizeRangeSchema, InclusiveDateRangeSchema } from "@ark/schema";
2
- import type { associateAttributesFromSchema } from "../attributes.ts";
2
+ import type { associateAttributesFromDateSchema } from "../attributes.ts";
3
3
  import type { ObjectType } from "./object.ts";
4
4
  /** @ts-ignore cast variance */
5
5
  interface Type<out t extends globalThis.Date = globalThis.Date, $ = {}> extends ObjectType<t, $> {
6
- atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "after", schema>, $>;
7
- atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "before", schema>, $>;
8
- laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "after", exclusivizeRangeSchema<schema>>, $>;
9
- earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "before", exclusivizeRangeSchema<schema>>, $>;
6
+ atOrAfter<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromDateSchema<t, "after", schema>, $>;
7
+ atOrBefore<const schema extends InclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromDateSchema<t, "before", schema>, $>;
8
+ laterThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromDateSchema<t, "after", exclusivizeRangeSchema<schema>>, $>;
9
+ earlierThan<const schema extends ExclusiveDateRangeSchema>(schema: schema): Type<associateAttributesFromDateSchema<t, "before", exclusivizeRangeSchema<schema>>, $>;
10
10
  }
11
11
  export type { Type as DateType };
@@ -1,12 +1,12 @@
1
1
  import type { Divisor, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema } from "@ark/schema";
2
- import type { associateAttributesFromSchema } from "../attributes.ts";
2
+ import type { associateAttributesFromNumberSchema } from "../attributes.ts";
3
3
  import type { BaseType } from "./base.ts";
4
4
  /** @ts-ignore cast variance */
5
5
  interface Type<out t extends number = number, $ = {}> extends BaseType<t, $> {
6
- divisibleBy<const schema extends Divisor.Schema>(schema: schema): Type<associateAttributesFromSchema<t, "divisor", schema>, $>;
7
- atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "min", schema>, $>;
8
- atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "max", schema>, $>;
9
- moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "min", exclusivizeRangeSchema<schema>>, $>;
10
- lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "max", exclusivizeRangeSchema<schema>>, $>;
6
+ divisibleBy<const schema extends Divisor.Schema>(schema: schema): Type<associateAttributesFromNumberSchema<t, "divisor", schema>, $>;
7
+ atLeast<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromNumberSchema<t, "min", schema>, $>;
8
+ atMost<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromNumberSchema<t, "max", schema>, $>;
9
+ moreThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromNumberSchema<t, "min", exclusivizeRangeSchema<schema>>, $>;
10
+ lessThan<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromNumberSchema<t, "max", exclusivizeRangeSchema<schema>>, $>;
11
11
  }
12
12
  export type { Type as NumberType };
@@ -1,13 +1,13 @@
1
1
  import type { ExactLength, ExclusiveNumericRangeSchema, exclusivizeRangeSchema, InclusiveNumericRangeSchema, Pattern } from "@ark/schema";
2
- import type { associateAttributesFromSchema } from "../attributes.ts";
2
+ import type { associateAttributesFromStringSchema } from "../attributes.ts";
3
3
  import type { BaseType } from "./base.ts";
4
4
  /** @ts-ignore cast variance */
5
5
  interface Type<out t extends string = string, $ = {}> extends BaseType<t, $> {
6
- matching<const schema extends Pattern.Schema>(schema: schema): Type<associateAttributesFromSchema<t, "pattern", schema>, $>;
7
- atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "minLength", schema>, $>;
8
- atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "maxLength", schema>, $>;
9
- moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
10
- lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromSchema<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
11
- exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<associateAttributesFromSchema<t, "exactLength", schema>, $>;
6
+ matching<const schema extends Pattern.Schema>(schema: schema): Type<associateAttributesFromStringSchema<t, "pattern", schema>, $>;
7
+ atLeastLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromStringSchema<t, "minLength", schema>, $>;
8
+ atMostLength<const schema extends InclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromStringSchema<t, "maxLength", schema>, $>;
9
+ moreThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromStringSchema<t, "minLength", exclusivizeRangeSchema<schema>>, $>;
10
+ lessThanLength<const schema extends ExclusiveNumericRangeSchema>(schema: schema): Type<associateAttributesFromStringSchema<t, "maxLength", exclusivizeRangeSchema<schema>>, $>;
11
+ exactlyLength<const schema extends ExactLength.Schema>(schema: schema): Type<associateAttributesFromStringSchema<t, "exactLength", schema>, $>;
12
12
  }
13
13
  export type { Type as StringType };
package/out/scope.d.ts CHANGED
@@ -6,7 +6,7 @@ import type { Ark, type } from "./keywords/keywords.ts";
6
6
  import type { BoundModule, Module, Submodule, instantiateExport } from "./module.ts";
7
7
  import type { DefAst, InferredAst } from "./parser/ast/infer.ts";
8
8
  import { type inferDefinition, type validateDefinition } from "./parser/definition.ts";
9
- import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
9
+ import { InternalTypeParser, type DeclarationParser, type DefinitionParser, type EnumeratedTypeParser, type InstanceOfTypeParser, type SchemaParser, type TypeParser, type UnitTypeParser } from "./type.ts";
10
10
  /** The convenience properties attached to `scope` */
11
11
  export type ScopeParserAttachments = Omit<ScopeParser, never>;
12
12
  export interface ScopeParser {
@@ -74,6 +74,7 @@ export declare class InternalScope<$ extends {} = {}> extends BaseScope<$> {
74
74
  parseString(def: string, ctx: BaseParseContext): BaseRoot;
75
75
  unit: UnitTypeParser<$>;
76
76
  enumerated: EnumeratedTypeParser<$>;
77
+ instanceOf: InstanceOfTypeParser<$>;
77
78
  type: InternalTypeParser;
78
79
  declare: () => {
79
80
  type: InternalTypeParser;
package/out/scope.js CHANGED
@@ -92,6 +92,7 @@ export class InternalScope extends BaseScope {
92
92
  }
93
93
  unit = value => this.units([value]);
94
94
  enumerated = (...values) => this.units(values);
95
+ instanceOf = ctor => this.node("proto", { proto: ctor }, { prereduced: true });
95
96
  type = new InternalTypeParser(this);
96
97
  declare = () => ({
97
98
  type: this.type
package/out/type.d.ts CHANGED
@@ -42,6 +42,11 @@ export interface TypeParser<$ = {}> extends Ark.boundTypeAttachments<$> {
42
42
  * @example const argsForm = type('===', 'foo', 'bar', obj)
43
43
  */
44
44
  enumerated: EnumeratedTypeParser<$>;
45
+ /**
46
+ * Create a `Type` that is satisfied only by a value of a specific class.
47
+ * @example const array = type.instanceOf(Array)
48
+ */
49
+ instanceOf: InstanceOfTypeParser<$>;
45
50
  }
46
51
  export declare class InternalTypeParser extends Callable<(...args: unknown[]) => BaseRoot | Generic, TypeParserAttachments> {
47
52
  constructor($: InternalScope);
@@ -50,6 +55,7 @@ export type DeclarationParser<$> = <preinferred>() => {
50
55
  type: <const def>(def: validateDeclared<preinferred, def, $, bindThis<def>>) => Type<preinferred, $>;
51
56
  };
52
57
  export type UnitTypeParser<$> = <const t>(value: t) => Type<t, $>;
58
+ export type InstanceOfTypeParser<$> = <const t extends object>(ctor: Constructor<t>) => Type<t, $>;
53
59
  export type EnumeratedTypeParser<$> = <const values extends readonly unknown[]>(...values: values) => Type<values[number], $>;
54
60
  export type DefinitionParser<$> = <const def>(def: type.validate<def, $>) => def;
55
61
  export type SchemaParser<$> = (schema: RootSchema, opts?: BaseParseOptions) => Type<unknown, $>;
package/out/type.js CHANGED
@@ -15,7 +15,8 @@ export class InternalTypeParser extends Callable {
15
15
  // this won't be defined during bootstrapping, but externally always will be
16
16
  keywords: $.ambient,
17
17
  unit: $.unit,
18
- enumerated: $.enumerated
18
+ enumerated: $.enumerated,
19
+ instanceOf: $.instanceOf
19
20
  },
20
21
  // also won't be defined during bootstrapping
21
22
  $.ambientAttachments);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "arktype",
3
3
  "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
- "version": "2.0.0-rc.16",
4
+ "version": "2.0.0-rc.18",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "David Blass",
@@ -34,8 +34,8 @@
34
34
  "out"
35
35
  ],
36
36
  "dependencies": {
37
- "@ark/util": "0.18.0",
38
- "@ark/schema": "0.18.0"
37
+ "@ark/util": "0.19.0",
38
+ "@ark/schema": "0.20.0"
39
39
  },
40
40
  "publishConfig": {
41
41
  "access": "public"