effect 4.0.0-beta.97 → 4.0.0-beta.98
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/dist/LayerRef.d.ts +4 -6
- package/dist/LayerRef.d.ts.map +1 -1
- package/dist/LayerRef.js +4 -6
- package/dist/LayerRef.js.map +1 -1
- package/dist/Number.d.ts.map +1 -1
- package/dist/Number.js +28 -10
- package/dist/Number.js.map +1 -1
- package/dist/Schema.d.ts +22 -22
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +36 -33
- package/dist/Schema.js.map +1 -1
- package/dist/SchemaAST.d.ts.map +1 -1
- package/dist/SchemaAST.js +25 -10
- package/dist/SchemaAST.js.map +1 -1
- package/dist/SchemaError.d.ts +53 -0
- package/dist/SchemaError.d.ts.map +1 -0
- package/dist/SchemaError.js +61 -0
- package/dist/SchemaError.js.map +1 -0
- package/dist/SchemaGetter.d.ts.map +1 -1
- package/dist/SchemaGetter.js +17 -12
- package/dist/SchemaGetter.js.map +1 -1
- package/dist/SchemaRepresentation.d.ts.map +1 -1
- package/dist/SchemaRepresentation.js +186 -224
- package/dist/SchemaRepresentation.js.map +1 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/effect.js +26 -8
- package/dist/internal/effect.js.map +1 -1
- package/dist/internal/schema/equivalence.js +1 -2
- package/dist/internal/schema/equivalence.js.map +1 -1
- package/dist/internal/schema/representation.js +1 -3
- package/dist/internal/schema/representation.js.map +1 -1
- package/dist/internal/schema/schema.d.ts +0 -13
- package/dist/internal/schema/schema.d.ts.map +1 -1
- package/dist/internal/schema/schema.js +4 -29
- package/dist/internal/schema/schema.js.map +1 -1
- package/dist/unstable/ai/McpSchema.d.ts +128 -128
- package/dist/unstable/ai/McpSchema.d.ts.map +1 -1
- package/dist/unstable/cli/CliError.d.ts +8 -16
- package/dist/unstable/cli/CliError.d.ts.map +1 -1
- package/dist/unstable/cli/CliError.js +8 -16
- package/dist/unstable/cli/CliError.js.map +1 -1
- package/dist/unstable/cli/Command.d.ts +4 -3
- package/dist/unstable/cli/Command.d.ts.map +1 -1
- package/dist/unstable/cli/Command.js +3 -3
- package/dist/unstable/cli/Command.js.map +1 -1
- package/dist/unstable/cli/GlobalFlag.d.ts +6 -8
- package/dist/unstable/cli/GlobalFlag.d.ts.map +1 -1
- package/dist/unstable/cli/GlobalFlag.js +6 -6
- package/dist/unstable/cli/GlobalFlag.js.map +1 -1
- package/dist/unstable/cluster/EntityProxyServer.d.ts +1 -1
- package/dist/unstable/cluster/EntityProxyServer.d.ts.map +1 -1
- package/dist/unstable/cluster/EntityProxyServer.js +2 -4
- package/dist/unstable/cluster/EntityProxyServer.js.map +1 -1
- package/dist/unstable/cluster/internal/entityManager.js +15 -12
- package/dist/unstable/cluster/internal/entityManager.js.map +1 -1
- package/dist/unstable/httpapi/HttpApi.d.ts +36 -15
- package/dist/unstable/httpapi/HttpApi.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApi.js +19 -20
- package/dist/unstable/httpapi/HttpApi.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts +51 -62
- package/dist/unstable/httpapi/HttpApiBuilder.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiBuilder.js +68 -58
- package/dist/unstable/httpapi/HttpApiBuilder.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.d.ts +53 -53
- package/dist/unstable/httpapi/HttpApiClient.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiClient.js +40 -30
- package/dist/unstable/httpapi/HttpApiClient.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts +165 -164
- package/dist/unstable/httpapi/HttpApiEndpoint.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiEndpoint.js +31 -19
- package/dist/unstable/httpapi/HttpApiEndpoint.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.d.ts +22 -0
- package/dist/unstable/httpapi/HttpApiError.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiError.js +31 -0
- package/dist/unstable/httpapi/HttpApiError.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiGroup.d.ts +55 -30
- package/dist/unstable/httpapi/HttpApiGroup.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiGroup.js +22 -29
- package/dist/unstable/httpapi/HttpApiGroup.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts +8 -8
- package/dist/unstable/httpapi/HttpApiMiddleware.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiMiddleware.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiScalar.d.ts +2 -2
- package/dist/unstable/httpapi/HttpApiScalar.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiScalar.js +4 -3
- package/dist/unstable/httpapi/HttpApiScalar.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiSchema.d.ts +8 -0
- package/dist/unstable/httpapi/HttpApiSchema.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiSchema.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiSwagger.d.ts +1 -1
- package/dist/unstable/httpapi/HttpApiSwagger.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiSwagger.js.map +1 -1
- package/dist/unstable/httpapi/HttpApiTest.d.ts +4 -4
- package/dist/unstable/httpapi/HttpApiTest.d.ts.map +1 -1
- package/dist/unstable/httpapi/HttpApiTest.js +9 -8
- package/dist/unstable/httpapi/HttpApiTest.js.map +1 -1
- package/dist/unstable/httpapi/OpenApi.d.ts +1 -1
- package/dist/unstable/httpapi/OpenApi.d.ts.map +1 -1
- package/dist/unstable/httpapi/OpenApi.js +77 -37
- package/dist/unstable/httpapi/OpenApi.js.map +1 -1
- package/dist/unstable/httpapi/internal/html.js +6 -2
- package/dist/unstable/httpapi/internal/html.js.map +1 -1
- package/dist/unstable/httpapi/internal/mediaType.d.ts +2 -0
- package/dist/unstable/httpapi/internal/mediaType.d.ts.map +1 -0
- package/dist/unstable/httpapi/internal/mediaType.js +7 -0
- package/dist/unstable/httpapi/internal/mediaType.js.map +1 -0
- package/dist/unstable/persistence/PersistedQueue.d.ts.map +1 -1
- package/dist/unstable/persistence/PersistedQueue.js +16 -12
- package/dist/unstable/persistence/PersistedQueue.js.map +1 -1
- package/dist/unstable/reactivity/Atom.js +1 -1
- package/dist/unstable/reactivity/Atom.js.map +1 -1
- package/dist/unstable/reactivity/AtomHttpApi.d.ts +7 -7
- package/dist/unstable/reactivity/AtomHttpApi.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomHttpApi.js +3 -2
- package/dist/unstable/reactivity/AtomHttpApi.js.map +1 -1
- package/dist/unstable/rpc/RpcServer.d.ts +3 -1
- package/dist/unstable/rpc/RpcServer.d.ts.map +1 -1
- package/dist/unstable/rpc/RpcServer.js +6 -3
- package/dist/unstable/rpc/RpcServer.js.map +1 -1
- package/dist/unstable/workflow/DurableDeferred.js +1 -1
- package/dist/unstable/workflow/DurableDeferred.js.map +1 -1
- package/dist/unstable/workflow/WorkflowProxyServer.d.ts +1 -1
- package/dist/unstable/workflow/WorkflowProxyServer.d.ts.map +1 -1
- package/dist/unstable/workflow/WorkflowProxyServer.js +1 -2
- package/dist/unstable/workflow/WorkflowProxyServer.js.map +1 -1
- package/package.json +1 -1
- package/src/LayerRef.ts +4 -6
- package/src/Number.ts +29 -10
- package/src/Schema.ts +42 -34
- package/src/SchemaAST.ts +25 -13
- package/src/SchemaError.ts +64 -0
- package/src/SchemaGetter.ts +22 -16
- package/src/SchemaRepresentation.ts +165 -202
- package/src/index.ts +10 -0
- package/src/internal/effect.ts +39 -22
- package/src/internal/schema/equivalence.ts +1 -2
- package/src/internal/schema/representation.ts +4 -4
- package/src/internal/schema/schema.ts +4 -37
- package/src/unstable/cli/CliError.ts +24 -16
- package/src/unstable/cli/Command.ts +8 -8
- package/src/unstable/cli/GlobalFlag.ts +22 -33
- package/src/unstable/cluster/EntityProxyServer.ts +15 -17
- package/src/unstable/cluster/internal/entityManager.ts +48 -34
- package/src/unstable/httpapi/HttpApi.ts +72 -47
- package/src/unstable/httpapi/HttpApiBuilder.ts +320 -217
- package/src/unstable/httpapi/HttpApiClient.ts +196 -151
- package/src/unstable/httpapi/HttpApiEndpoint.ts +251 -447
- package/src/unstable/httpapi/HttpApiError.ts +33 -0
- package/src/unstable/httpapi/HttpApiGroup.ts +95 -74
- package/src/unstable/httpapi/HttpApiMiddleware.ts +10 -7
- package/src/unstable/httpapi/HttpApiScalar.ts +15 -12
- package/src/unstable/httpapi/HttpApiSchema.ts +8 -0
- package/src/unstable/httpapi/HttpApiSwagger.ts +2 -2
- package/src/unstable/httpapi/HttpApiTest.ts +15 -14
- package/src/unstable/httpapi/OpenApi.ts +74 -36
- package/src/unstable/httpapi/internal/html.ts +9 -2
- package/src/unstable/httpapi/internal/mediaType.ts +6 -0
- package/src/unstable/persistence/PersistedQueue.ts +16 -14
- package/src/unstable/reactivity/Atom.ts +1 -1
- package/src/unstable/reactivity/AtomHttpApi.ts +164 -162
- package/src/unstable/rpc/RpcServer.ts +10 -4
- package/src/unstable/workflow/DurableDeferred.ts +1 -1
- package/src/unstable/workflow/WorkflowProxyServer.ts +11 -12
package/src/Schema.ts
CHANGED
|
@@ -40,7 +40,6 @@ import * as InternalArbitrary from "./internal/schema/arbitrary.ts"
|
|
|
40
40
|
import * as InternalEquivalence from "./internal/schema/equivalence.ts"
|
|
41
41
|
import * as InternalStandard from "./internal/schema/representation.ts"
|
|
42
42
|
import * as InternalSchema from "./internal/schema/schema.ts"
|
|
43
|
-
import { SchemaError } from "./internal/schema/schema.ts"
|
|
44
43
|
import * as JsonPatch from "./JsonPatch.ts"
|
|
45
44
|
import * as JsonSchema from "./JsonSchema.ts"
|
|
46
45
|
import { remainder } from "./Number.ts"
|
|
@@ -54,6 +53,7 @@ import * as Redacted_ from "./Redacted.ts"
|
|
|
54
53
|
import * as Result_ from "./Result.ts"
|
|
55
54
|
import * as Scheduler from "./Scheduler.ts"
|
|
56
55
|
import * as SchemaAST from "./SchemaAST.ts"
|
|
56
|
+
import { isSchemaError, SchemaError } from "./SchemaError.ts"
|
|
57
57
|
import * as SchemaGetter from "./SchemaGetter.ts"
|
|
58
58
|
import * as SchemaIssue from "./SchemaIssue.ts"
|
|
59
59
|
import * as SchemaParser from "./SchemaParser.ts"
|
|
@@ -1029,6 +1029,27 @@ export interface Optic<out T, out Iso> extends Schema<T> {
|
|
|
1029
1029
|
}
|
|
1030
1030
|
|
|
1031
1031
|
export {
|
|
1032
|
+
/**
|
|
1033
|
+
* Returns `true` if `u` is a {@link SchemaError}.
|
|
1034
|
+
*
|
|
1035
|
+
* **Example** (Narrowing Schema errors in a catch block)
|
|
1036
|
+
*
|
|
1037
|
+
* ```ts
|
|
1038
|
+
* import { Schema } from "effect"
|
|
1039
|
+
*
|
|
1040
|
+
* try {
|
|
1041
|
+
* Schema.decodeUnknownSync(Schema.Number)("oops")
|
|
1042
|
+
* } catch (err) {
|
|
1043
|
+
* if (Schema.isSchemaError(err)) {
|
|
1044
|
+
* console.log(err._tag) // "SchemaError"
|
|
1045
|
+
* }
|
|
1046
|
+
* }
|
|
1047
|
+
* ```
|
|
1048
|
+
*
|
|
1049
|
+
* @category guards
|
|
1050
|
+
* @since 4.0.0
|
|
1051
|
+
*/
|
|
1052
|
+
isSchemaError,
|
|
1032
1053
|
/**
|
|
1033
1054
|
* Error thrown (or returned as the error channel value) when schema decoding
|
|
1034
1055
|
* or encoding fails.
|
|
@@ -1063,30 +1084,6 @@ export {
|
|
|
1063
1084
|
SchemaError
|
|
1064
1085
|
}
|
|
1065
1086
|
|
|
1066
|
-
/**
|
|
1067
|
-
* Returns `true` if `u` is a {@link SchemaError}.
|
|
1068
|
-
*
|
|
1069
|
-
* **Example** (Narrowing Schema errors in a catch block)
|
|
1070
|
-
*
|
|
1071
|
-
* ```ts
|
|
1072
|
-
* import { Schema } from "effect"
|
|
1073
|
-
*
|
|
1074
|
-
* try {
|
|
1075
|
-
* Schema.decodeUnknownSync(Schema.Number)("oops")
|
|
1076
|
-
* } catch (err) {
|
|
1077
|
-
* if (Schema.isSchemaError(err)) {
|
|
1078
|
-
* console.log(err._tag) // "SchemaError"
|
|
1079
|
-
* }
|
|
1080
|
-
* }
|
|
1081
|
-
* ```
|
|
1082
|
-
*
|
|
1083
|
-
* @category guards
|
|
1084
|
-
* @since 4.0.0
|
|
1085
|
-
*/
|
|
1086
|
-
export function isSchemaError(u: unknown): u is SchemaError {
|
|
1087
|
-
return Predicate.hasProperty(u, InternalSchema.SchemaErrorTypeId)
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
1087
|
function makeStandardResult<A>(exit: Exit_.Exit<StandardSchemaV1.Result<A>>): StandardSchemaV1.Result<A> {
|
|
1091
1088
|
return Exit_.isSuccess(exit) ? exit.value : {
|
|
1092
1089
|
issues: [{ message: Cause_.pretty(exit.cause) }]
|
|
@@ -1374,7 +1371,7 @@ export function decodeUnknownEffect<S extends Constraint>(schema: S, options?: S
|
|
|
1374
1371
|
input: unknown,
|
|
1375
1372
|
options?: SchemaAST.ParseOptions
|
|
1376
1373
|
): Effect.Effect<S["Type"], SchemaError, S["DecodingServices"]> => {
|
|
1377
|
-
return InternalSchema.
|
|
1374
|
+
return InternalSchema.fromIssueEffect(parser(input, options))
|
|
1378
1375
|
}
|
|
1379
1376
|
}
|
|
1380
1377
|
|
|
@@ -1478,10 +1475,16 @@ function runSchemaErrorSync<A>(
|
|
|
1478
1475
|
export function decodeUnknownExit<S extends ConstraintDecoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions) {
|
|
1479
1476
|
const parser = SchemaParser.decodeUnknownExit(schema, options)
|
|
1480
1477
|
return (input: unknown, options?: SchemaAST.ParseOptions): Exit_.Exit<S["Type"], SchemaError> => {
|
|
1481
|
-
return
|
|
1478
|
+
return fromIssueExit(parser(input, options))
|
|
1482
1479
|
}
|
|
1483
1480
|
}
|
|
1484
1481
|
|
|
1482
|
+
function fromIssueExit<A>(exit: Exit_.Exit<A, SchemaIssue.Issue>): Exit_.Exit<A, SchemaError> {
|
|
1483
|
+
return Exit_.isSuccess(exit)
|
|
1484
|
+
? Exit_.succeed(exit.value)
|
|
1485
|
+
: Exit_.failCause(Cause_.map(exit.cause, (issue) => new SchemaError(issue)))
|
|
1486
|
+
}
|
|
1487
|
+
|
|
1485
1488
|
/**
|
|
1486
1489
|
* Decodes a typed input (the schema's `Encoded` type) against a schema
|
|
1487
1490
|
* synchronously, returning an `Exit` that is either a `Success` with the decoded
|
|
@@ -1837,7 +1840,7 @@ export function encodeUnknownEffect<S extends Constraint>(schema: S, options?: S
|
|
|
1837
1840
|
input: unknown,
|
|
1838
1841
|
options?: SchemaAST.ParseOptions
|
|
1839
1842
|
): Effect.Effect<S["Encoded"], SchemaError, S["EncodingServices"]> => {
|
|
1840
|
-
return InternalSchema.
|
|
1843
|
+
return InternalSchema.fromIssueEffect(parser(input, options))
|
|
1841
1844
|
}
|
|
1842
1845
|
}
|
|
1843
1846
|
|
|
@@ -1902,7 +1905,7 @@ export const encodeEffect: <S extends Constraint>(
|
|
|
1902
1905
|
export function encodeUnknownExit<S extends ConstraintEncoder<unknown>>(schema: S, options?: SchemaAST.ParseOptions) {
|
|
1903
1906
|
const parser = SchemaParser.encodeUnknownExit(schema, options)
|
|
1904
1907
|
return (input: unknown, options?: SchemaAST.ParseOptions): Exit_.Exit<S["Encoded"], SchemaError> => {
|
|
1905
|
-
return
|
|
1908
|
+
return fromIssueExit(parser(input, options))
|
|
1906
1909
|
}
|
|
1907
1910
|
}
|
|
1908
1911
|
|
|
@@ -5695,7 +5698,13 @@ export function withConstructorDefault<S extends Constraint & WithoutConstructor
|
|
|
5695
5698
|
defaultValue: Effect.Effect<S["~type.make.in"], SchemaError>
|
|
5696
5699
|
) {
|
|
5697
5700
|
return (schema: S): withConstructorDefault<S> =>
|
|
5698
|
-
make(SchemaAST.withConstructorDefault(schema.ast,
|
|
5701
|
+
make(SchemaAST.withConstructorDefault(schema.ast, toIssueEffect(defaultValue)), { schema })
|
|
5702
|
+
}
|
|
5703
|
+
|
|
5704
|
+
function toIssueEffect<A, R>(
|
|
5705
|
+
self: Effect.Effect<A, SchemaError, R>
|
|
5706
|
+
): Effect.Effect<A, SchemaIssue.Issue, R> {
|
|
5707
|
+
return Effect.catchCause(self, (cause) => Effect.failCauseSync(() => Cause_.map(cause, (error) => error.issue)))
|
|
5699
5708
|
}
|
|
5700
5709
|
|
|
5701
5710
|
/**
|
|
@@ -5767,7 +5776,7 @@ export function withDecodingDefaultKey<S extends Constraint, R = never>(
|
|
|
5767
5776
|
const encode = options?.encodingStrategy === "omit" ? SchemaGetter.omit() : SchemaGetter.passthrough()
|
|
5768
5777
|
return (self: S): withDecodingDefaultKey<S, R> => {
|
|
5769
5778
|
return optionalKey(toEncoded(self)).pipe(decodeTo(self, {
|
|
5770
|
-
decode: SchemaGetter.withDefault(
|
|
5779
|
+
decode: SchemaGetter.withDefault(toIssueEffect(defaultValue)),
|
|
5771
5780
|
encode
|
|
5772
5781
|
}))
|
|
5773
5782
|
}
|
|
@@ -5875,7 +5884,7 @@ export function withDecodingDefault<S extends Constraint, R = never>(
|
|
|
5875
5884
|
const encode = options?.encodingStrategy === "omit" ? SchemaGetter.omit() : SchemaGetter.passthrough()
|
|
5876
5885
|
return (self: S): withDecodingDefault<S, R> => {
|
|
5877
5886
|
return optional(toEncoded(self)).pipe(decodeTo(self, {
|
|
5878
|
-
decode: SchemaGetter.withDefault(
|
|
5887
|
+
decode: SchemaGetter.withDefault(toIssueEffect(defaultValue)),
|
|
5879
5888
|
encode
|
|
5880
5889
|
}))
|
|
5881
5890
|
}
|
|
@@ -13599,8 +13608,7 @@ export function toFormatter<S extends Constraint>(schema: S, options?: {
|
|
|
13599
13608
|
// handle post rest elements
|
|
13600
13609
|
// ---------------------------------------------
|
|
13601
13610
|
for (let j = 0; j < tail.length; j++) {
|
|
13602
|
-
i
|
|
13603
|
-
out.push(tail[j](t[i]))
|
|
13611
|
+
out.push(tail[j](t[i + j]))
|
|
13604
13612
|
}
|
|
13605
13613
|
}
|
|
13606
13614
|
|
package/src/SchemaAST.ts
CHANGED
|
@@ -2504,9 +2504,9 @@ export function collectSentinels(ast: AST): Array<Sentinel> {
|
|
|
2504
2504
|
}
|
|
2505
2505
|
|
|
2506
2506
|
type CandidateIndex = {
|
|
2507
|
-
byType?: { [K in Type]?: Array<
|
|
2508
|
-
bySentinel?: Map<PropertyKey, Map<LiteralValue | symbol, Array<
|
|
2509
|
-
otherwise?: { [K in Type]?: Array<
|
|
2507
|
+
byType?: { [K in Type]?: Array<number> }
|
|
2508
|
+
bySentinel?: Map<PropertyKey, Map<LiteralValue | symbol, Array<number>>>
|
|
2509
|
+
otherwise?: { [K in Type]?: Array<number> }
|
|
2510
2510
|
}
|
|
2511
2511
|
|
|
2512
2512
|
const candidateIndexCache = new WeakMap<ReadonlyArray<AST>, CandidateIndex>()
|
|
@@ -2516,16 +2516,17 @@ function getIndex(types: ReadonlyArray<AST>): CandidateIndex {
|
|
|
2516
2516
|
if (idx) return idx
|
|
2517
2517
|
|
|
2518
2518
|
idx = {}
|
|
2519
|
-
for (
|
|
2519
|
+
for (let i = 0; i < types.length; i++) {
|
|
2520
|
+
const a = types[i]
|
|
2520
2521
|
const encoded = toEncoded(a)
|
|
2521
2522
|
if (isNever(encoded)) continue
|
|
2522
2523
|
|
|
2523
|
-
const
|
|
2524
|
+
const candidateTypes = getCandidateTypes(encoded)
|
|
2524
2525
|
const sentinels = collectSentinels(encoded)
|
|
2525
2526
|
|
|
2526
2527
|
// by-type (always filled – cheap primary filter)
|
|
2527
2528
|
idx.byType ??= {}
|
|
2528
|
-
for (const t of
|
|
2529
|
+
for (const t of candidateTypes) (idx.byType[t] ??= []).push(i)
|
|
2529
2530
|
|
|
2530
2531
|
if (sentinels.length > 0) { // discriminated variants
|
|
2531
2532
|
idx.bySentinel ??= new Map()
|
|
@@ -2534,11 +2535,11 @@ function getIndex(types: ReadonlyArray<AST>): CandidateIndex {
|
|
|
2534
2535
|
if (!m) idx.bySentinel.set(key, m = new Map())
|
|
2535
2536
|
let arr = m.get(literal)
|
|
2536
2537
|
if (!arr) m.set(literal, arr = [])
|
|
2537
|
-
arr.push(
|
|
2538
|
+
arr.push(i)
|
|
2538
2539
|
}
|
|
2539
2540
|
} else { // non-discriminated
|
|
2540
2541
|
idx.otherwise ??= {}
|
|
2541
|
-
for (const t of
|
|
2542
|
+
for (const t of candidateTypes) (idx.otherwise[t] ??= []).push(i)
|
|
2542
2543
|
}
|
|
2543
2544
|
}
|
|
2544
2545
|
|
|
@@ -2571,18 +2572,22 @@ export function getCandidates(input: any, types: ReadonlyArray<AST>): ReadonlyAr
|
|
|
2571
2572
|
if (idx.bySentinel) {
|
|
2572
2573
|
const base = idx.otherwise?.[runtimeType] ?? []
|
|
2573
2574
|
if (runtimeType === "object" || runtimeType === "array") {
|
|
2575
|
+
const selected = new Set(base)
|
|
2574
2576
|
for (const [k, m] of idx.bySentinel) {
|
|
2575
2577
|
if (Object.hasOwn(input, k)) {
|
|
2576
2578
|
const match = m.get((input as any)[k])
|
|
2577
|
-
if (match)
|
|
2579
|
+
if (match) {
|
|
2580
|
+
for (const candidate of match) selected.add(candidate)
|
|
2581
|
+
}
|
|
2578
2582
|
}
|
|
2579
2583
|
}
|
|
2584
|
+
return Array.from(selected).sort((a, b) => a - b).map((i) => types[i]).filter(filterLiterals(input))
|
|
2580
2585
|
}
|
|
2581
|
-
return base
|
|
2586
|
+
return base.map((i) => types[i])
|
|
2582
2587
|
}
|
|
2583
2588
|
|
|
2584
2589
|
// 2. Fallback: runtime-type dispatch only
|
|
2585
|
-
return (idx.byType?.[runtimeType] ?? []).filter(filterLiterals(input))
|
|
2590
|
+
return (idx.byType?.[runtimeType] ?? []).map((i) => types[i]).filter(filterLiterals(input))
|
|
2586
2591
|
}
|
|
2587
2592
|
|
|
2588
2593
|
/**
|
|
@@ -2659,7 +2664,7 @@ export class Union<A extends AST = AST> extends Base {
|
|
|
2659
2664
|
options
|
|
2660
2665
|
}
|
|
2661
2666
|
const concurrency = resolveConcurrency(options?.concurrency)
|
|
2662
|
-
const eff = parseUnion(state, candidates, concurrency)
|
|
2667
|
+
const eff = parseUnion(state, candidates, concurrency ? { ...concurrency, orderedStep: true } : undefined)
|
|
2663
2668
|
if (!eff) {
|
|
2664
2669
|
return state.out
|
|
2665
2670
|
? Effect.succeed(state.out)
|
|
@@ -3883,8 +3888,15 @@ export function isJson(u: unknown): u is Schema.Json {
|
|
|
3883
3888
|
if (validated.has(u)) {
|
|
3884
3889
|
return true
|
|
3885
3890
|
}
|
|
3891
|
+
const isArray = Array.isArray(u)
|
|
3892
|
+
if (!isArray) {
|
|
3893
|
+
const prototype = Object.getPrototypeOf(u)
|
|
3894
|
+
if (prototype !== null && Object.getPrototypeOf(prototype) !== null) {
|
|
3895
|
+
return false
|
|
3896
|
+
}
|
|
3897
|
+
}
|
|
3886
3898
|
onPath.add(u)
|
|
3887
|
-
const ok =
|
|
3899
|
+
const ok = isArray
|
|
3888
3900
|
? u.every(recur)
|
|
3889
3901
|
: Object.keys(u).every((key) => recur((u as Record<string, unknown>)[key]))
|
|
3890
3902
|
// Pop on exit so siblings reaching the same node via a different path
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 4.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Data from "./Data.ts"
|
|
5
|
+
import * as Predicate from "./Predicate.ts"
|
|
6
|
+
import type { Issue } from "./SchemaIssue.ts"
|
|
7
|
+
|
|
8
|
+
const TypeId = "~effect/SchemaError/SchemaError"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown (or returned as the error channel value) when schema decoding
|
|
12
|
+
* or encoding fails.
|
|
13
|
+
*
|
|
14
|
+
* **Details**
|
|
15
|
+
*
|
|
16
|
+
* The `issue` field contains a structured {@link Issue} tree describing
|
|
17
|
+
* every validation failure, including the path to the problematic value,
|
|
18
|
+
* expected types, and actual values received. `message` renders the issue tree
|
|
19
|
+
* as a human-readable string.
|
|
20
|
+
*
|
|
21
|
+
* Use {@link isSchemaError} to narrow an unknown value to `SchemaError`.
|
|
22
|
+
*
|
|
23
|
+
* **Example** (Catching a SchemaError)
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { Schema } from "effect"
|
|
27
|
+
*
|
|
28
|
+
* try {
|
|
29
|
+
* Schema.decodeUnknownSync(Schema.Number)("not a number")
|
|
30
|
+
* } catch (err) {
|
|
31
|
+
* if (Schema.isSchemaError(err)) {
|
|
32
|
+
* console.log(err.message)
|
|
33
|
+
* // Expected number, actual "not a number"
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @category errors
|
|
39
|
+
* @since 4.0.0
|
|
40
|
+
*/
|
|
41
|
+
export class SchemaError extends Data.TaggedError("SchemaError")<{
|
|
42
|
+
readonly issue: Issue
|
|
43
|
+
}> {
|
|
44
|
+
readonly [TypeId]: typeof TypeId = TypeId
|
|
45
|
+
constructor(issue: Issue) {
|
|
46
|
+
super({ issue })
|
|
47
|
+
}
|
|
48
|
+
override get message() {
|
|
49
|
+
return this.issue.toString()
|
|
50
|
+
}
|
|
51
|
+
override toString() {
|
|
52
|
+
return `SchemaError(${this.message})`
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns `true` if `u` is a {@link SchemaError}.
|
|
58
|
+
*
|
|
59
|
+
* @category guards
|
|
60
|
+
* @since 4.0.0
|
|
61
|
+
*/
|
|
62
|
+
export function isSchemaError(u: unknown): u is SchemaError {
|
|
63
|
+
return Predicate.hasProperty(u, TypeId)
|
|
64
|
+
}
|
package/src/SchemaGetter.ts
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import * as DateTime from "./DateTime.ts"
|
|
15
15
|
import * as Effect from "./Effect.ts"
|
|
16
16
|
import * as Encoding from "./Encoding.ts"
|
|
17
|
+
import * as internalRecord from "./internal/record.ts"
|
|
17
18
|
import * as Option from "./Option.ts"
|
|
18
19
|
import * as Pipeable from "./Pipeable.ts"
|
|
19
20
|
import * as Predicate from "./Predicate.ts"
|
|
@@ -1703,6 +1704,20 @@ function bracketPathToTokens(bracketPath: string): Array<string | number> {
|
|
|
1703
1704
|
.map((part) => (INDEX_REGEXP.test(part) ? globalThis.Number(part) : part))
|
|
1704
1705
|
}
|
|
1705
1706
|
|
|
1707
|
+
function getOrCreateContainer(
|
|
1708
|
+
self: any,
|
|
1709
|
+
key: PropertyKey,
|
|
1710
|
+
shouldBeArray: boolean
|
|
1711
|
+
): any {
|
|
1712
|
+
const current = Object.hasOwn(self, key) ? self[key] : undefined
|
|
1713
|
+
if (current !== undefined) {
|
|
1714
|
+
return current
|
|
1715
|
+
}
|
|
1716
|
+
const container = shouldBeArray ? [] : {}
|
|
1717
|
+
internalRecord.set(self, key, container)
|
|
1718
|
+
return container
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1706
1721
|
/**
|
|
1707
1722
|
* Builds a nested tree object from a list of bracket-path entries.
|
|
1708
1723
|
*
|
|
@@ -1765,33 +1780,24 @@ export function makeTreeRecord<A>(
|
|
|
1765
1780
|
const next = tokens[i + 1]
|
|
1766
1781
|
const shouldBeArray = typeof next === "number" || next === ""
|
|
1767
1782
|
const index = cur.length
|
|
1768
|
-
|
|
1769
|
-
if (cur[index] === undefined) {
|
|
1770
|
-
cur[index] = shouldBeArray ? [] : {}
|
|
1771
|
-
}
|
|
1772
|
-
|
|
1773
|
-
cur = cur[index]
|
|
1783
|
+
cur = getOrCreateContainer(cur, index, shouldBeArray)
|
|
1774
1784
|
}
|
|
1775
1785
|
} else if (isLast) {
|
|
1776
1786
|
// If we're setting a value at a path that already exists
|
|
1777
1787
|
// convert it to an array to support multiple values for the same key
|
|
1778
|
-
|
|
1788
|
+
const hasOwn = Object.hasOwn(cur, token)
|
|
1789
|
+
if (hasOwn && Array.isArray(cur[token])) {
|
|
1779
1790
|
cur[token].push(value)
|
|
1780
|
-
} else if (
|
|
1781
|
-
cur
|
|
1791
|
+
} else if (hasOwn) {
|
|
1792
|
+
internalRecord.set(cur, token, [cur[token], value])
|
|
1782
1793
|
} else {
|
|
1783
|
-
cur
|
|
1794
|
+
internalRecord.set(cur, token, value)
|
|
1784
1795
|
}
|
|
1785
1796
|
} else {
|
|
1786
1797
|
const next = tokens[i + 1]
|
|
1787
1798
|
// if next is a number OR "" (from []), we are building an array
|
|
1788
1799
|
const shouldBeArray = typeof next === "number" || next === ""
|
|
1789
|
-
|
|
1790
|
-
if (cur[token] === undefined) {
|
|
1791
|
-
cur[token] = shouldBeArray ? [] : {}
|
|
1792
|
-
}
|
|
1793
|
-
|
|
1794
|
-
cur = cur[token]
|
|
1800
|
+
cur = getOrCreateContainer(cur, token, shouldBeArray)
|
|
1795
1801
|
}
|
|
1796
1802
|
})
|
|
1797
1803
|
})
|