arktype 2.0.0-dev.7 → 2.0.0-dev.9
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/__tests__/array.test.ts +23 -25
- package/__tests__/config.test.ts +19 -8
- package/__tests__/examples.test.ts +3 -3
- package/__tests__/imports.test.ts +18 -12
- package/__tests__/instanceof.test.ts +7 -7
- package/__tests__/intersection.test.ts +2 -2
- package/__tests__/keyTraversal.test.ts +15 -15
- package/__tests__/keywords.test.ts +50 -37
- package/__tests__/literal.test.ts +30 -14
- package/__tests__/match.bench.ts +6 -6
- package/__tests__/narrow.test.ts +18 -24
- package/__tests__/objectLiteral.test.ts +134 -11
- package/__tests__/operator.bench.ts +4 -4
- package/__tests__/{morph.test.ts → pipe.test.ts} +136 -75
- package/__tests__/scope.test.ts +202 -15
- package/__tests__/traverse.test.ts +45 -36
- package/__tests__/type.test.ts +8 -10
- package/ark.ts +14 -11
- package/generic.ts +8 -6
- package/match.ts +59 -70
- package/out/ark.d.ts +5 -5
- package/out/ark.js +4 -5
- package/out/ark.js.map +1 -1
- package/out/generic.d.ts +0 -1
- package/out/generic.js +8 -7
- package/out/generic.js.map +1 -1
- package/out/main.d.ts +0 -1
- package/out/main.js +0 -1
- package/out/match.d.ts +0 -1
- package/out/match.js +50 -57
- package/out/match.js.map +1 -1
- package/out/module.d.ts +0 -1
- package/out/module.js +0 -1
- package/out/parser/definition.d.ts +1 -2
- package/out/parser/definition.js +0 -1
- package/out/parser/generic.d.ts +5 -6
- package/out/parser/generic.js +4 -6
- package/out/parser/generic.js.map +1 -1
- package/out/parser/objectLiteral.d.ts +1 -2
- package/out/parser/objectLiteral.js +17 -11
- package/out/parser/objectLiteral.js.map +1 -1
- package/out/parser/semantic/bounds.d.ts +1 -2
- package/out/parser/semantic/bounds.js +0 -1
- package/out/parser/semantic/divisor.d.ts +0 -1
- package/out/parser/semantic/divisor.js +0 -2
- package/out/parser/semantic/infer.d.ts +0 -1
- package/out/parser/semantic/infer.js +0 -2
- package/out/parser/semantic/utils.d.ts +0 -1
- package/out/parser/semantic/utils.js +0 -2
- package/out/parser/semantic/validate.d.ts +4 -5
- package/out/parser/semantic/validate.js +0 -1
- package/out/parser/semantic/validate.js.map +1 -1
- package/out/parser/string/reduce/dynamic.d.ts +0 -1
- package/out/parser/string/reduce/dynamic.js +4 -9
- package/out/parser/string/reduce/dynamic.js.map +1 -1
- package/out/parser/string/reduce/shared.d.ts +5 -18
- package/out/parser/string/reduce/shared.js +0 -1
- package/out/parser/string/reduce/static.d.ts +0 -1
- package/out/parser/string/reduce/static.js +0 -2
- package/out/parser/string/shift/operand/date.d.ts +2 -3
- package/out/parser/string/shift/operand/date.js +2 -5
- package/out/parser/string/shift/operand/date.js.map +1 -1
- package/out/parser/string/shift/operand/enclosed.d.ts +2 -17
- package/out/parser/string/shift/operand/enclosed.js +6 -9
- package/out/parser/string/shift/operand/enclosed.js.map +1 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts +5 -6
- package/out/parser/string/shift/operand/genericArgs.js +4 -6
- package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
- package/out/parser/string/shift/operand/operand.d.ts +0 -1
- package/out/parser/string/shift/operand/operand.js +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts +6 -7
- package/out/parser/string/shift/operand/unenclosed.js +6 -13
- package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
- package/out/parser/string/shift/operator/bounds.d.ts +1 -8
- package/out/parser/string/shift/operator/bounds.js +4 -9
- package/out/parser/string/shift/operator/bounds.js.map +1 -1
- package/out/parser/string/shift/operator/divisor.d.ts +1 -2
- package/out/parser/string/shift/operator/divisor.js +1 -3
- package/out/parser/string/shift/operator/divisor.js.map +1 -1
- package/out/parser/string/shift/operator/operator.d.ts +1 -2
- package/out/parser/string/shift/operator/operator.js +0 -1
- package/out/parser/string/shift/scanner.d.ts +0 -1
- package/out/parser/string/shift/scanner.js +2 -5
- package/out/parser/string/shift/scanner.js.map +1 -1
- package/out/parser/string/string.d.ts +0 -1
- package/out/parser/string/string.js +1 -3
- package/out/parser/string/string.js.map +1 -1
- package/out/parser/tuple.d.ts +1 -2
- package/out/parser/tuple.js +12 -16
- package/out/parser/tuple.js.map +1 -1
- package/out/scope.d.ts +12 -12
- package/out/scope.js +16 -38
- package/out/scope.js.map +1 -1
- package/out/type.d.ts +38 -19
- package/out/type.js +32 -2
- package/out/type.js.map +1 -1
- package/package.json +6 -4
- package/parser/generic.ts +13 -14
- package/parser/objectLiteral.ts +25 -11
- package/parser/semantic/validate.ts +2 -3
- package/parser/string/reduce/dynamic.ts +7 -10
- package/parser/string/shift/operand/date.ts +3 -6
- package/parser/string/shift/operand/enclosed.ts +8 -8
- package/parser/string/shift/operand/genericArgs.ts +8 -8
- package/parser/string/shift/operand/unenclosed.ts +15 -25
- package/parser/string/shift/operator/bounds.ts +7 -9
- package/parser/string/shift/operator/divisor.ts +2 -3
- package/parser/string/shift/scanner.ts +2 -4
- package/parser/string/string.ts +2 -3
- package/parser/tuple.ts +15 -16
- package/scope.ts +39 -57
- package/type.ts +151 -35
- package/__tests__/cyclic.test.ts +0 -148
- package/out/ark.d.ts.map +0 -1
- package/out/base.d.ts +0 -205
- package/out/base.d.ts.map +0 -1
- package/out/base.js +0 -320
- package/out/base.js.map +0 -1
- package/out/builtins/builtins.d.ts +0 -23
- package/out/builtins/builtins.d.ts.map +0 -1
- package/out/builtins/builtins.js +0 -15
- package/out/builtins/builtins.js.map +0 -1
- package/out/builtins/jsObjects.d.ts +0 -19
- package/out/builtins/jsObjects.d.ts.map +0 -1
- package/out/builtins/jsObjects.js +0 -18
- package/out/builtins/jsObjects.js.map +0 -1
- package/out/builtins/parsing.d.ts +0 -16
- package/out/builtins/parsing.d.ts.map +0 -1
- package/out/builtins/parsing.js +0 -62
- package/out/builtins/parsing.js.map +0 -1
- package/out/builtins/tsGenerics.d.ts +0 -2
- package/out/builtins/tsGenerics.d.ts.map +0 -1
- package/out/builtins/tsGenerics.js +0 -26
- package/out/builtins/tsGenerics.js.map +0 -1
- package/out/builtins/tsKeywords.d.ts +0 -23
- package/out/builtins/tsKeywords.d.ts.map +0 -1
- package/out/builtins/tsKeywords.js +0 -23
- package/out/builtins/tsKeywords.js.map +0 -1
- package/out/builtins/utils/creditCard.d.ts +0 -3
- package/out/builtins/utils/creditCard.d.ts.map +0 -1
- package/out/builtins/utils/creditCard.js +0 -35
- package/out/builtins/utils/creditCard.js.map +0 -1
- package/out/builtins/utils/date.d.ts +0 -41
- package/out/builtins/utils/date.d.ts.map +0 -1
- package/out/builtins/utils/date.js +0 -56
- package/out/builtins/utils/date.js.map +0 -1
- package/out/builtins/validation.d.ts +0 -19
- package/out/builtins/validation.d.ts.map +0 -1
- package/out/builtins/validation.js +0 -74
- package/out/builtins/validation.js.map +0 -1
- package/out/config.d.ts +0 -9
- package/out/config.d.ts.map +0 -1
- package/out/config.js +0 -18
- package/out/config.js.map +0 -1
- package/out/constraints/ast.d.ts +0 -287
- package/out/constraints/ast.d.ts.map +0 -1
- package/out/constraints/ast.js +0 -2
- package/out/constraints/ast.js.map +0 -1
- package/out/constraints/constraint.d.ts +0 -200
- package/out/constraints/constraint.d.ts.map +0 -1
- package/out/constraints/constraint.js +0 -25
- package/out/constraints/constraint.js.map +0 -1
- package/out/constraints/predicate.d.ts +0 -64
- package/out/constraints/predicate.d.ts.map +0 -1
- package/out/constraints/predicate.js +0 -52
- package/out/constraints/predicate.js.map +0 -1
- package/out/constraints/props/index.d.ts +0 -42
- package/out/constraints/props/index.d.ts.map +0 -1
- package/out/constraints/props/index.js +0 -56
- package/out/constraints/props/index.js.map +0 -1
- package/out/constraints/props/optional.d.ts +0 -37
- package/out/constraints/props/optional.d.ts.map +0 -1
- package/out/constraints/props/optional.js +0 -56
- package/out/constraints/props/optional.js.map +0 -1
- package/out/constraints/props/prop.d.ts +0 -49
- package/out/constraints/props/prop.d.ts.map +0 -1
- package/out/constraints/props/prop.js +0 -106
- package/out/constraints/props/prop.js.map +0 -1
- package/out/constraints/props/props.d.ts +0 -37
- package/out/constraints/props/props.d.ts.map +0 -1
- package/out/constraints/props/props.js +0 -121
- package/out/constraints/props/props.js.map +0 -1
- package/out/constraints/props/required.d.ts +0 -46
- package/out/constraints/props/required.d.ts.map +0 -1
- package/out/constraints/props/required.js +0 -81
- package/out/constraints/props/required.js.map +0 -1
- package/out/constraints/props/sequence.d.ts +0 -82
- package/out/constraints/props/sequence.d.ts.map +0 -1
- package/out/constraints/props/sequence.js +0 -376
- package/out/constraints/props/sequence.js.map +0 -1
- package/out/constraints/props/shared.d.ts +0 -3
- package/out/constraints/props/shared.d.ts.map +0 -1
- package/out/constraints/props/shared.js +0 -4
- package/out/constraints/props/shared.js.map +0 -1
- package/out/constraints/refinements/after.d.ts +0 -33
- package/out/constraints/refinements/after.d.ts.map +0 -1
- package/out/constraints/refinements/after.js +0 -36
- package/out/constraints/refinements/after.js.map +0 -1
- package/out/constraints/refinements/before.d.ts +0 -33
- package/out/constraints/refinements/before.d.ts.map +0 -1
- package/out/constraints/refinements/before.js +0 -43
- package/out/constraints/refinements/before.js.map +0 -1
- package/out/constraints/refinements/divisor.d.ts +0 -52
- package/out/constraints/refinements/divisor.d.ts.map +0 -1
- package/out/constraints/refinements/divisor.js +0 -52
- package/out/constraints/refinements/divisor.js.map +0 -1
- package/out/constraints/refinements/exactLength.d.ts +0 -45
- package/out/constraints/refinements/exactLength.d.ts.map +0 -1
- package/out/constraints/refinements/exactLength.js +0 -52
- package/out/constraints/refinements/exactLength.js.map +0 -1
- package/out/constraints/refinements/kinds.d.ts +0 -29
- package/out/constraints/refinements/kinds.d.ts.map +0 -1
- package/out/constraints/refinements/kinds.js +0 -17
- package/out/constraints/refinements/kinds.js.map +0 -1
- package/out/constraints/refinements/length.d.ts +0 -48
- package/out/constraints/refinements/length.d.ts.map +0 -1
- package/out/constraints/refinements/length.js +0 -53
- package/out/constraints/refinements/length.js.map +0 -1
- package/out/constraints/refinements/max.d.ts +0 -31
- package/out/constraints/refinements/max.d.ts.map +0 -1
- package/out/constraints/refinements/max.js +0 -35
- package/out/constraints/refinements/max.js.map +0 -1
- package/out/constraints/refinements/maxLength.d.ts +0 -32
- package/out/constraints/refinements/maxLength.d.ts.map +0 -1
- package/out/constraints/refinements/maxLength.js +0 -36
- package/out/constraints/refinements/maxLength.js.map +0 -1
- package/out/constraints/refinements/min.d.ts +0 -31
- package/out/constraints/refinements/min.d.ts.map +0 -1
- package/out/constraints/refinements/min.js +0 -28
- package/out/constraints/refinements/min.js.map +0 -1
- package/out/constraints/refinements/minLength.d.ts +0 -32
- package/out/constraints/refinements/minLength.d.ts.map +0 -1
- package/out/constraints/refinements/minLength.js +0 -33
- package/out/constraints/refinements/minLength.js.map +0 -1
- package/out/constraints/refinements/range.d.ts +0 -119
- package/out/constraints/refinements/range.d.ts.map +0 -1
- package/out/constraints/refinements/range.js +0 -89
- package/out/constraints/refinements/range.js.map +0 -1
- package/out/constraints/refinements/regex.d.ts +0 -47
- package/out/constraints/refinements/regex.d.ts.map +0 -1
- package/out/constraints/refinements/regex.js +0 -37
- package/out/constraints/refinements/regex.js.map +0 -1
- package/out/constraints/refinements/shared.d.ts +0 -20
- package/out/constraints/refinements/shared.d.ts.map +0 -1
- package/out/constraints/refinements/shared.js +0 -10
- package/out/constraints/refinements/shared.js.map +0 -1
- package/out/generic.d.ts.map +0 -1
- package/out/inference.d.ts +0 -137
- package/out/inference.d.ts.map +0 -1
- package/out/inference.js +0 -73
- package/out/inference.js.map +0 -1
- package/out/keywords/ark.d.ts +0 -70
- package/out/keywords/ark.d.ts.map +0 -1
- package/out/keywords/ark.js +0 -29
- package/out/keywords/ark.js.map +0 -1
- package/out/keywords/internal.d.ts +0 -13
- package/out/keywords/internal.d.ts.map +0 -1
- package/out/keywords/internal.js +0 -13
- package/out/keywords/internal.js.map +0 -1
- package/out/keywords/jsObject.d.ts +0 -20
- package/out/keywords/jsObject.d.ts.map +0 -1
- package/out/keywords/jsObject.js +0 -18
- package/out/keywords/jsObject.js.map +0 -1
- package/out/keywords/parsing.d.ts +0 -17
- package/out/keywords/parsing.d.ts.map +0 -1
- package/out/keywords/parsing.js +0 -64
- package/out/keywords/parsing.js.map +0 -1
- package/out/keywords/root.d.ts +0 -7
- package/out/keywords/root.d.ts.map +0 -1
- package/out/keywords/root.js +0 -4
- package/out/keywords/root.js.map +0 -1
- package/out/keywords/tsGenerics.d.ts +0 -2
- package/out/keywords/tsGenerics.d.ts.map +0 -1
- package/out/keywords/tsGenerics.js +0 -26
- package/out/keywords/tsGenerics.js.map +0 -1
- package/out/keywords/tsPrimitive.d.ts +0 -24
- package/out/keywords/tsPrimitive.d.ts.map +0 -1
- package/out/keywords/tsPrimitive.js +0 -25
- package/out/keywords/tsPrimitive.js.map +0 -1
- package/out/keywords/utils/creditCard.d.ts +0 -11
- package/out/keywords/utils/creditCard.d.ts.map +0 -1
- package/out/keywords/utils/creditCard.js +0 -35
- package/out/keywords/utils/creditCard.js.map +0 -1
- package/out/keywords/utils/date.d.ts +0 -45
- package/out/keywords/utils/date.d.ts.map +0 -1
- package/out/keywords/utils/date.js +0 -56
- package/out/keywords/utils/date.js.map +0 -1
- package/out/keywords/validation.d.ts +0 -20
- package/out/keywords/validation.d.ts.map +0 -1
- package/out/keywords/validation.js +0 -80
- package/out/keywords/validation.js.map +0 -1
- package/out/kinds.d.ts +0 -102
- package/out/kinds.d.ts.map +0 -1
- package/out/kinds.js +0 -29
- package/out/kinds.js.map +0 -1
- package/out/main.d.ts.map +0 -1
- package/out/match.d.ts.map +0 -1
- package/out/module.d.ts.map +0 -1
- package/out/parse.d.ts +0 -29
- package/out/parse.d.ts.map +0 -1
- package/out/parse.js +0 -161
- package/out/parse.js.map +0 -1
- package/out/parser/definition.d.ts.map +0 -1
- package/out/parser/generic.d.ts.map +0 -1
- package/out/parser/objectLiteral.d.ts.map +0 -1
- package/out/parser/semantic/bounds.d.ts.map +0 -1
- package/out/parser/semantic/divisor.d.ts.map +0 -1
- package/out/parser/semantic/infer.d.ts.map +0 -1
- package/out/parser/semantic/utils.d.ts.map +0 -1
- package/out/parser/semantic/validate.d.ts.map +0 -1
- package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
- package/out/parser/string/reduce/shared.d.ts.map +0 -1
- package/out/parser/string/reduce/static.d.ts.map +0 -1
- package/out/parser/string/shift/operand/date.d.ts.map +0 -1
- package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
- package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
- package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
- package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
- package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
- package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
- package/out/parser/string/shift/scanner.d.ts.map +0 -1
- package/out/parser/string/string.d.ts.map +0 -1
- package/out/parser/tuple.d.ts.map +0 -1
- package/out/schema.d.ts +0 -175
- package/out/schema.d.ts.map +0 -1
- package/out/schema.js +0 -73
- package/out/schema.js.map +0 -1
- package/out/schemaScope.d.ts +0 -180
- package/out/schemaScope.d.ts.map +0 -1
- package/out/schemaScope.js +0 -235
- package/out/schemaScope.js.map +0 -1
- package/out/scope.d.ts.map +0 -1
- package/out/shared/compile.d.ts +0 -22
- package/out/shared/compile.d.ts.map +0 -1
- package/out/shared/compile.js +0 -79
- package/out/shared/compile.js.map +0 -1
- package/out/shared/context.d.ts +0 -54
- package/out/shared/context.d.ts.map +0 -1
- package/out/shared/context.js +0 -53
- package/out/shared/context.js.map +0 -1
- package/out/shared/declare.d.ts +0 -69
- package/out/shared/declare.d.ts.map +0 -1
- package/out/shared/declare.js +0 -2
- package/out/shared/declare.js.map +0 -1
- package/out/shared/disjoint.d.ts +0 -91
- package/out/shared/disjoint.d.ts.map +0 -1
- package/out/shared/disjoint.js +0 -110
- package/out/shared/disjoint.js.map +0 -1
- package/out/shared/errors.d.ts +0 -99
- package/out/shared/errors.d.ts.map +0 -1
- package/out/shared/errors.js +0 -108
- package/out/shared/errors.js.map +0 -1
- package/out/shared/implement.d.ts +0 -363
- package/out/shared/implement.d.ts.map +0 -1
- package/out/shared/implement.js +0 -68
- package/out/shared/implement.js.map +0 -1
- package/out/shared/inference.d.ts +0 -6
- package/out/shared/inference.d.ts.map +0 -1
- package/out/shared/inference.js +0 -5
- package/out/shared/inference.js.map +0 -1
- package/out/shared/intersections.d.ts +0 -60
- package/out/shared/intersections.d.ts.map +0 -1
- package/out/shared/intersections.js +0 -2
- package/out/shared/intersections.js.map +0 -1
- package/out/shared/traversal.d.ts +0 -62
- package/out/shared/traversal.d.ts.map +0 -1
- package/out/shared/traversal.js +0 -97
- package/out/shared/traversal.js.map +0 -1
- package/out/shared/utils.d.ts +0 -24
- package/out/shared/utils.d.ts.map +0 -1
- package/out/shared/utils.js +0 -15
- package/out/shared/utils.js.map +0 -1
- package/out/type.d.ts.map +0 -1
- package/out/types/basis.d.ts +0 -47
- package/out/types/basis.d.ts.map +0 -1
- package/out/types/basis.js +0 -15
- package/out/types/basis.js.map +0 -1
- package/out/types/discriminate.d.ts +0 -34
- package/out/types/discriminate.d.ts.map +0 -1
- package/out/types/discriminate.js +0 -120
- package/out/types/discriminate.js.map +0 -1
- package/out/types/domain.d.ts +0 -57
- package/out/types/domain.d.ts.map +0 -1
- package/out/types/domain.js +0 -41
- package/out/types/domain.js.map +0 -1
- package/out/types/intersection.d.ts +0 -127
- package/out/types/intersection.d.ts.map +0 -1
- package/out/types/intersection.js +0 -379
- package/out/types/intersection.js.map +0 -1
- package/out/types/morph.d.ts +0 -157
- package/out/types/morph.d.ts.map +0 -1
- package/out/types/morph.js +0 -99
- package/out/types/morph.js.map +0 -1
- package/out/types/proto.d.ts +0 -51
- package/out/types/proto.d.ts.map +0 -1
- package/out/types/proto.js +0 -60
- package/out/types/proto.js.map +0 -1
- package/out/types/type.d.ts +0 -143
- package/out/types/type.d.ts.map +0 -1
- package/out/types/type.js +0 -123
- package/out/types/type.js.map +0 -1
- package/out/types/union.d.ts +0 -71
- package/out/types/union.d.ts.map +0 -1
- package/out/types/union.js +0 -379
- package/out/types/union.js.map +0 -1
- package/out/types/unit.d.ts +0 -45
- package/out/types/unit.d.ts.map +0 -1
- package/out/types/unit.js +0 -56
- package/out/types/unit.js.map +0 -1
- package/out/util.d.ts +0 -31
- package/out/util.d.ts.map +0 -1
- package/out/util.js +0 -8
- package/out/util.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
-
import {
|
|
2
|
+
import { printable, registeredReference } from "@arktype/util"
|
|
3
3
|
import { type } from "arktype"
|
|
4
4
|
|
|
5
5
|
contextualize(
|
|
@@ -11,28 +11,44 @@ contextualize(
|
|
|
11
11
|
attest(t.json).equals(type("5").json)
|
|
12
12
|
})
|
|
13
13
|
|
|
14
|
-
it("
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
it("symbol with description", () => {
|
|
15
|
+
// if another symbol with the description "ism" is used in a test,
|
|
16
|
+
// the snapshotted errors below could break, but I want to test the
|
|
17
|
+
// actual display format.
|
|
18
|
+
// if you're here for that reason, choose another symbol name :P
|
|
19
|
+
const ism = Symbol("ism")
|
|
20
|
+
const t = type(["===", ism])
|
|
21
|
+
attest(t(ism)).equals(ism)
|
|
22
|
+
|
|
23
|
+
// same with ick- don't use it
|
|
24
|
+
const ick = Symbol("ick")
|
|
25
|
+
|
|
26
|
+
attest(t(ick).toString()).snap("must be Symbol(ism) (was Symbol(ick))")
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("anonymous symbol", () => {
|
|
30
|
+
const anon = Symbol()
|
|
31
|
+
// An anonymous symbol will definitely have a suffix so we need to
|
|
32
|
+
// get the name ahead of time
|
|
33
|
+
const anonName = printable(anon)
|
|
34
|
+
const t = type(["===", anon])
|
|
17
35
|
attest<symbol>(t.infer)
|
|
18
|
-
attest(t(
|
|
19
|
-
attest(t("test").
|
|
20
|
-
'must be (symbol anonymous) (was "test")'
|
|
21
|
-
)
|
|
36
|
+
attest(t(anon)).equals(anon)
|
|
37
|
+
attest(t("test").toString()).equals(`must be ${anonName} (was "test")`)
|
|
22
38
|
})
|
|
23
39
|
|
|
24
40
|
it("branches", () => {
|
|
25
41
|
const o = { ark: true }
|
|
26
|
-
const oReference =
|
|
42
|
+
const oReference = registeredReference(o)
|
|
27
43
|
const s = Symbol()
|
|
28
|
-
const sReference =
|
|
44
|
+
const sReference = registeredReference(s)
|
|
29
45
|
const t = type(["===", true, "foo", 5, 1n, null, undefined, o, s])
|
|
30
46
|
attest<
|
|
31
47
|
true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
|
|
32
48
|
>(t.infer)
|
|
33
49
|
attest(t.json).equals([
|
|
34
|
-
{ unit: sReference },
|
|
35
50
|
{ unit: oReference },
|
|
51
|
+
{ unit: sReference },
|
|
36
52
|
{ unit: "1n" },
|
|
37
53
|
{ unit: "foo" },
|
|
38
54
|
{ unit: "undefined" },
|
|
@@ -52,16 +68,16 @@ contextualize(
|
|
|
52
68
|
|
|
53
69
|
it("branches", () => {
|
|
54
70
|
const o = { ark: true }
|
|
55
|
-
const oReference =
|
|
71
|
+
const oReference = registeredReference(o)
|
|
56
72
|
const s = Symbol()
|
|
57
|
-
const sReference =
|
|
73
|
+
const sReference = registeredReference(s)
|
|
58
74
|
const t = type("===", "foo", 5, true, null, 1n, undefined, o, s)
|
|
59
75
|
attest<
|
|
60
76
|
true | "foo" | 5 | 1n | null | undefined | { ark: boolean } | typeof s
|
|
61
77
|
>(t.infer)
|
|
62
78
|
attest(t.json).equals([
|
|
63
|
-
{ unit: sReference },
|
|
64
79
|
{ unit: oReference },
|
|
80
|
+
{ unit: sReference },
|
|
65
81
|
{ unit: "1n" },
|
|
66
82
|
{ unit: "foo" },
|
|
67
83
|
{ unit: "undefined" },
|
package/__tests__/match.bench.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { match } from "arktype"
|
|
|
3
3
|
|
|
4
4
|
bench("general matchers", () => {
|
|
5
5
|
const matcher = match()
|
|
6
|
-
.when("string",
|
|
7
|
-
.when("number",
|
|
8
|
-
.when("boolean",
|
|
6
|
+
.when("string", s => s)
|
|
7
|
+
.when("number", n => n)
|
|
8
|
+
.when("boolean", b => b)
|
|
9
9
|
.orThrow()
|
|
10
10
|
|
|
11
11
|
const a = matcher("abc")
|
|
@@ -18,9 +18,9 @@ bench("general matchers", () => {
|
|
|
18
18
|
bench("match.only<T>", () => {
|
|
19
19
|
const matcher = match
|
|
20
20
|
.only<string | number | boolean>()
|
|
21
|
-
.when("string",
|
|
22
|
-
.when("number",
|
|
23
|
-
.when("boolean",
|
|
21
|
+
.when("string", s => s)
|
|
22
|
+
.when("number", n => n)
|
|
23
|
+
.when("boolean", b => b)
|
|
24
24
|
.finalize()
|
|
25
25
|
|
|
26
26
|
const a = matcher("abc")
|
package/__tests__/narrow.test.ts
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
1
|
import { attest, contextualize } from "@arktype/attest"
|
|
2
2
|
import type { Narrowed, Out, of, string } from "@arktype/schema"
|
|
3
|
-
import { type equals
|
|
4
|
-
import { type
|
|
3
|
+
import { registeredReference, type equals } from "@arktype/util"
|
|
4
|
+
import { type, type Type } from "arktype"
|
|
5
5
|
|
|
6
6
|
contextualize(() => {
|
|
7
7
|
it("implicit problem", () => {
|
|
8
8
|
const isOdd = (n: number) => n % 2 === 1
|
|
9
|
-
const isOddRef =
|
|
9
|
+
const isOddRef = registeredReference(isOdd)
|
|
10
10
|
const odd = type(["number", ":", isOdd])
|
|
11
11
|
attest<number>(odd.infer)
|
|
12
12
|
attest(odd.json).equals({ domain: "number", predicate: [isOddRef] })
|
|
13
|
-
attest(odd(1)
|
|
14
|
-
attest(odd(2).
|
|
15
|
-
"must be valid according to isOdd (was 2)"
|
|
16
|
-
)
|
|
13
|
+
attest(odd(1)).equals(1)
|
|
14
|
+
attest(odd(2).toString()).snap("must be valid according to isOdd (was 2)")
|
|
17
15
|
})
|
|
18
16
|
|
|
19
17
|
it("implicit problem anonymous", () => {
|
|
20
|
-
const even = type("number", ":",
|
|
21
|
-
attest(even(1).
|
|
18
|
+
const even = type("number", ":", n => n % 2 === 0)
|
|
19
|
+
attest(even(1).toString()).snap(
|
|
22
20
|
"must be valid according to an anonymous predicate (was 1)"
|
|
23
21
|
)
|
|
24
22
|
})
|
|
@@ -27,9 +25,9 @@ contextualize(() => {
|
|
|
27
25
|
const even = type([
|
|
28
26
|
"number",
|
|
29
27
|
":",
|
|
30
|
-
(n, ctx) => n % 3 === 0 || ctx.invalid(
|
|
28
|
+
(n, ctx) => n % 3 === 0 || ctx.invalid("divisible by 3")
|
|
31
29
|
])
|
|
32
|
-
attest(even(1).
|
|
30
|
+
attest(even(1).toString()).snap("must be divisible by 3 (was 1)")
|
|
33
31
|
})
|
|
34
32
|
|
|
35
33
|
it("problem at path", () => {
|
|
@@ -40,16 +38,15 @@ contextualize(() => {
|
|
|
40
38
|
},
|
|
41
39
|
":",
|
|
42
40
|
({ a, b }, ctx) => {
|
|
43
|
-
if (a === b)
|
|
44
|
-
|
|
45
|
-
}
|
|
41
|
+
if (a === b) return true
|
|
42
|
+
|
|
46
43
|
ctx.error({ expected: "equal to b", path: ["a"] })
|
|
47
44
|
ctx.error({ expected: "equal to a", path: ["b"] })
|
|
48
45
|
return false
|
|
49
46
|
}
|
|
50
47
|
])
|
|
51
|
-
attest(abEqual({ a: 1, b: 1 })
|
|
52
|
-
attest(abEqual({ a: 1, b: 2 }).
|
|
48
|
+
attest(abEqual({ a: 1, b: 1 })).equals({ a: 1, b: 1 })
|
|
49
|
+
attest(abEqual({ a: 1, b: 2 }).toString()).snap(
|
|
53
50
|
'a must be equal to b (was {"a":1,"b":2})\nb must be equal to a (was {"a":1,"b":2})'
|
|
54
51
|
)
|
|
55
52
|
})
|
|
@@ -65,11 +62,8 @@ contextualize(() => {
|
|
|
65
62
|
t: equals<t, Expected> extends true ? t : Expected
|
|
66
63
|
) => true
|
|
67
64
|
attest<number | boolean[]>(
|
|
68
|
-
type([
|
|
69
|
-
|
|
70
|
-
":",
|
|
71
|
-
(data) => validateNumberOrBooleanList(data)
|
|
72
|
-
]).infer
|
|
65
|
+
type(["number|boolean[]", ":", data => validateNumberOrBooleanList(data)])
|
|
66
|
+
.infer
|
|
73
67
|
)
|
|
74
68
|
attest(() => {
|
|
75
69
|
// @ts-expect-error
|
|
@@ -85,15 +79,15 @@ contextualize(() => {
|
|
|
85
79
|
s === [...s].reverse().join("") ? true : ctx.invalid("a palindrome")
|
|
86
80
|
])
|
|
87
81
|
attest<Type<string.narrowed>>(palindrome)
|
|
88
|
-
attest(palindrome("dad")
|
|
89
|
-
attest(palindrome("david").
|
|
82
|
+
attest(palindrome("dad")).snap("dad")
|
|
83
|
+
attest(palindrome("david").toString()).snap(
|
|
90
84
|
'must be a palindrome (was "david")'
|
|
91
85
|
)
|
|
92
86
|
})
|
|
93
87
|
|
|
94
88
|
it("narrows the output type of a morph", () => {
|
|
95
89
|
const t = type("string")
|
|
96
|
-
.
|
|
90
|
+
.pipe(s => s.length)
|
|
97
91
|
.narrow((n): n is 5 => n === 5)
|
|
98
92
|
|
|
99
93
|
attest<Type<(In: string) => Out<of<5, Narrowed>>, {}>>(t)
|
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
writeUnboundableMessage,
|
|
5
5
|
writeUnresolvableMessage
|
|
6
6
|
} from "@arktype/schema"
|
|
7
|
-
import { printable,
|
|
7
|
+
import { printable, registeredReference } from "@arktype/util"
|
|
8
8
|
import { scope, type } from "arktype"
|
|
9
9
|
import { writeInvalidSpreadTypeMessage } from "../parser/objectLiteral.js"
|
|
10
10
|
|
|
@@ -42,7 +42,7 @@ contextualize(
|
|
|
42
42
|
|
|
43
43
|
it("symbol key", () => {
|
|
44
44
|
const s = Symbol()
|
|
45
|
-
const name =
|
|
45
|
+
const name = registeredReference(s)
|
|
46
46
|
const t = type({
|
|
47
47
|
[s]: "string"
|
|
48
48
|
})
|
|
@@ -89,19 +89,26 @@ contextualize(
|
|
|
89
89
|
attest<{ "a?": string }>(t.infer)
|
|
90
90
|
})
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
it("traverse optional", () => {
|
|
93
|
+
const o = type({ "a?": "string" })
|
|
94
|
+
attest(o({ a: "a" })).snap({ a: "a" })
|
|
95
|
+
attest(o({})).snap({})
|
|
96
|
+
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
// it("traverse strict optional", () => {
|
|
100
|
+
// // TODO: strict
|
|
101
|
+
// const o = type({ "a?": "string" })
|
|
102
|
+
// attest(o({ a: "a" })).snap({ a: "a" })
|
|
103
|
+
// attest(o({})).snap({})
|
|
104
|
+
// attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
98
105
|
// })
|
|
99
106
|
|
|
100
107
|
// it("multiple bad strict", () => {
|
|
101
108
|
// const t = type({ a: "string", b: "boolean" }).configure({
|
|
102
109
|
// keys: "strict"
|
|
103
110
|
// })
|
|
104
|
-
// attest(t({ a: 1, b: 2 }).
|
|
111
|
+
// attest(t({ a: 1, b: 2 }).toString()).snap(
|
|
105
112
|
// "a must be a string (was number)\nb must be boolean (was number)"
|
|
106
113
|
// )
|
|
107
114
|
// })
|
|
@@ -217,19 +224,75 @@ contextualize(
|
|
|
217
224
|
},
|
|
218
225
|
"index",
|
|
219
226
|
() => {
|
|
220
|
-
it("index", () => {
|
|
227
|
+
it("string index", () => {
|
|
221
228
|
const o = type({ "[string]": "string" })
|
|
222
229
|
attest<{ [x: string]: string }>(o.infer)
|
|
230
|
+
attest(o.json).snap({
|
|
231
|
+
domain: "object",
|
|
232
|
+
index: [{ key: "string", value: "string" }]
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
attest(o({})).equals({})
|
|
236
|
+
attest(o({ a: "a", b: "b" })).equals({ a: "a", b: "b" })
|
|
237
|
+
|
|
238
|
+
const validWithSymbol = { a: "a", [Symbol()]: null }
|
|
239
|
+
attest(validWithSymbol).equals(validWithSymbol)
|
|
240
|
+
|
|
241
|
+
attest(o({ a: 1 }).toString()).snap("a must be a string (was number)")
|
|
242
|
+
attest(o({ a: true, b: false }).toString())
|
|
243
|
+
.snap(`a must be a string (was true)
|
|
244
|
+
b must be a string (was false)`)
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
it("symbol index", () => {
|
|
248
|
+
const o = type({ "[symbol]": "1" })
|
|
249
|
+
attest<{ [x: symbol]: 1 }>(o.infer)
|
|
250
|
+
attest(o.json).snap({
|
|
251
|
+
domain: "object",
|
|
252
|
+
index: [{ key: "symbol", value: { unit: 1 } }]
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
attest(o({})).equals({})
|
|
256
|
+
|
|
257
|
+
attest(o({ a: 999 })).unknown.snap({ a: 999 })
|
|
258
|
+
|
|
259
|
+
const zildjian = Symbol()
|
|
260
|
+
const zildjianName = printable(zildjian)
|
|
261
|
+
|
|
262
|
+
// I've been dope, suspenseful with a pencil
|
|
263
|
+
// Ever since...
|
|
264
|
+
const prince = Symbol()
|
|
265
|
+
const princeName = printable(prince)
|
|
266
|
+
|
|
267
|
+
attest(o({ [zildjian]: 1, [prince]: 1 })).equals({
|
|
268
|
+
[zildjian]: 1,
|
|
269
|
+
[prince]: 1
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
attest({ a: 0, [zildjian]: 1 }).equals({ a: 0, [zildjian]: 1 })
|
|
273
|
+
|
|
274
|
+
attest(o({ [zildjian]: 0 }).toString()).equals(
|
|
275
|
+
`value at [${zildjianName}] must be 1 (was 0)`
|
|
276
|
+
)
|
|
277
|
+
attest(o({ [prince]: null, [zildjian]: undefined }).toString())
|
|
278
|
+
.snap(`value at [${princeName}] must be 1 (was null)
|
|
279
|
+
value at [${zildjianName}] must be 1 (was undefined)`)
|
|
223
280
|
})
|
|
224
281
|
|
|
225
282
|
it("enumerable indexed union", () => {
|
|
226
283
|
const o = type({ "['foo' | 'bar']": "string" })
|
|
227
|
-
|
|
284
|
+
const expected = type({ foo: "string", bar: "string" })
|
|
285
|
+
attest<typeof expected>(o)
|
|
286
|
+
attest(o.json).equals(expected.json)
|
|
228
287
|
})
|
|
229
288
|
|
|
230
289
|
it("non-enumerable indexed union", () => {
|
|
231
290
|
const o = type({ "[string | symbol]": "string" })
|
|
232
291
|
attest<{ [x: string]: string; [x: symbol]: string }>(o.infer)
|
|
292
|
+
attest(o.json).snap({
|
|
293
|
+
domain: "object",
|
|
294
|
+
index: [{ key: ["string", "symbol"], value: "string" }]
|
|
295
|
+
})
|
|
233
296
|
})
|
|
234
297
|
|
|
235
298
|
it("multiple indexed", () => {
|
|
@@ -238,6 +301,35 @@ contextualize(
|
|
|
238
301
|
"[symbol]": "number"
|
|
239
302
|
})
|
|
240
303
|
attest<{ [x: string]: string; [x: symbol]: number }>(o.infer)
|
|
304
|
+
attest(o.json).snap({
|
|
305
|
+
domain: "object",
|
|
306
|
+
index: [
|
|
307
|
+
{ key: "string", value: "string" },
|
|
308
|
+
{ key: "symbol", value: "number" }
|
|
309
|
+
]
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
attest(o({})).equals({})
|
|
313
|
+
attest(o({ foo: "f" })).equals({ foo: "f" })
|
|
314
|
+
|
|
315
|
+
const sym = Symbol()
|
|
316
|
+
|
|
317
|
+
const symName = printable(sym)
|
|
318
|
+
|
|
319
|
+
const validWithStringsAndSymbols = {
|
|
320
|
+
str: "string",
|
|
321
|
+
[sym]: 8675309
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
attest(o(validWithStringsAndSymbols)).equals(validWithStringsAndSymbols)
|
|
325
|
+
|
|
326
|
+
attest(
|
|
327
|
+
o({
|
|
328
|
+
str: 100,
|
|
329
|
+
[sym]: "💯"
|
|
330
|
+
}).toString()
|
|
331
|
+
).snap(`str must be a string (was number)
|
|
332
|
+
value at [${symName}] must be a number (was string)`)
|
|
241
333
|
})
|
|
242
334
|
|
|
243
335
|
it("all key kinds", () => {
|
|
@@ -249,6 +341,25 @@ contextualize(
|
|
|
249
341
|
attest<{ [x: string]: string; required: "foo"; optional?: "bar" }>(
|
|
250
342
|
o.infer
|
|
251
343
|
)
|
|
344
|
+
attest(o.json).snap({
|
|
345
|
+
domain: "object",
|
|
346
|
+
prop: [
|
|
347
|
+
{ key: "optional", optional: true, value: { unit: "bar" } },
|
|
348
|
+
{ key: "required", value: { unit: "foo" } }
|
|
349
|
+
],
|
|
350
|
+
index: [{ key: "string", value: "string" }]
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
const valid: typeof o.infer = { required: "foo", other: "bar" }
|
|
354
|
+
attest(o(valid)).equals(valid)
|
|
355
|
+
attest(
|
|
356
|
+
o({
|
|
357
|
+
optional: "wrongString",
|
|
358
|
+
other: 0n
|
|
359
|
+
}).toString()
|
|
360
|
+
).snap(`optional must be "bar" (was "wrongString")
|
|
361
|
+
required must be "foo" (was missing)
|
|
362
|
+
other must be a string (was bigint)`)
|
|
252
363
|
})
|
|
253
364
|
|
|
254
365
|
it("index key from scope", () => {
|
|
@@ -261,6 +372,14 @@ contextualize(
|
|
|
261
372
|
type Key = symbol | "foo" | "bar" | "baz"
|
|
262
373
|
attest<Key>(types.key.infer)
|
|
263
374
|
attest<Record<Key, string>>(types.obj.infer)
|
|
375
|
+
|
|
376
|
+
const expected = type({ "[symbol]": "string" }).and({
|
|
377
|
+
foo: "string",
|
|
378
|
+
bar: "string",
|
|
379
|
+
baz: "string"
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
attest(types.obj.json).snap(expected.json)
|
|
264
383
|
})
|
|
265
384
|
|
|
266
385
|
it("syntax error in index definition", () => {
|
|
@@ -314,6 +433,10 @@ contextualize(
|
|
|
314
433
|
it("escaped index", () => {
|
|
315
434
|
const o = type({ "\\[string]": "string" })
|
|
316
435
|
attest<{ "[string]": string }>(o.infer)
|
|
436
|
+
attest(o.json).snap({
|
|
437
|
+
domain: "object",
|
|
438
|
+
prop: [{ key: "[string]", value: "string" }]
|
|
439
|
+
})
|
|
317
440
|
})
|
|
318
441
|
}
|
|
319
442
|
)
|
|
@@ -106,25 +106,25 @@ bench("divisor", () => {
|
|
|
106
106
|
.types([1023, "instantiations"])
|
|
107
107
|
|
|
108
108
|
bench("filter-tuple", () => {
|
|
109
|
-
const _ = type(["boolean", ":",
|
|
109
|
+
const _ = type(["boolean", ":", b => b])
|
|
110
110
|
})
|
|
111
111
|
.median([8.04, "us"])
|
|
112
112
|
.types([583, "instantiations"])
|
|
113
113
|
|
|
114
114
|
bench("filter-chain", () => {
|
|
115
|
-
const _ = type("boolean").narrow(
|
|
115
|
+
const _ = type("boolean").narrow(b => b)
|
|
116
116
|
})
|
|
117
117
|
.median([12.19, "us"])
|
|
118
118
|
.types([42, "instantiations"])
|
|
119
119
|
|
|
120
120
|
bench("morph-tuple", () => {
|
|
121
|
-
const _ = type(["boolean", "=>",
|
|
121
|
+
const _ = type(["boolean", "=>", b => b])
|
|
122
122
|
})
|
|
123
123
|
.median([8.96, "us"])
|
|
124
124
|
.types([562, "instantiations"])
|
|
125
125
|
|
|
126
126
|
bench("morph-chain", () => {
|
|
127
|
-
const _ = type("boolean").
|
|
127
|
+
const _ = type("boolean").pipe(b => b)
|
|
128
128
|
})
|
|
129
129
|
.median([2.98, "us"])
|
|
130
130
|
.types([26, "instantiations"])
|