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,110 +1,172 @@
|
|
|
1
1
|
import { attest, contextualize } from "@arktype/attest"
|
|
2
|
-
import { type Out
|
|
3
|
-
import {
|
|
2
|
+
import { assertNodeKind, type Out } from "@arktype/schema"
|
|
3
|
+
import { scope, type, type Type } from "arktype"
|
|
4
4
|
|
|
5
5
|
contextualize(() => {
|
|
6
6
|
it("base", () => {
|
|
7
|
-
const t = type("number").
|
|
7
|
+
const t = type("number").pipe(data => `${data}`)
|
|
8
8
|
attest<Type<(In: number) => Out<string>>>(t)
|
|
9
9
|
attest<string>(t.infer)
|
|
10
10
|
attest<number>(t.in.infer)
|
|
11
|
-
const
|
|
12
|
-
attest<string |
|
|
13
|
-
const
|
|
14
|
-
attest(
|
|
11
|
+
const out = t(5)
|
|
12
|
+
attest<string | type.errors>(out).equals("5")
|
|
13
|
+
const result = t("foo")
|
|
14
|
+
attest(result.toString()).snap("must be a number (was string)")
|
|
15
15
|
})
|
|
16
16
|
|
|
17
17
|
it("within type", () => {
|
|
18
|
-
const t = type(["boolean", "=>",
|
|
18
|
+
const t = type(["boolean", "=>", data => !data])
|
|
19
19
|
attest<Type<(In: boolean) => Out<boolean>>>(t)
|
|
20
|
-
const
|
|
21
|
-
attest<boolean |
|
|
20
|
+
const out = t(true)
|
|
21
|
+
attest<boolean | type.errors>(out).equals(false)
|
|
22
22
|
})
|
|
23
23
|
|
|
24
|
-
it("
|
|
25
|
-
const
|
|
24
|
+
it("type instance reference", () => {
|
|
25
|
+
const user = type({
|
|
26
26
|
name: "string",
|
|
27
27
|
age: "number"
|
|
28
28
|
})
|
|
29
|
+
const parsedUser = type("string").pipe(s => JSON.parse(s), user)
|
|
30
|
+
|
|
29
31
|
attest<
|
|
30
32
|
Type<
|
|
31
33
|
(In: string) => Out<{
|
|
32
34
|
name: string
|
|
33
35
|
age: number
|
|
34
|
-
}
|
|
35
|
-
{}
|
|
36
|
+
}>
|
|
36
37
|
>
|
|
37
38
|
>(parsedUser)
|
|
39
|
+
|
|
40
|
+
const validUser = { name: "David", age: 30 }
|
|
41
|
+
attest(parsedUser(JSON.stringify(validUser))).equals(validUser)
|
|
42
|
+
const missingKey = { name: "David" }
|
|
43
|
+
attest(parsedUser(JSON.stringify(missingKey)).toString()).snap(
|
|
44
|
+
"age must be a number (was missing)"
|
|
45
|
+
)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("many pipes", () => {
|
|
49
|
+
const pipeAlphabet = type("'a'").pipe(
|
|
50
|
+
s => `${s}b` as const,
|
|
51
|
+
s => `${s}c` as const,
|
|
52
|
+
s => `${s}d` as const,
|
|
53
|
+
s => `${s}e` as const,
|
|
54
|
+
s => `${s}f` as const,
|
|
55
|
+
s => `${s}g` as const,
|
|
56
|
+
s => `${s}h` as const
|
|
57
|
+
)
|
|
58
|
+
attest<"abcdefgh">(pipeAlphabet.infer)
|
|
59
|
+
attest(pipeAlphabet("a")).equals("abcdefgh")
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("uses pipe for consecutive types", () => {
|
|
63
|
+
const bar = type({ bar: "number" })
|
|
64
|
+
const t = type({ foo: "string" }).pipe(bar)
|
|
65
|
+
attest<
|
|
66
|
+
Type<{
|
|
67
|
+
foo: string
|
|
68
|
+
bar: number
|
|
69
|
+
}>
|
|
70
|
+
>(t)
|
|
71
|
+
const expected = type({ foo: "string", bar: "number" })
|
|
72
|
+
attest(t.json).equals(expected.json)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it("uses pipe for many consecutive types", () => {
|
|
76
|
+
const t = type({ a: "1" }).pipe(
|
|
77
|
+
type({ b: "1" }),
|
|
78
|
+
type({ c: "1" }),
|
|
79
|
+
type({ d: "1" })
|
|
80
|
+
)
|
|
81
|
+
attest<
|
|
82
|
+
Type<{
|
|
83
|
+
a: 1
|
|
84
|
+
b: 1
|
|
85
|
+
c: 1
|
|
86
|
+
d: 1
|
|
87
|
+
}>
|
|
88
|
+
>(t)
|
|
89
|
+
const expected = type({ a: "1", b: "1", c: "1", d: "1" })
|
|
90
|
+
attest(t.json).equals(expected.json)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it("two morphs", () => {
|
|
94
|
+
const inefficientStringIsEmpty = type("string").pipe(
|
|
95
|
+
s => s.length,
|
|
96
|
+
length => length === 0
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
attest<Type<(In: string) => Out<boolean>>>(inefficientStringIsEmpty)
|
|
100
|
+
attest(inefficientStringIsEmpty("")).equals(true)
|
|
101
|
+
attest(inefficientStringIsEmpty("foo")).equals(false)
|
|
102
|
+
attest(inefficientStringIsEmpty(0).toString()).snap(
|
|
103
|
+
"must be a string (was number)"
|
|
104
|
+
)
|
|
38
105
|
})
|
|
39
106
|
|
|
40
107
|
it("any as out", () => {
|
|
41
|
-
const t = type("string", "=>",
|
|
108
|
+
const t = type("string", "=>", s => s as any)
|
|
42
109
|
attest<string>(t.in.infer)
|
|
43
110
|
attest<any>(t.infer)
|
|
44
111
|
})
|
|
45
112
|
|
|
46
113
|
it("never as out", () => {
|
|
47
|
-
const t = type("string", "=>",
|
|
114
|
+
const t = type("string", "=>", s => s as never)
|
|
48
115
|
attest<string>(t.in.infer)
|
|
49
116
|
attest<never>(t.infer)
|
|
50
117
|
})
|
|
51
118
|
|
|
52
|
-
it("return
|
|
119
|
+
it("return error", () => {
|
|
53
120
|
const divide100By = type("number", "=>", (n, ctx) =>
|
|
54
121
|
n !== 0 ? 100 / n : ctx.error("non-zero")
|
|
55
122
|
)
|
|
56
123
|
attest<Type<(In: number) => Out<number>>>(divide100By)
|
|
57
|
-
attest(divide100By(5)
|
|
58
|
-
attest(divide100By(0).
|
|
124
|
+
attest(divide100By(5)).equals(20)
|
|
125
|
+
attest(divide100By(0).toString()).snap("must be non-zero (was 0)")
|
|
59
126
|
})
|
|
60
127
|
|
|
61
128
|
it("at path", () => {
|
|
62
|
-
const t = type({ a: ["string", "=>",
|
|
129
|
+
const t = type({ a: ["string", "=>", data => data.length] })
|
|
63
130
|
attest<Type<{ a: (In: string) => Out<number> }>>(t)
|
|
64
131
|
|
|
65
132
|
const input = { a: "four" }
|
|
66
133
|
|
|
67
|
-
const
|
|
134
|
+
const out = t(input)
|
|
68
135
|
|
|
69
|
-
attest<{ a: number } |
|
|
70
|
-
attest<{ a: string } | undefined>(data).snap({ a: "four" })
|
|
71
|
-
// out is cloned before the morph
|
|
72
|
-
attest(out === (input as never)).equals(false)
|
|
73
|
-
// data has the original reference + data
|
|
74
|
-
attest(data === input).equals(true)
|
|
136
|
+
attest<{ a: number } | type.errors>(out).equals({ a: 4 })
|
|
75
137
|
})
|
|
76
138
|
|
|
77
139
|
it("in array", () => {
|
|
78
140
|
const types = scope({
|
|
79
|
-
lengthOfString: ["string", "=>",
|
|
141
|
+
lengthOfString: ["string", "=>", data => data.length],
|
|
80
142
|
mapToLengths: "lengthOfString[]"
|
|
81
143
|
}).export()
|
|
82
144
|
attest<Type<((In: string) => Out<number>)[]>>(types.mapToLengths)
|
|
83
|
-
const
|
|
84
|
-
attest<number[] |
|
|
145
|
+
const out = types.mapToLengths(["1", "22", "333"])
|
|
146
|
+
attest<number[] | type.errors>(out).equals([1, 2, 3])
|
|
85
147
|
})
|
|
86
148
|
|
|
87
149
|
it("object to string", () => {
|
|
88
|
-
const t = type([{ a: "string" }, "=>",
|
|
89
|
-
const
|
|
90
|
-
attest<
|
|
91
|
-
attest<string | undefined>(out).snap('{"a":"foo"}')
|
|
150
|
+
const t = type([{ a: "string" }, "=>", data => JSON.stringify(data)])
|
|
151
|
+
const out = t({ a: "foo" })
|
|
152
|
+
attest<string | type.errors>(out).snap('{"a":"foo"}')
|
|
92
153
|
})
|
|
93
154
|
|
|
94
155
|
it("intersection", () => {
|
|
95
156
|
const $ = scope({
|
|
96
157
|
b: "3.14",
|
|
97
|
-
a: ["number", "=>",
|
|
158
|
+
a: ["number", "=>", data => `${data}`],
|
|
98
159
|
aAndB: () => $.type("a&b"),
|
|
99
160
|
bAndA: () => $.type("b&a")
|
|
100
161
|
})
|
|
101
162
|
const types = $.export()
|
|
102
|
-
assertNodeKind(types.bAndA, "morph")
|
|
103
|
-
assertNodeKind(types.aAndB, "morph")
|
|
163
|
+
assertNodeKind(types.bAndA.raw, "morph")
|
|
164
|
+
assertNodeKind(types.aAndB.raw, "morph")
|
|
165
|
+
|
|
104
166
|
attest<Type<(In: 3.14) => Out<string>>>(types.aAndB)
|
|
105
167
|
attest(types.aAndB.json).snap({
|
|
106
|
-
|
|
107
|
-
morphs: types.aAndB.serializedMorphs
|
|
168
|
+
from: { unit: 3.14 },
|
|
169
|
+
morphs: types.aAndB.raw.serializedMorphs
|
|
108
170
|
})
|
|
109
171
|
attest<typeof types.aAndB>(types.bAndA)
|
|
110
172
|
attest(types.bAndA).equals(types.aAndB)
|
|
@@ -112,29 +174,29 @@ contextualize(() => {
|
|
|
112
174
|
|
|
113
175
|
it("object intersection", () => {
|
|
114
176
|
const $ = scope({
|
|
115
|
-
a: [{ a: "1" }, "=>",
|
|
177
|
+
a: [{ a: "1" }, "=>", data => `${data}`],
|
|
116
178
|
b: { b: "2" },
|
|
117
179
|
c: "a&b"
|
|
118
180
|
})
|
|
119
181
|
const types = $.export()
|
|
120
182
|
// TODO: FIX
|
|
121
183
|
// attest<Type<(In: { a: 1; b: 2 }) => string>>(types.c)
|
|
122
|
-
assertNodeKind(types.c, "morph")
|
|
184
|
+
assertNodeKind(types.c.raw, "morph")
|
|
123
185
|
attest(types.c.json).snap({
|
|
124
|
-
|
|
186
|
+
from: {
|
|
125
187
|
domain: "object",
|
|
126
188
|
prop: [
|
|
127
189
|
{ key: "a", value: { unit: 1 } },
|
|
128
190
|
{ key: "b", value: { unit: 2 } }
|
|
129
191
|
]
|
|
130
192
|
},
|
|
131
|
-
morphs: types.c.serializedMorphs
|
|
193
|
+
morphs: types.c.raw.serializedMorphs
|
|
132
194
|
})
|
|
133
195
|
})
|
|
134
196
|
|
|
135
197
|
it("union", () => {
|
|
136
198
|
const types = scope({
|
|
137
|
-
a: ["number", "=>",
|
|
199
|
+
a: ["number", "=>", data => `${data}`],
|
|
138
200
|
b: "boolean",
|
|
139
201
|
aOrB: "a|b",
|
|
140
202
|
bOrA: "b|a"
|
|
@@ -143,7 +205,7 @@ contextualize(() => {
|
|
|
143
205
|
const serializedMorphs =
|
|
144
206
|
types.aOrB.raw.firstReferenceOfKindOrThrow("morph").serializedMorphs
|
|
145
207
|
attest(types.aOrB.json).snap([
|
|
146
|
-
{
|
|
208
|
+
{ from: "number", morphs: serializedMorphs },
|
|
147
209
|
{ unit: false },
|
|
148
210
|
{ unit: true }
|
|
149
211
|
])
|
|
@@ -158,7 +220,7 @@ contextualize(() => {
|
|
|
158
220
|
|
|
159
221
|
it("deep union", () => {
|
|
160
222
|
const types = scope({
|
|
161
|
-
a: { a: ["number>0", "=>",
|
|
223
|
+
a: { a: ["number>0", "=>", data => `${data}`] },
|
|
162
224
|
b: { a: "Function" },
|
|
163
225
|
c: "a|b"
|
|
164
226
|
}).export()
|
|
@@ -184,7 +246,7 @@ contextualize(() => {
|
|
|
184
246
|
{
|
|
185
247
|
key: "a",
|
|
186
248
|
value: {
|
|
187
|
-
|
|
249
|
+
from: {
|
|
188
250
|
domain: "number",
|
|
189
251
|
min: { exclusive: true, rule: 0 }
|
|
190
252
|
},
|
|
@@ -198,77 +260,77 @@ contextualize(() => {
|
|
|
198
260
|
|
|
199
261
|
it("chained reference", () => {
|
|
200
262
|
const $ = scope({
|
|
201
|
-
a: type("string").
|
|
202
|
-
b: () => $.type("a").
|
|
263
|
+
a: type("string").pipe(s => s.length),
|
|
264
|
+
b: () => $.type("a").pipe(n => n === 0)
|
|
203
265
|
})
|
|
204
266
|
const types = $.export()
|
|
205
267
|
attest<Type<(In: string) => Out<boolean>>>(types.b)
|
|
206
|
-
assertNodeKind(types.b, "morph")
|
|
268
|
+
assertNodeKind(types.b.raw, "morph")
|
|
207
269
|
attest(types.b.json).snap({
|
|
208
|
-
|
|
209
|
-
morphs: types.b.serializedMorphs
|
|
270
|
+
from: "string",
|
|
271
|
+
morphs: types.b.raw.serializedMorphs
|
|
210
272
|
})
|
|
211
273
|
})
|
|
212
274
|
|
|
213
275
|
it("chained nested", () => {
|
|
214
276
|
const $ = scope({
|
|
215
|
-
a: type("string").
|
|
216
|
-
b: () => $.type({ a: "a" }).
|
|
277
|
+
a: type("string").pipe(s => s.length),
|
|
278
|
+
b: () => $.type({ a: "a" }).pipe(({ a }) => a === 0)
|
|
217
279
|
})
|
|
218
280
|
|
|
219
281
|
const types = $.export()
|
|
220
282
|
attest<Type<(In: { a: string }) => Out<boolean>>>(types.b)
|
|
221
|
-
assertNodeKind(types.b, "morph")
|
|
222
|
-
assertNodeKind(types.a, "morph")
|
|
283
|
+
assertNodeKind(types.b.raw, "morph")
|
|
284
|
+
assertNodeKind(types.a.raw, "morph")
|
|
223
285
|
attest(types.b.json).snap({
|
|
224
|
-
|
|
286
|
+
from: {
|
|
225
287
|
domain: "object",
|
|
226
288
|
prop: [
|
|
227
289
|
{
|
|
228
290
|
key: "a",
|
|
229
291
|
value: {
|
|
230
|
-
|
|
231
|
-
morphs: types.a.serializedMorphs
|
|
292
|
+
from: "string",
|
|
293
|
+
morphs: types.a.raw.serializedMorphs
|
|
232
294
|
}
|
|
233
295
|
}
|
|
234
296
|
]
|
|
235
297
|
},
|
|
236
|
-
morphs: types.b.serializedMorphs
|
|
298
|
+
morphs: types.b.raw.serializedMorphs
|
|
237
299
|
})
|
|
238
300
|
})
|
|
239
301
|
|
|
240
302
|
it("directly nested", () => {
|
|
241
303
|
const t = type([
|
|
242
304
|
{
|
|
243
|
-
a: ["string", "=>",
|
|
305
|
+
a: ["string", "=>", s => s.length]
|
|
244
306
|
},
|
|
245
307
|
"=>",
|
|
246
308
|
({ a }) => a === 0
|
|
247
309
|
])
|
|
248
310
|
// TODO: check
|
|
249
311
|
// attest<Type<(In: { a: string }) => Out<boolean>>>(t)
|
|
250
|
-
assertNodeKind(t, "morph")
|
|
251
|
-
const nestedMorph = t.firstReferenceOfKindOrThrow("morph")
|
|
312
|
+
assertNodeKind(t.raw, "morph")
|
|
313
|
+
const nestedMorph = t.raw.firstReferenceOfKindOrThrow("morph")
|
|
252
314
|
attest(t.json).snap({
|
|
253
|
-
|
|
315
|
+
from: {
|
|
254
316
|
domain: "object",
|
|
255
317
|
prop: [
|
|
256
318
|
{
|
|
257
319
|
key: "a",
|
|
258
320
|
value: {
|
|
259
|
-
|
|
321
|
+
from: "string",
|
|
260
322
|
morphs: nestedMorph.serializedMorphs
|
|
261
323
|
}
|
|
262
324
|
}
|
|
263
325
|
]
|
|
264
326
|
},
|
|
265
|
-
morphs: t.serializedMorphs
|
|
327
|
+
morphs: t.raw.serializedMorphs
|
|
266
328
|
})
|
|
267
329
|
})
|
|
268
330
|
|
|
269
331
|
it("discriminable tuple union", () => {
|
|
270
332
|
const $ = scope({
|
|
271
|
-
a: () => $.type(["string"]).
|
|
333
|
+
a: () => $.type(["string"]).pipe(s => [...s, "!"]),
|
|
272
334
|
b: ["boolean"],
|
|
273
335
|
c: () => $.type("a|b")
|
|
274
336
|
})
|
|
@@ -282,21 +344,20 @@ contextualize(() => {
|
|
|
282
344
|
"=>",
|
|
283
345
|
(s, ctx) => {
|
|
284
346
|
const result = Number.parseInt(s)
|
|
285
|
-
if (Number.isNaN(result))
|
|
286
|
-
|
|
287
|
-
}
|
|
347
|
+
if (Number.isNaN(result)) return ctx.error("an integer string")
|
|
348
|
+
|
|
288
349
|
return result
|
|
289
350
|
}
|
|
290
351
|
])
|
|
291
352
|
attest<Type<(In: string) => Out<number>>>(parsedInt)
|
|
292
|
-
attest(parsedInt("5")
|
|
293
|
-
attest(parsedInt("five").
|
|
353
|
+
attest(parsedInt("5")).snap(5)
|
|
354
|
+
attest(parsedInt("five").toString()).snap(
|
|
294
355
|
'must be an integer string (was "five")'
|
|
295
356
|
)
|
|
296
357
|
})
|
|
297
358
|
|
|
298
359
|
it("nullable return", () => {
|
|
299
|
-
const toNullableNumber = type(["string", "=>",
|
|
360
|
+
const toNullableNumber = type(["string", "=>", s => s.length || null])
|
|
300
361
|
attest<Type<(In: string) => Out<number | null>>>(toNullableNumber)
|
|
301
362
|
})
|
|
302
363
|
|
|
@@ -304,7 +365,7 @@ contextualize(() => {
|
|
|
304
365
|
const toUndefinableNumber = type([
|
|
305
366
|
"string",
|
|
306
367
|
"=>",
|
|
307
|
-
|
|
368
|
+
s => s.length || undefined
|
|
308
369
|
])
|
|
309
370
|
attest<Type<(In: string) => Out<number | undefined>>>(toUndefinableNumber)
|
|
310
371
|
})
|
|
@@ -313,7 +374,7 @@ contextualize(() => {
|
|
|
313
374
|
const toMaybeNumber = type([
|
|
314
375
|
"string",
|
|
315
376
|
"=>",
|
|
316
|
-
|
|
377
|
+
s =>
|
|
317
378
|
s.length === 0 ? undefined
|
|
318
379
|
: s.length === 1 ? null
|
|
319
380
|
: s.length
|
package/__tests__/scope.test.ts
CHANGED
|
@@ -53,22 +53,12 @@ contextualize(() => {
|
|
|
53
53
|
|
|
54
54
|
it("infers input and output", () => {
|
|
55
55
|
const $ = scope({
|
|
56
|
-
a: ["string", "=>",
|
|
56
|
+
a: ["string", "=>", s => s.length]
|
|
57
57
|
})
|
|
58
58
|
attest<{ a: number }>($.infer)
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
|
|
60
|
+
attest<{ a: string }>($.inferIn)
|
|
61
61
|
})
|
|
62
|
-
// TODO: remove if not preserving
|
|
63
|
-
// it("scope.scope", () => {
|
|
64
|
-
// const $ = scope({
|
|
65
|
-
// a: "string"
|
|
66
|
-
// })
|
|
67
|
-
// const importer = $.scope({ b: "a[]" })
|
|
68
|
-
// attest<{ b: string[] }>(importer.infer)
|
|
69
|
-
// const t = importer.type("b")
|
|
70
|
-
// attest(t.json).equals(type("string[]").json)
|
|
71
|
-
// })
|
|
72
62
|
|
|
73
63
|
it("infers its own helpers", () => {
|
|
74
64
|
const $ = scope({
|
|
@@ -151,8 +141,8 @@ contextualize(() => {
|
|
|
151
141
|
X: Apple
|
|
152
142
|
}).export()
|
|
153
143
|
|
|
154
|
-
const
|
|
155
|
-
attest(
|
|
144
|
+
const out = X({ pear: { tasty: true } })
|
|
145
|
+
attest(out).snap({ pear: { tasty: true } })
|
|
156
146
|
})
|
|
157
147
|
describe("define", () => {
|
|
158
148
|
it("ark", () => {
|
|
@@ -184,4 +174,201 @@ contextualize(() => {
|
|
|
184
174
|
)
|
|
185
175
|
})
|
|
186
176
|
})
|
|
177
|
+
describe("cyclic", () => {
|
|
178
|
+
it("base", () => {
|
|
179
|
+
const types = scope({ a: { b: "b" }, b: { a: "a" } }).export()
|
|
180
|
+
|
|
181
|
+
const a = {} as { b: typeof b }
|
|
182
|
+
const b = { a }
|
|
183
|
+
a.b = b
|
|
184
|
+
|
|
185
|
+
attest(types.a(a)).equals(a)
|
|
186
|
+
attest(types.a({ b: { a: { b: { a: 5 } } } }).toString()).snap(
|
|
187
|
+
"b.a.b.a must be an object (was number)"
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// Type hint displays as "..." on hitting cycle (or any if "noErrorTruncation" is true)
|
|
191
|
+
attest({} as typeof types.a.infer).type.toString.snap()
|
|
192
|
+
attest({} as typeof types.b.infer.a.b.a.b.a.b.a).type.toString.snap()
|
|
193
|
+
|
|
194
|
+
// @ts-expect-error
|
|
195
|
+
attest({} as typeof types.a.infer.b.a.b.c).type.errors.snap(
|
|
196
|
+
`Property 'c' does not exist on type '{ a: { b: ...; }; }'.`
|
|
197
|
+
)
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
const getCyclicScope = () =>
|
|
201
|
+
scope({
|
|
202
|
+
package: {
|
|
203
|
+
name: "string",
|
|
204
|
+
"dependencies?": "package[]",
|
|
205
|
+
"contributors?": "contributor[]"
|
|
206
|
+
},
|
|
207
|
+
contributor: {
|
|
208
|
+
email: "email",
|
|
209
|
+
"packages?": "package[]"
|
|
210
|
+
}
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
type Package = ReturnType<typeof getCyclicScope>["infer"]["package"]
|
|
214
|
+
|
|
215
|
+
const getCyclicData = () => {
|
|
216
|
+
const packageData = {
|
|
217
|
+
name: "arktype",
|
|
218
|
+
dependencies: [{ name: "typescript" }],
|
|
219
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
220
|
+
} satisfies Package
|
|
221
|
+
packageData.dependencies.push(packageData)
|
|
222
|
+
return packageData
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
it("cyclic union", () => {
|
|
226
|
+
const $ = scope({
|
|
227
|
+
a: { b: "b|false" },
|
|
228
|
+
b: { a: "a|true" }
|
|
229
|
+
})
|
|
230
|
+
attest($.infer).type.toString.snap(
|
|
231
|
+
"{ a: { b: false | { a: true | any; }; }; b: { a: true | { b: false | any; }; }; }"
|
|
232
|
+
)
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
it("cyclic intersection", () => {
|
|
236
|
+
const $ = scope({
|
|
237
|
+
a: { b: "b&a" },
|
|
238
|
+
b: { a: "a&b" }
|
|
239
|
+
})
|
|
240
|
+
attest($.infer).type.toString.snap(
|
|
241
|
+
"{ a: { b: { a: { b: any; a: any; }; b: any; }; }; b: { a: { b: { a: any; b: any; }; a: any; }; }; }"
|
|
242
|
+
)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it("allows valid", () => {
|
|
246
|
+
const types = getCyclicScope().export()
|
|
247
|
+
const data = getCyclicData()
|
|
248
|
+
attest(types.package(data)).snap({
|
|
249
|
+
name: "arktype",
|
|
250
|
+
dependencies: [{ name: "typescript" }, "(cycle)" as never],
|
|
251
|
+
contributors: [{ email: "david@arktype.io" }]
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it("adds errors on invalid", () => {
|
|
256
|
+
const types = getCyclicScope().export()
|
|
257
|
+
const data = getCyclicData()
|
|
258
|
+
data.contributors[0].email = "ssalbdivad"
|
|
259
|
+
attest(types.package(data).toString()).snap(
|
|
260
|
+
'contributors[0].email must be a valid email (was "ssalbdivad")'
|
|
261
|
+
)
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it("can include cyclic data in message", () => {
|
|
265
|
+
const data = getCyclicData()
|
|
266
|
+
const nonSelfDependent = getCyclicScope().type([
|
|
267
|
+
"package",
|
|
268
|
+
":",
|
|
269
|
+
p => !p.dependencies?.some(d => d.name === p.name)
|
|
270
|
+
])
|
|
271
|
+
attest(nonSelfDependent(data).toString()).snap(
|
|
272
|
+
'must be valid according to an anonymous predicate (was {"name":"arktype","dependencies":[{"name":"typescript"},"(cycle)"],"contributors":[{"email":"david@arktype.io"}]})'
|
|
273
|
+
)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
it("union cyclic reference", () => {
|
|
277
|
+
const types = scope({
|
|
278
|
+
a: {
|
|
279
|
+
b: "b"
|
|
280
|
+
},
|
|
281
|
+
b: {
|
|
282
|
+
a: "a|3"
|
|
283
|
+
}
|
|
284
|
+
}).export()
|
|
285
|
+
attest(types.a.infer).type.toString.snap("{ b: { a: 3 | any; }")
|
|
286
|
+
|
|
287
|
+
attest(types.a.json).snap({
|
|
288
|
+
domain: "object",
|
|
289
|
+
prop: [
|
|
290
|
+
{
|
|
291
|
+
key: "b",
|
|
292
|
+
value: {
|
|
293
|
+
domain: "object",
|
|
294
|
+
prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
]
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
const valid: typeof types.a.infer = { b: { a: 3 } }
|
|
301
|
+
|
|
302
|
+
attest(types.a(valid)).equals(valid)
|
|
303
|
+
|
|
304
|
+
valid.b.a = valid
|
|
305
|
+
|
|
306
|
+
// check cyclic
|
|
307
|
+
attest(types.a(valid)).equals(valid)
|
|
308
|
+
|
|
309
|
+
attest(types.a({ b: { a: { b: { a: 4 } } } }).toString()).snap(
|
|
310
|
+
'b.a.b.a must be an object or 3 (was number, 4) or b.a must be 3 (was {"b":{"a":4}})'
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
attest(types.b.infer).type.toString.snap("{ a: 3 | { b: any; }; }")
|
|
314
|
+
attest(types.b.json).snap({
|
|
315
|
+
domain: "object",
|
|
316
|
+
prop: [{ key: "a", value: ["$a", { unit: 3 }] }]
|
|
317
|
+
})
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
it("intersect cyclic reference", () => {
|
|
321
|
+
const types = scope({
|
|
322
|
+
a: {
|
|
323
|
+
b: "b"
|
|
324
|
+
},
|
|
325
|
+
b: {
|
|
326
|
+
c: "a&b"
|
|
327
|
+
}
|
|
328
|
+
}).export()
|
|
329
|
+
attest(types.a.infer).type.toString.snap()
|
|
330
|
+
attest(types.b.infer).type.toString.snap()
|
|
331
|
+
|
|
332
|
+
const expectedCyclicJson =
|
|
333
|
+
types.a.raw.firstReferenceOfKindOrThrow("alias").json
|
|
334
|
+
|
|
335
|
+
attest(types.a.json).snap({
|
|
336
|
+
domain: "object",
|
|
337
|
+
prop: [
|
|
338
|
+
{
|
|
339
|
+
key: "b",
|
|
340
|
+
value: {
|
|
341
|
+
domain: "object",
|
|
342
|
+
prop: [
|
|
343
|
+
{
|
|
344
|
+
key: "c",
|
|
345
|
+
value: expectedCyclicJson
|
|
346
|
+
}
|
|
347
|
+
]
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
]
|
|
351
|
+
})
|
|
352
|
+
const a = {} as typeof types.a.infer
|
|
353
|
+
const b = { c: {} } as typeof types.b.infer
|
|
354
|
+
a.b = b
|
|
355
|
+
b.c.b = b
|
|
356
|
+
b.c.c = b.c
|
|
357
|
+
|
|
358
|
+
attest(types.a(a)).equals(a)
|
|
359
|
+
attest(types.a({ b: { c: {} } }).toString())
|
|
360
|
+
.snap(`b.c.b must be { c: a&b } (was missing)
|
|
361
|
+
b.c.c must be a&b (was missing)`)
|
|
362
|
+
|
|
363
|
+
attest(types.b.json).snap({
|
|
364
|
+
domain: "object",
|
|
365
|
+
prop: [
|
|
366
|
+
{
|
|
367
|
+
key: "c",
|
|
368
|
+
value: expectedCyclicJson
|
|
369
|
+
}
|
|
370
|
+
]
|
|
371
|
+
})
|
|
372
|
+
})
|
|
373
|
+
})
|
|
187
374
|
})
|