@systemfsoftware/effect-schema-law 0.6.1 → 0.7.0
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/CHANGELOG.md +15 -0
- package/LICENSE +203 -21
- package/dist/effect-schema-law.d.ts +127 -3
- package/dist/index.d.ts +121 -6
- package/dist/index.mjs +352 -21
- package/package.json +14 -14
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @systemfsoftware/effect-schema-law
|
|
2
|
+
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- cut over to effect v4 (4.0.0-rc.108): public surface derives from effect types; peers flip effect ^3→^4
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Array types are spelled one way. `Array<T>` and `ReadonlyArray<T>` in emitted
|
|
12
|
+
declarations become `T[]` and `readonly T[]`, which the type checker cannot tell
|
|
13
|
+
apart: no exported type changes, only how it is written.
|
|
14
|
+
|
|
15
|
+
- New version is published through npm trusted publishing, so it carries a provenance attestation you can verify.
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,203 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Copyright (c) 2026 Ryan Lee (systemfsoftware)
|
|
2
|
+
|
|
3
|
+
Apache License
|
|
4
|
+
Version 2.0, January 2004
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
12
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
13
|
+
|
|
14
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
15
|
+
the copyright owner that is granting the License.
|
|
16
|
+
|
|
17
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
18
|
+
other entities that control, are controlled by, or are under common
|
|
19
|
+
control with that entity. For the purposes of this definition,
|
|
20
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
21
|
+
direction or management of such entity, whether by contract or
|
|
22
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
23
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
24
|
+
|
|
25
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
26
|
+
exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
29
|
+
including but not limited to software source code, documentation
|
|
30
|
+
source, and configuration files.
|
|
31
|
+
|
|
32
|
+
"Object" form shall mean any form resulting from mechanical
|
|
33
|
+
transformation or translation of a Source form, including but
|
|
34
|
+
not limited to compiled object code, generated documentation,
|
|
35
|
+
and conversions to other media types.
|
|
36
|
+
|
|
37
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
38
|
+
Object form, made available under the License, as indicated by a
|
|
39
|
+
copyright notice that is included in or attached to the work
|
|
40
|
+
(an example is provided in the Appendix below).
|
|
41
|
+
|
|
42
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
43
|
+
form, that is based on (or derived from) the Work and for which the
|
|
44
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
45
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
46
|
+
of this License, Derivative Works shall not include works that remain
|
|
47
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
48
|
+
the Work and Derivative Works thereof.
|
|
49
|
+
|
|
50
|
+
"Contribution" shall mean any work of authorship, including
|
|
51
|
+
the original version of the Work and any modifications or additions
|
|
52
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
53
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
54
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
55
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
56
|
+
means any form of electronic, verbal, or written communication sent
|
|
57
|
+
to the Licensor or its representatives, including but not limited to
|
|
58
|
+
communication on electronic mailing lists, source code control systems,
|
|
59
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
60
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
61
|
+
excluding communication that is conspicuously marked or otherwise
|
|
62
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
63
|
+
|
|
64
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
65
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
66
|
+
subsequently incorporated within the Work.
|
|
67
|
+
|
|
68
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
69
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
70
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
71
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
72
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
73
|
+
Work and such Derivative Works in Source or Object form.
|
|
74
|
+
|
|
75
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
76
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
77
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
78
|
+
(except as stated in this section) patent license to make, have made,
|
|
79
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
80
|
+
where such license applies only to those patent claims licensable
|
|
81
|
+
by such Contributor that are necessarily infringed by their
|
|
82
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
83
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
84
|
+
institute patent litigation against any entity (including a
|
|
85
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
86
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
87
|
+
or contributory patent infringement, then any patent licenses
|
|
88
|
+
granted to You under this License for that Work shall terminate
|
|
89
|
+
as of the date such litigation is filed.
|
|
90
|
+
|
|
91
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
92
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
93
|
+
modifications, and in Source or Object form, provided that You
|
|
94
|
+
meet the following conditions:
|
|
95
|
+
|
|
96
|
+
(a) You must give any other recipients of the Work or
|
|
97
|
+
Derivative Works a copy of this License; and
|
|
98
|
+
|
|
99
|
+
(b) You must cause any modified files to carry prominent notices
|
|
100
|
+
stating that You changed the files; and
|
|
101
|
+
|
|
102
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
103
|
+
that You distribute, all copyright, patent, trademark, and
|
|
104
|
+
attribution notices from the Source form of the Work,
|
|
105
|
+
excluding those notices that do not pertain to any part of
|
|
106
|
+
the Derivative Works; and
|
|
107
|
+
|
|
108
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
109
|
+
distribution, then any Derivative Works that You distribute must
|
|
110
|
+
include a readable copy of the attribution notices contained
|
|
111
|
+
within such NOTICE file, excluding those notices that do not
|
|
112
|
+
pertain to any part of the Derivative Works, in at least one
|
|
113
|
+
of the following places: within a NOTICE text file distributed
|
|
114
|
+
as part of the Derivative Works; within the Source form or
|
|
115
|
+
documentation, if provided along with the Derivative Works; or,
|
|
116
|
+
within a display generated by the Derivative Works, if and
|
|
117
|
+
wherever such third-party notices normally appear. The contents
|
|
118
|
+
of the NOTICE file are for informational purposes only and
|
|
119
|
+
do not modify the License. You may add Your own attribution
|
|
120
|
+
notices within Derivative Works that You distribute, alongside
|
|
121
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
122
|
+
that such additional attribution notices cannot be construed
|
|
123
|
+
as modifying the License.
|
|
124
|
+
|
|
125
|
+
You may add Your own copyright statement to Your modifications and
|
|
126
|
+
may provide additional or different license terms and conditions
|
|
127
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
128
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
129
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
130
|
+
the conditions stated in this License.
|
|
131
|
+
|
|
132
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
133
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
134
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
135
|
+
this License, without any additional terms or conditions.
|
|
136
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
137
|
+
the terms of any separate license agreement you may have executed
|
|
138
|
+
with Licensor regarding such Contributions.
|
|
139
|
+
|
|
140
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
141
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
142
|
+
except as required for reasonable and customary use in describing the
|
|
143
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
144
|
+
|
|
145
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
146
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
147
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
148
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
149
|
+
implied, including, without limitation, any warranties or conditions
|
|
150
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
151
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
152
|
+
appropriateness of using or redistributing the Work and assume any
|
|
153
|
+
risks associated with Your exercise of permissions under this License.
|
|
154
|
+
|
|
155
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
156
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
157
|
+
unless required by applicable law (such as deliberate and grossly
|
|
158
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
159
|
+
liable to You for damages, including any direct, indirect, special,
|
|
160
|
+
incidental, or consequential damages of any character arising as a
|
|
161
|
+
result of this License or out of the use or inability to use the
|
|
162
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
163
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
164
|
+
other commercial damages or losses), even if such Contributor
|
|
165
|
+
has been advised of the possibility of such damages.
|
|
166
|
+
|
|
167
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
168
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
169
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
170
|
+
or other liability obligations and/or rights consistent with this
|
|
171
|
+
License. However, in accepting such obligations, You may act only
|
|
172
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
173
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
174
|
+
defend, and hold each Contributor harmless for any liability
|
|
175
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
176
|
+
of your accepting any such warranty or additional liability.
|
|
177
|
+
|
|
178
|
+
END OF TERMS AND CONDITIONS
|
|
179
|
+
|
|
180
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
181
|
+
|
|
182
|
+
To apply the Apache License to your work, attach the following
|
|
183
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
184
|
+
replaced with your own identifying information. (Don't include
|
|
185
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
186
|
+
comment syntax for the file format. We also recommend that a
|
|
187
|
+
file or class name and description of purpose be included on the
|
|
188
|
+
same "printed page" as the copyright notice for easier
|
|
189
|
+
identification within third-party archives.
|
|
190
|
+
|
|
191
|
+
Copyright 2026 Ryan Lee (systemfsoftware)
|
|
192
|
+
|
|
193
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
|
+
you may not use this file except in compliance with the License.
|
|
195
|
+
You may obtain a copy of the License at
|
|
196
|
+
|
|
197
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
198
|
+
|
|
199
|
+
Unless required by applicable law or agreed to in writing, software
|
|
200
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
201
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
202
|
+
See the License for the specific language governing permissions and
|
|
203
|
+
limitations under the License.
|
|
@@ -1,10 +1,117 @@
|
|
|
1
|
+
import * as AST from 'effect/SchemaAST';
|
|
2
|
+
import { FastCheck } from 'effect/testing';
|
|
1
3
|
import { Schema } from 'effect';
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
/** Verdict for one schema's obligation set, carrying the detail R7 requires in a failure. */
|
|
6
|
+
export declare interface AdequacyReport {
|
|
7
|
+
readonly adequate: boolean;
|
|
8
|
+
readonly undischarged: readonly Obligation[];
|
|
9
|
+
readonly message: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Which obligations no declared generator discharges. A bare "adequacy failed" leaves
|
|
14
|
+
* the author nothing to act on, so the message names each node's tag, every path
|
|
15
|
+
* reaching it, and the witness that proves the weakening is permissive.
|
|
16
|
+
*/
|
|
17
|
+
export declare const adequacyReport: (schema: Schema.ConstraintDecoder<unknown>, generators: RefusalGenerators) => AdequacyReport;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* One weakening of an Effect schema, produced by `armsOf`. Each arm identifies
|
|
21
|
+
* the AST node it removes; the rebuilt tree is the surrounding schema with
|
|
22
|
+
* that node replaced by its child or, for a dropped refinement, by the same
|
|
23
|
+
* node without that check.
|
|
24
|
+
*
|
|
25
|
+
* Two schemas reaching the same `node` share its identity and therefore its
|
|
26
|
+
* obligation key — the deduplication is the entire point of the shape.
|
|
27
|
+
*/
|
|
28
|
+
export declare interface Arm {
|
|
29
|
+
readonly kind: 'drop-refinement' | 'drop-to-arm' | 'drop-from-arm';
|
|
30
|
+
readonly path: string;
|
|
31
|
+
/** The node this arm removes — the obligation key. Two arms removing it are one obligation. */
|
|
32
|
+
readonly node: AST.AST;
|
|
33
|
+
readonly weakened: AST.AST;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Walk an Effect schema's AST and return every weakenable arm, recursively
|
|
38
|
+
* through `Objects`, `Union`, `Arrays`, `Declaration`, and `Suspend`, plus
|
|
39
|
+
* the v4 per-node `Checks` (refinements) and encoding `Link` chains
|
|
40
|
+
* (transformations). The walk terminates on `Suspend` cycles at `depthCap`
|
|
41
|
+
* levels; every other AST tag is a leaf whose children cannot hold a
|
|
42
|
+
* refinement, and `Union` over structurally identical members is fine because
|
|
43
|
+
* the walk keys arms by node identity, not shape.
|
|
44
|
+
*
|
|
45
|
+
* The arm's `node` is the AST node its weakening removes; reference identity
|
|
46
|
+
* is the obligation key (R3). `weakened` is the enclosing tree with `node`
|
|
47
|
+
* replaced by its child, ready to be passed to `Schema.make`.
|
|
48
|
+
*/
|
|
49
|
+
export declare const armsOf: (schema: Schema.ConstraintDecoder<unknown>) => readonly Arm[];
|
|
50
|
+
|
|
51
|
+
/** An arm no source could draw for: the search could not look, which is not the same as finding nothing. */
|
|
52
|
+
export declare interface BlindArm {
|
|
53
|
+
readonly path: string;
|
|
54
|
+
readonly kind: string;
|
|
55
|
+
readonly message: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export declare const boundedUnion: <Base extends readonly [Schema.ConstraintCodec<unknown, unknown>, ...readonly Schema.ConstraintCodec<unknown, unknown>[]], Recur extends readonly [Schema.ConstraintCodec<unknown, unknown>, ...readonly Schema.ConstraintCodec<unknown, unknown>[]]>(identifier: string, options: {
|
|
4
59
|
readonly base: Base;
|
|
5
60
|
readonly recur: Recur;
|
|
6
61
|
readonly maxDepth?: number;
|
|
7
|
-
}) => Schema.
|
|
62
|
+
}) => Schema.Codec<Base[number]['Type'] | Recur[number]['Type'], Base[number]['Encoded'] | Recur[number]['Encoded']>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* For each obligation, return the names of the generators whose draws
|
|
66
|
+
* the weakened schema accepts AND the original rejects. Generators
|
|
67
|
+
* whose draws the original also accepts do not discharge — they are
|
|
68
|
+
* the language the original already covers. Generators whose draws
|
|
69
|
+
* the weakened schema rejects do not discharge either.
|
|
70
|
+
*
|
|
71
|
+
* The original schema is part of the signature because discharge is
|
|
72
|
+
* defined relative to it.
|
|
73
|
+
*/
|
|
74
|
+
export declare const dischargedBy: (schema: Schema.ConstraintDecoder<unknown>, obligations: ReadonlyMap<AST.AST, Obligation>, generators: Readonly<Record<string, NamedArbitrary>>) => ReadonlyMap<AST.AST, readonly string[]>;
|
|
75
|
+
|
|
76
|
+
/** True when `value` is a witness for some obligation: accepted by that weakening, rejected by the schema. */
|
|
77
|
+
export declare const discriminates: (schema: Schema.ConstraintDecoder<unknown>, obligations: ReadonlyMap<AST.AST, Obligation>, value: unknown) => boolean;
|
|
78
|
+
|
|
79
|
+
declare type NamedArbitrary = FastCheck.Arbitrary<unknown>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One node-keyed refutation obligation: a witness exists that the
|
|
83
|
+
* weakened schema accepts and the original rejects. Obligations are
|
|
84
|
+
* keyed by the AST node the arm removes (R3); several paths reaching
|
|
85
|
+
* one node are one obligation carrying several paths.
|
|
86
|
+
*/
|
|
87
|
+
export declare interface Obligation {
|
|
88
|
+
readonly node: AST.AST;
|
|
89
|
+
readonly tag: string;
|
|
90
|
+
readonly paths: readonly string[];
|
|
91
|
+
readonly weakened: AST.AST;
|
|
92
|
+
readonly witness: unknown;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Every arm of a schema, split into the ones a witness proved and the ones nothing could see. */
|
|
96
|
+
export declare interface ObligationScan {
|
|
97
|
+
readonly obligations: ReadonlyMap<AST.AST, Obligation>;
|
|
98
|
+
readonly blind: readonly BlindArm[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export declare const obligationsOf: (schema: Schema.ConstraintDecoder<unknown>) => ReadonlyMap<AST.AST, Obligation>;
|
|
102
|
+
|
|
103
|
+
/** Named refusal generators: each draws rejection-class inputs the schema must reject. */
|
|
104
|
+
export declare type RefusalGenerators = Record<string, FastCheck.Arbitrary<unknown>>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Property-test one schema's rejection contract. Registers per generator a refusal
|
|
108
|
+
* property (the schema rejects every draw) and a discrimination property (each draw is
|
|
109
|
+
* explained by some weakening), plus one adequacy property for the schema.
|
|
110
|
+
*
|
|
111
|
+
* Names follow the house convention: `∀b_<generator>_⊥`, `∀g_<generator>_discriminates`,
|
|
112
|
+
* `∀s_<schema>_adequate` — disjoint from `ruleOfSchemas`' `∀x_<name>_=x` pair.
|
|
113
|
+
*/
|
|
114
|
+
export declare const refutes: (schema: Schema.ConstraintDecoder<unknown>, generators: RefusalGenerators) => void;
|
|
8
115
|
|
|
9
116
|
/**
|
|
10
117
|
* Property-test the round-trip and encode-stability laws of any Effect Schema.
|
|
@@ -15,6 +122,23 @@ export declare const boundedUnion: <Base extends readonly [Schema.Schema.Any, ..
|
|
|
15
122
|
*
|
|
16
123
|
* Use inside a `describe` block to scope the generated tests.
|
|
17
124
|
*/
|
|
18
|
-
export declare const ruleOfSchemas: <A, I>(name: string, schema: Schema.
|
|
125
|
+
export declare const ruleOfSchemas: <A, I>(name: string, schema: Schema.Codec<A, I>) => void;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Walk `armsOf` and classify every arm. An arm is an obligation iff a witness
|
|
129
|
+
* exists — an input the weakened schema accepts and the original rejects —
|
|
130
|
+
* drawn from a fallback chain (encoded arbitrary, type arbitrary, generic
|
|
131
|
+
* pool). An arm whose every source failed to construct is `blind`: "no
|
|
132
|
+
* obligation" and "could not look" are different answers, and collapsing
|
|
133
|
+
* them is the silent miss this scan exists to prevent.
|
|
134
|
+
*/
|
|
135
|
+
export declare const scanObligations: (schema: Schema.ConstraintDecoder<unknown>) => ObligationScan;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The sampling budget is a property of the contract, not a knob. The
|
|
139
|
+
* plan pins the expectation against a literal table; the kernel draws
|
|
140
|
+
* up to this many candidates per source before exhausting the chain.
|
|
141
|
+
*/
|
|
142
|
+
export declare const WITNESS_BUDGET = 256;
|
|
19
143
|
|
|
20
144
|
export { }
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,95 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import { FastCheck } from "effect/testing";
|
|
3
|
+
import * as AST from "effect/SchemaAST";
|
|
4
|
+
//#region src/BoundedUnion.d.ts
|
|
5
|
+
declare const boundedUnion: <Base extends readonly [Schema.ConstraintCodec<unknown, unknown>, ...readonly Schema.ConstraintCodec<unknown, unknown>[]], Recur extends readonly [Schema.ConstraintCodec<unknown, unknown>, ...readonly Schema.ConstraintCodec<unknown, unknown>[]]>(identifier: string, options: {
|
|
4
6
|
readonly base: Base;
|
|
5
7
|
readonly recur: Recur;
|
|
6
8
|
readonly maxDepth?: number;
|
|
7
|
-
}) => Schema.
|
|
9
|
+
}) => Schema.Codec<Base[number]['Type'] | Recur[number]['Type'], Base[number]['Encoded'] | Recur[number]['Encoded']>;
|
|
8
10
|
//#endregion
|
|
9
|
-
//#region src/
|
|
11
|
+
//#region src/Refutation.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* One node-keyed refutation obligation: a witness exists that the
|
|
14
|
+
* weakened schema accepts and the original rejects. Obligations are
|
|
15
|
+
* keyed by the AST node the arm removes (R3); several paths reaching
|
|
16
|
+
* one node are one obligation carrying several paths.
|
|
17
|
+
*/
|
|
18
|
+
interface Obligation {
|
|
19
|
+
readonly node: AST.AST;
|
|
20
|
+
readonly tag: string;
|
|
21
|
+
readonly paths: readonly string[];
|
|
22
|
+
readonly weakened: AST.AST;
|
|
23
|
+
readonly witness: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** An arm no source could draw for: the search could not look, which is not the same as finding nothing. */
|
|
26
|
+
interface BlindArm {
|
|
27
|
+
readonly path: string;
|
|
28
|
+
readonly kind: string;
|
|
29
|
+
readonly message: string;
|
|
30
|
+
}
|
|
31
|
+
/** Every arm of a schema, split into the ones a witness proved and the ones nothing could see. */
|
|
32
|
+
interface ObligationScan {
|
|
33
|
+
readonly obligations: ReadonlyMap<AST.AST, Obligation>;
|
|
34
|
+
readonly blind: readonly BlindArm[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The sampling budget is a property of the contract, not a knob. The
|
|
38
|
+
* plan pins the expectation against a literal table; the kernel draws
|
|
39
|
+
* up to this many candidates per source before exhausting the chain.
|
|
40
|
+
*/
|
|
41
|
+
declare const WITNESS_BUDGET = 256;
|
|
42
|
+
type NamedArbitrary = FastCheck.Arbitrary<unknown>;
|
|
43
|
+
/**
|
|
44
|
+
* Walk `armsOf` and classify every arm. An arm is an obligation iff a witness
|
|
45
|
+
* exists — an input the weakened schema accepts and the original rejects —
|
|
46
|
+
* drawn from a fallback chain (encoded arbitrary, type arbitrary, generic
|
|
47
|
+
* pool). An arm whose every source failed to construct is `blind`: "no
|
|
48
|
+
* obligation" and "could not look" are different answers, and collapsing
|
|
49
|
+
* them is the silent miss this scan exists to prevent.
|
|
50
|
+
*/
|
|
51
|
+
declare const scanObligations: (schema: Schema.ConstraintDecoder<unknown>) => ObligationScan;
|
|
52
|
+
declare const obligationsOf: (schema: Schema.ConstraintDecoder<unknown>) => ReadonlyMap<AST.AST, Obligation>;
|
|
53
|
+
/**
|
|
54
|
+
* For each obligation, return the names of the generators whose draws
|
|
55
|
+
* the weakened schema accepts AND the original rejects. Generators
|
|
56
|
+
* whose draws the original also accepts do not discharge — they are
|
|
57
|
+
* the language the original already covers. Generators whose draws
|
|
58
|
+
* the weakened schema rejects do not discharge either.
|
|
59
|
+
*
|
|
60
|
+
* The original schema is part of the signature because discharge is
|
|
61
|
+
* defined relative to it.
|
|
62
|
+
*/
|
|
63
|
+
declare const dischargedBy: (schema: Schema.ConstraintDecoder<unknown>, obligations: ReadonlyMap<AST.AST, Obligation>, generators: Readonly<Record<string, NamedArbitrary>>) => ReadonlyMap<AST.AST, readonly string[]>;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/Refutes.d.ts
|
|
66
|
+
/** Named refusal generators: each draws rejection-class inputs the schema must reject. */
|
|
67
|
+
type RefusalGenerators = Record<string, FastCheck.Arbitrary<unknown>>;
|
|
68
|
+
/** Verdict for one schema's obligation set, carrying the detail R7 requires in a failure. */
|
|
69
|
+
interface AdequacyReport {
|
|
70
|
+
readonly adequate: boolean;
|
|
71
|
+
readonly undischarged: readonly Obligation[];
|
|
72
|
+
readonly message: string;
|
|
73
|
+
}
|
|
74
|
+
/** True when `value` is a witness for some obligation: accepted by that weakening, rejected by the schema. */
|
|
75
|
+
declare const discriminates: (schema: Schema.ConstraintDecoder<unknown>, obligations: ReadonlyMap<AST.AST, Obligation>, value: unknown) => boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Which obligations no declared generator discharges. A bare "adequacy failed" leaves
|
|
78
|
+
* the author nothing to act on, so the message names each node's tag, every path
|
|
79
|
+
* reaching it, and the witness that proves the weakening is permissive.
|
|
80
|
+
*/
|
|
81
|
+
declare const adequacyReport: (schema: Schema.ConstraintDecoder<unknown>, generators: RefusalGenerators) => AdequacyReport;
|
|
82
|
+
/**
|
|
83
|
+
* Property-test one schema's rejection contract. Registers per generator a refusal
|
|
84
|
+
* property (the schema rejects every draw) and a discrimination property (each draw is
|
|
85
|
+
* explained by some weakening), plus one adequacy property for the schema.
|
|
86
|
+
*
|
|
87
|
+
* Names follow the house convention: `∀b_<generator>_⊥`, `∀g_<generator>_discriminates`,
|
|
88
|
+
* `∀s_<schema>_adequate` — disjoint from `ruleOfSchemas`' `∀x_<name>_=x` pair.
|
|
89
|
+
*/
|
|
90
|
+
declare const refutes: (schema: Schema.ConstraintDecoder<unknown>, generators: RefusalGenerators) => void;
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/RuleOfSchemas.d.ts
|
|
10
93
|
/**
|
|
11
94
|
* Property-test the round-trip and encode-stability laws of any Effect Schema.
|
|
12
95
|
*
|
|
@@ -16,6 +99,38 @@ declare const boundedUnion: <Base extends readonly [Schema.Schema.Any, ...Readon
|
|
|
16
99
|
*
|
|
17
100
|
* Use inside a `describe` block to scope the generated tests.
|
|
18
101
|
*/
|
|
19
|
-
declare const ruleOfSchemas: <A, I>(name: string, schema: Schema.
|
|
102
|
+
declare const ruleOfSchemas: <A, I>(name: string, schema: Schema.Codec<A, I>) => void;
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/Weaken.d.ts
|
|
105
|
+
/**
|
|
106
|
+
* One weakening of an Effect schema, produced by `armsOf`. Each arm identifies
|
|
107
|
+
* the AST node it removes; the rebuilt tree is the surrounding schema with
|
|
108
|
+
* that node replaced by its child or, for a dropped refinement, by the same
|
|
109
|
+
* node without that check.
|
|
110
|
+
*
|
|
111
|
+
* Two schemas reaching the same `node` share its identity and therefore its
|
|
112
|
+
* obligation key — the deduplication is the entire point of the shape.
|
|
113
|
+
*/
|
|
114
|
+
interface Arm {
|
|
115
|
+
readonly kind: 'drop-refinement' | 'drop-to-arm' | 'drop-from-arm';
|
|
116
|
+
readonly path: string;
|
|
117
|
+
/** The node this arm removes — the obligation key. Two arms removing it are one obligation. */
|
|
118
|
+
readonly node: AST.AST;
|
|
119
|
+
readonly weakened: AST.AST;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Walk an Effect schema's AST and return every weakenable arm, recursively
|
|
123
|
+
* through `Objects`, `Union`, `Arrays`, `Declaration`, and `Suspend`, plus
|
|
124
|
+
* the v4 per-node `Checks` (refinements) and encoding `Link` chains
|
|
125
|
+
* (transformations). The walk terminates on `Suspend` cycles at `depthCap`
|
|
126
|
+
* levels; every other AST tag is a leaf whose children cannot hold a
|
|
127
|
+
* refinement, and `Union` over structurally identical members is fine because
|
|
128
|
+
* the walk keys arms by node identity, not shape.
|
|
129
|
+
*
|
|
130
|
+
* The arm's `node` is the AST node its weakening removes; reference identity
|
|
131
|
+
* is the obligation key (R3). `weakened` is the enclosing tree with `node`
|
|
132
|
+
* replaced by its child, ready to be passed to `Schema.make`.
|
|
133
|
+
*/
|
|
134
|
+
declare const armsOf: (schema: Schema.ConstraintDecoder<unknown>) => readonly Arm[];
|
|
20
135
|
//#endregion
|
|
21
|
-
export { boundedUnion, ruleOfSchemas };
|
|
136
|
+
export { type AdequacyReport, type Arm, type BlindArm, type Obligation, type ObligationScan, type RefusalGenerators, WITNESS_BUDGET, adequacyReport, armsOf, boundedUnion, dischargedBy, discriminates, obligationsOf, refutes, ruleOfSchemas, scanObligations };
|
package/dist/index.mjs
CHANGED
|
@@ -1,20 +1,350 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Exit, Option, Result, Schema } from "effect";
|
|
2
|
+
import { FastCheck } from "effect/testing";
|
|
3
|
+
import * as AST from "effect/SchemaAST";
|
|
2
4
|
import { it } from "@effect/vitest";
|
|
3
|
-
|
|
5
|
+
import { expect } from "vitest";
|
|
6
|
+
//#region src/BoundedUnion.ts
|
|
4
7
|
const boundedUnion = (identifier, options) => {
|
|
5
8
|
const { base, maxDepth = 2, recur } = options;
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
+
let deriving = false;
|
|
10
|
+
const hook = () => (fc, _context) => {
|
|
11
|
+
const baseArbitraries = fc.oneof(...base.map((member) => Schema.toArbitrary(member)(fc)));
|
|
12
|
+
if (deriving) return baseArbitraries;
|
|
13
|
+
deriving = true;
|
|
14
|
+
try {
|
|
15
|
+
const recurArbitraries = recur.map((member) => Schema.toArbitrary(member)(fc));
|
|
16
|
+
return {
|
|
17
|
+
arbitrary: fc.oneof({
|
|
18
|
+
depthIdentifier: identifier,
|
|
19
|
+
maxDepth
|
|
20
|
+
}, baseArbitraries, ...recurArbitraries),
|
|
21
|
+
terminal: baseArbitraries
|
|
22
|
+
};
|
|
23
|
+
} finally {
|
|
24
|
+
deriving = false;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
return Schema.Union([...base, ...recur]).annotate({
|
|
9
28
|
identifier,
|
|
10
|
-
|
|
11
|
-
depthIdentifier: identifier,
|
|
12
|
-
maxDepth
|
|
13
|
-
}, fc.oneof(...baseArbitraries), ...recurArbitraries)
|
|
29
|
+
toArbitrary: hook
|
|
14
30
|
});
|
|
15
31
|
};
|
|
16
32
|
//#endregion
|
|
17
|
-
//#region src/
|
|
33
|
+
//#region src/Weaken.ts
|
|
34
|
+
const DEFAULT_SUSPEND_DEPTH_CAP = 16;
|
|
35
|
+
/**
|
|
36
|
+
* Walk an Effect schema's AST and return every weakenable arm, recursively
|
|
37
|
+
* through `Objects`, `Union`, `Arrays`, `Declaration`, and `Suspend`, plus
|
|
38
|
+
* the v4 per-node `Checks` (refinements) and encoding `Link` chains
|
|
39
|
+
* (transformations). The walk terminates on `Suspend` cycles at `depthCap`
|
|
40
|
+
* levels; every other AST tag is a leaf whose children cannot hold a
|
|
41
|
+
* refinement, and `Union` over structurally identical members is fine because
|
|
42
|
+
* the walk keys arms by node identity, not shape.
|
|
43
|
+
*
|
|
44
|
+
* The arm's `node` is the AST node its weakening removes; reference identity
|
|
45
|
+
* is the obligation key (R3). `weakened` is the enclosing tree with `node`
|
|
46
|
+
* replaced by its child, ready to be passed to `Schema.make`.
|
|
47
|
+
*/
|
|
48
|
+
const armsOf = (schema) => {
|
|
49
|
+
const out = [];
|
|
50
|
+
const visited = /* @__PURE__ */ new Set();
|
|
51
|
+
walk(schema.ast, "root", out, 0, DEFAULT_SUSPEND_DEPTH_CAP, (replacement) => replacement, visited);
|
|
52
|
+
return out;
|
|
53
|
+
};
|
|
54
|
+
const replaceAt = (items, index, item) => items.map((existing, i) => i === index ? item : existing);
|
|
55
|
+
/**
|
|
56
|
+
* Clone an AST node, patching selected own properties. This mirrors the v4
|
|
57
|
+
* `SchemaAST` internals (which rebuild checked nodes the same way on
|
|
58
|
+
* `Schema.check`) without depending on non-public exports.
|
|
59
|
+
*/
|
|
60
|
+
const cloneWith = (node, patch) => {
|
|
61
|
+
const target = Object.assign({}, node);
|
|
62
|
+
Object.setPrototypeOf(target, Reflect.getPrototypeOf(node));
|
|
63
|
+
Object.assign(target, patch);
|
|
64
|
+
return target;
|
|
65
|
+
};
|
|
66
|
+
/** The AST with one refinement check removed — dropping a refinement in v4. */
|
|
67
|
+
const dropCheck = (node, check) => {
|
|
68
|
+
const checks = node.checks;
|
|
69
|
+
if (checks === void 0) return node;
|
|
70
|
+
const rest = checks.filter((existing) => existing !== check);
|
|
71
|
+
return cloneWith(node, { checks: rest.length === 0 ? void 0 : rest });
|
|
72
|
+
};
|
|
73
|
+
/** The AST with its encoding chain removed — the decoded (type-side) view. */
|
|
74
|
+
const withoutEncoding = (node) => node.encoding === void 0 ? node : cloneWith(node, { encoding: void 0 });
|
|
75
|
+
/** The AST with the encoding link at `index` retargeted at `to`. */
|
|
76
|
+
const replaceLinkAt = (node, index, to) => {
|
|
77
|
+
const encoding = node.encoding;
|
|
78
|
+
if (encoding === void 0) return node;
|
|
79
|
+
const link = encoding[index];
|
|
80
|
+
if (link === void 0 || link.to === to) return node;
|
|
81
|
+
const next = [
|
|
82
|
+
...encoding.slice(0, index),
|
|
83
|
+
new AST.Link(to, link.transformation),
|
|
84
|
+
...encoding.slice(index + 1)
|
|
85
|
+
];
|
|
86
|
+
return cloneWith(node, { encoding: next });
|
|
87
|
+
};
|
|
88
|
+
const walk = (node, path, out, suspendDepth, depthCap, rebuild, visited) => {
|
|
89
|
+
const isNewNode = !visited.has(node);
|
|
90
|
+
if (isNewNode) visited.add(node);
|
|
91
|
+
if (node.checks) node.checks.forEach((check, index) => {
|
|
92
|
+
out.push({
|
|
93
|
+
kind: "drop-refinement",
|
|
94
|
+
path: `${path}/refinement/${index}`,
|
|
95
|
+
node,
|
|
96
|
+
weakened: rebuild(dropCheck(node, check))
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
if (node.encoding !== void 0) node.encoding.forEach((link, index) => {
|
|
100
|
+
out.push({
|
|
101
|
+
kind: "drop-to-arm",
|
|
102
|
+
path: `${path}/to/${index}`,
|
|
103
|
+
node,
|
|
104
|
+
weakened: rebuild(link.to)
|
|
105
|
+
});
|
|
106
|
+
out.push({
|
|
107
|
+
kind: "drop-from-arm",
|
|
108
|
+
path: `${path}/from/${index}`,
|
|
109
|
+
node,
|
|
110
|
+
weakened: rebuild(withoutEncoding(node))
|
|
111
|
+
});
|
|
112
|
+
walk(link.to, `${path}/to/${index}`, out, suspendDepth, depthCap, (replacement) => rebuild(replaceLinkAt(node, index, replacement)), visited);
|
|
113
|
+
});
|
|
114
|
+
if (!isNewNode) return;
|
|
115
|
+
if (AST.isObjects(node)) {
|
|
116
|
+
node.propertySignatures.forEach((property, i) => {
|
|
117
|
+
walk(property.type, `${path}/property/${String(property.name)}`, out, suspendDepth, depthCap, (replacement) => rebuild(new AST.Objects(replaceAt(node.propertySignatures, i, new AST.PropertySignature(property.name, replacement)), node.indexSignatures, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks)), visited);
|
|
118
|
+
});
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (AST.isUnion(node)) {
|
|
122
|
+
node.types.forEach((member, i) => {
|
|
123
|
+
walk(member, `${path}/union/${i}`, out, suspendDepth, depthCap, (replacement) => rebuild(new AST.Union(replaceAt(node.types, i, replacement), node.mode, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks)), visited);
|
|
124
|
+
});
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (AST.isArrays(node)) {
|
|
128
|
+
node.elements.forEach((element, i) => {
|
|
129
|
+
walk(element, `${path}/element/${i}`, out, suspendDepth, depthCap, (replacement) => rebuild(new AST.Arrays(node.isMutable, replaceAt(node.elements, i, replacement), node.rest, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks)), visited);
|
|
130
|
+
});
|
|
131
|
+
node.rest.forEach((rest, i) => {
|
|
132
|
+
walk(rest, `${path}/rest/${i}`, out, suspendDepth, depthCap, (replacement) => rebuild(new AST.Arrays(node.isMutable, node.elements, replaceAt(node.rest, i, replacement), node.annotations, node.checks, node.encoding, node.context, node.encodingChecks)), visited);
|
|
133
|
+
});
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (AST.isDeclaration(node)) {
|
|
137
|
+
node.typeParameters.forEach((parameter, i) => {
|
|
138
|
+
walk(parameter, `${path}/typeParameter/${i}`, out, suspendDepth, depthCap, (replacement) => rebuild(new AST.Declaration(replaceAt(node.typeParameters, i, replacement), node.run, node.annotations, node.checks, node.encoding, node.context, node.encodingChecks)), visited);
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (AST.isSuspend(node)) {
|
|
143
|
+
if (suspendDepth >= depthCap) return;
|
|
144
|
+
walk(node.thunk(), `${path}/suspend`, out, suspendDepth + 1, depthCap, (replacement) => rebuild(new AST.Suspend(() => replacement, node.annotations, node.checks, node.encoding, node.context)), visited);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/Refutation.ts
|
|
149
|
+
/**
|
|
150
|
+
* The sampling budget is a property of the contract, not a knob. The
|
|
151
|
+
* plan pins the expectation against a literal table; the kernel draws
|
|
152
|
+
* up to this many candidates per source before exhausting the chain.
|
|
153
|
+
*/
|
|
154
|
+
const WITNESS_BUDGET = 256;
|
|
155
|
+
const REJECTION_GENERIC_POOL = [
|
|
156
|
+
"",
|
|
157
|
+
" ",
|
|
158
|
+
"g",
|
|
159
|
+
"0",
|
|
160
|
+
"1",
|
|
161
|
+
-1,
|
|
162
|
+
0,
|
|
163
|
+
1,
|
|
164
|
+
true,
|
|
165
|
+
false,
|
|
166
|
+
null,
|
|
167
|
+
void 0,
|
|
168
|
+
{},
|
|
169
|
+
[],
|
|
170
|
+
{ _: "unknown" },
|
|
171
|
+
["unknown"],
|
|
172
|
+
NaN,
|
|
173
|
+
Number.POSITIVE_INFINITY,
|
|
174
|
+
"not-a-date"
|
|
175
|
+
];
|
|
176
|
+
/** Decode-side, not validate-side: refusal generators draw wire-form inputs, so this is a question about the Encoded side. */
|
|
177
|
+
const accepts = (schema, value) => Exit.isSuccess(Schema.decodeUnknownExit(schema)(value));
|
|
178
|
+
const buildArbitrary = (schema) => {
|
|
179
|
+
try {
|
|
180
|
+
return Schema.toArbitrary(schema)(FastCheck);
|
|
181
|
+
} catch {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
const sample = (arbitrary, budget) => {
|
|
186
|
+
try {
|
|
187
|
+
return FastCheck.sample(arbitrary, {
|
|
188
|
+
numRuns: budget,
|
|
189
|
+
seed: 1
|
|
190
|
+
});
|
|
191
|
+
} catch {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const findWitness = (schema, arm) => {
|
|
196
|
+
const weakened = Schema.make(arm.weakened);
|
|
197
|
+
const isWitness = (value) => accepts(weakened, value) && !accepts(schema, value);
|
|
198
|
+
let schemaDerivedDraws = 0;
|
|
199
|
+
for (const source of [Schema.toEncoded(weakened), weakened]) {
|
|
200
|
+
const arbitrary = buildArbitrary(source);
|
|
201
|
+
if (arbitrary === void 0) continue;
|
|
202
|
+
const draws = sample(arbitrary, 256);
|
|
203
|
+
if (draws === void 0) continue;
|
|
204
|
+
schemaDerivedDraws += draws.length;
|
|
205
|
+
for (const value of draws) if (isWitness(value)) return Result.succeed(Option.some(value));
|
|
206
|
+
}
|
|
207
|
+
for (const value of REJECTION_GENERIC_POOL) if (isWitness(value)) return Result.succeed(Option.some(value));
|
|
208
|
+
if (schemaDerivedDraws === 0) return Result.fail({
|
|
209
|
+
path: arm.path,
|
|
210
|
+
kind: arm.kind,
|
|
211
|
+
message: `refutation.kernel: witness search failed for arm "${arm.path}" (${arm.kind}); neither the encoded nor the type arbitrary of the weakened schema yielded a draw, so "no obligation" cannot be distinguished from "could not look".`
|
|
212
|
+
});
|
|
213
|
+
return Result.succeed(Option.none());
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Walk `armsOf` and classify every arm. An arm is an obligation iff a witness
|
|
217
|
+
* exists — an input the weakened schema accepts and the original rejects —
|
|
218
|
+
* drawn from a fallback chain (encoded arbitrary, type arbitrary, generic
|
|
219
|
+
* pool). An arm whose every source failed to construct is `blind`: "no
|
|
220
|
+
* obligation" and "could not look" are different answers, and collapsing
|
|
221
|
+
* them is the silent miss this scan exists to prevent.
|
|
222
|
+
*/
|
|
223
|
+
const scanObligations = (schema) => {
|
|
224
|
+
const collected = /* @__PURE__ */ new Map();
|
|
225
|
+
const blind = [];
|
|
226
|
+
for (const arm of armsOf(schema)) {
|
|
227
|
+
const found = findWitness(schema, arm);
|
|
228
|
+
if (Result.isFailure(found)) {
|
|
229
|
+
blind.push(found.failure);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (Option.isNone(found.success)) continue;
|
|
233
|
+
const existing = collected.get(arm.node);
|
|
234
|
+
if (existing === void 0) collected.set(arm.node, {
|
|
235
|
+
paths: [arm.path],
|
|
236
|
+
witness: found.success.value,
|
|
237
|
+
weakened: arm.weakened
|
|
238
|
+
});
|
|
239
|
+
else existing.paths.push(arm.path);
|
|
240
|
+
}
|
|
241
|
+
const obligations = /* @__PURE__ */ new Map();
|
|
242
|
+
for (const [node, entry] of collected) obligations.set(node, {
|
|
243
|
+
node,
|
|
244
|
+
tag: node._tag,
|
|
245
|
+
paths: entry.paths,
|
|
246
|
+
weakened: entry.weakened,
|
|
247
|
+
witness: entry.witness
|
|
248
|
+
});
|
|
249
|
+
return {
|
|
250
|
+
obligations,
|
|
251
|
+
blind
|
|
252
|
+
};
|
|
253
|
+
};
|
|
254
|
+
const obligationsOf = (schema) => scanObligations(schema).obligations;
|
|
255
|
+
const discharges = (schema, arbitrary, obligation) => {
|
|
256
|
+
const weakened = Schema.make(obligation.weakened);
|
|
257
|
+
const draws = sample(arbitrary, 256);
|
|
258
|
+
if (draws === void 0) return false;
|
|
259
|
+
for (const value of draws) if (accepts(weakened, value) && !accepts(schema, value)) return true;
|
|
260
|
+
return false;
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* For each obligation, return the names of the generators whose draws
|
|
264
|
+
* the weakened schema accepts AND the original rejects. Generators
|
|
265
|
+
* whose draws the original also accepts do not discharge — they are
|
|
266
|
+
* the language the original already covers. Generators whose draws
|
|
267
|
+
* the weakened schema rejects do not discharge either.
|
|
268
|
+
*
|
|
269
|
+
* The original schema is part of the signature because discharge is
|
|
270
|
+
* defined relative to it.
|
|
271
|
+
*/
|
|
272
|
+
const dischargedBy = (schema, obligations, generators) => {
|
|
273
|
+
const out = /* @__PURE__ */ new Map();
|
|
274
|
+
for (const [node, obligation] of obligations) {
|
|
275
|
+
const discharging = [];
|
|
276
|
+
for (const [name, arbitrary] of Object.entries(generators)) if (discharges(schema, arbitrary, obligation)) discharging.push(name);
|
|
277
|
+
out.set(node, discharging);
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
};
|
|
281
|
+
//#endregion
|
|
282
|
+
//#region src/Refutes.ts
|
|
283
|
+
const renderWitness = (witness) => {
|
|
284
|
+
try {
|
|
285
|
+
return JSON.stringify(witness) ?? String(witness);
|
|
286
|
+
} catch {
|
|
287
|
+
return String(witness);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
/** True when `value` is a witness for some obligation: accepted by that weakening, rejected by the schema. */
|
|
291
|
+
const discriminates = (schema, obligations, value) => {
|
|
292
|
+
if (Exit.isSuccess(Schema.decodeUnknownExit(schema)(value))) return false;
|
|
293
|
+
for (const obligation of obligations.values()) {
|
|
294
|
+
const weakened = Schema.make(obligation.weakened);
|
|
295
|
+
if (Exit.isSuccess(Schema.decodeUnknownExit(weakened)(value))) return true;
|
|
296
|
+
}
|
|
297
|
+
return false;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* Which obligations no declared generator discharges. A bare "adequacy failed" leaves
|
|
301
|
+
* the author nothing to act on, so the message names each node's tag, every path
|
|
302
|
+
* reaching it, and the witness that proves the weakening is permissive.
|
|
303
|
+
*/
|
|
304
|
+
const adequacyReport = (schema, generators) => {
|
|
305
|
+
const scan = scanObligations(schema);
|
|
306
|
+
const credits = dischargedBy(schema, scan.obligations, generators);
|
|
307
|
+
const undischarged = [...scan.obligations.values()].filter((obligation) => (credits.get(obligation.node) ?? []).length === 0);
|
|
308
|
+
if (scan.blind.length > 0) return {
|
|
309
|
+
adequate: false,
|
|
310
|
+
undischarged,
|
|
311
|
+
message: `${scan.blind.length} arm(s) could not be searched for a witness:\n` + scan.blind.map((b) => ` ${b.message}`).join("\n")
|
|
312
|
+
};
|
|
313
|
+
if (undischarged.length === 0) return {
|
|
314
|
+
adequate: true,
|
|
315
|
+
undischarged,
|
|
316
|
+
message: ""
|
|
317
|
+
};
|
|
318
|
+
const detail = undischarged.map((o) => ` ${o.tag} reached by [${o.paths.join(" | ")}] — witness ${renderWitness(o.witness)}`).join("\n");
|
|
319
|
+
return {
|
|
320
|
+
adequate: false,
|
|
321
|
+
undischarged,
|
|
322
|
+
message: `${undischarged.length} obligation(s) discharged by no declared generator:\n${detail}`
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
* Property-test one schema's rejection contract. Registers per generator a refusal
|
|
327
|
+
* property (the schema rejects every draw) and a discrimination property (each draw is
|
|
328
|
+
* explained by some weakening), plus one adequacy property for the schema.
|
|
329
|
+
*
|
|
330
|
+
* Names follow the house convention: `∀b_<generator>_⊥`, `∀g_<generator>_discriminates`,
|
|
331
|
+
* `∀s_<schema>_adequate` — disjoint from `ruleOfSchemas`' `∀x_<name>_=x` pair.
|
|
332
|
+
*/
|
|
333
|
+
const refutes = (schema, generators) => {
|
|
334
|
+
const obligations = obligationsOf(schema);
|
|
335
|
+
const name = AST.resolveIdentifier(schema.ast) ?? String(schema.ast);
|
|
336
|
+
const decode = Schema.decodeUnknownExit(schema);
|
|
337
|
+
for (const [generator, arbitrary] of Object.entries(generators)) {
|
|
338
|
+
it.prop(`∀b_${generator}_⊥`, [arbitrary], ([value]) => Exit.isFailure(decode(value)));
|
|
339
|
+
it.prop(`∀g_${generator}_discriminates`, [arbitrary], ([value]) => discriminates(schema, obligations, value));
|
|
340
|
+
}
|
|
341
|
+
it("∀s_" + name + "_adequate", () => {
|
|
342
|
+
const report = adequacyReport(schema, generators);
|
|
343
|
+
expect(report.message).toBe("");
|
|
344
|
+
});
|
|
345
|
+
};
|
|
346
|
+
//#endregion
|
|
347
|
+
//#region src/RuleOfSchemas.ts
|
|
18
348
|
/**
|
|
19
349
|
* Property-test the round-trip and encode-stability laws of any Effect Schema.
|
|
20
350
|
*
|
|
@@ -25,22 +355,23 @@ const boundedUnion = (identifier, options) => {
|
|
|
25
355
|
* Use inside a `describe` block to scope the generated tests.
|
|
26
356
|
*/
|
|
27
357
|
const ruleOfSchemas = (name, schema) => {
|
|
28
|
-
const
|
|
358
|
+
const decodeExit = Schema.decodeExit(schema);
|
|
29
359
|
const encodeSync = Schema.encodeSync(schema);
|
|
30
|
-
const typeEq = Schema.
|
|
31
|
-
const encodedEq = Schema.
|
|
32
|
-
|
|
360
|
+
const typeEq = Schema.toEquivalence(schema);
|
|
361
|
+
const encodedEq = Schema.toEquivalence(Schema.toEncoded(schema));
|
|
362
|
+
const arbitrary = Schema.toArbitrary(schema)(FastCheck);
|
|
363
|
+
it.prop(`∀x_${name}Enc_=x`, [arbitrary], ([value]) => {
|
|
33
364
|
const encoded = encodeSync(value);
|
|
34
|
-
const result =
|
|
35
|
-
if (
|
|
36
|
-
const reencoded = encodeSync(result.
|
|
365
|
+
const result = decodeExit(encoded);
|
|
366
|
+
if (Exit.isFailure(result)) return false;
|
|
367
|
+
const reencoded = encodeSync(result.value);
|
|
37
368
|
return encodedEq(reencoded, encoded);
|
|
38
369
|
});
|
|
39
|
-
it.prop(`∀x_${name}_=x`, [
|
|
370
|
+
it.prop(`∀x_${name}_=x`, [arbitrary], ([value]) => {
|
|
40
371
|
const encoded = encodeSync(value);
|
|
41
|
-
const result =
|
|
42
|
-
return
|
|
372
|
+
const result = decodeExit(encoded);
|
|
373
|
+
return Exit.isSuccess(result) && typeEq(result.value, value);
|
|
43
374
|
});
|
|
44
375
|
};
|
|
45
376
|
//#endregion
|
|
46
|
-
export { boundedUnion, ruleOfSchemas };
|
|
377
|
+
export { WITNESS_BUDGET, adequacyReport, armsOf, boundedUnion, dischargedBy, discriminates, obligationsOf, refutes, ruleOfSchemas, scanObligations };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@systemfsoftware/effect-schema-law",
|
|
3
|
-
"license": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"license": "Apache-2.0",
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"author": "Ryan Lee <drdgvhbh@gmail.com>",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -36,24 +36,24 @@
|
|
|
36
36
|
"dist"
|
|
37
37
|
],
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@effect/vitest": "0.
|
|
39
|
+
"@effect/vitest": "4.0.0-rc.108",
|
|
40
40
|
"@microsoft/api-extractor": "^7.58.7",
|
|
41
|
+
"@systemfsoftware/arethetypeswrong-cli": "^1.1.1",
|
|
41
42
|
"@types/node": "^24",
|
|
42
|
-
"effect": "
|
|
43
|
-
"fast-check": "^
|
|
43
|
+
"effect": "4.0.0-rc.108",
|
|
44
|
+
"fast-check": "^4",
|
|
45
|
+
"oxlint": "^1.77.0",
|
|
44
46
|
"rimraf": "^6.1.3",
|
|
45
|
-
"tsdown": "^0.22.
|
|
46
|
-
"
|
|
47
|
-
"vitest": "^4",
|
|
48
|
-
"@systemfsoftware/arethetypeswrong-cli": "^1.1.1",
|
|
49
|
-
"@systemfsoftware/effect-gherkin-spec": "^0.5.1",
|
|
50
|
-
"@systemfsoftware/tsconfig": "^1.3.0",
|
|
47
|
+
"tsdown": "^0.22.14",
|
|
48
|
+
"vitest": "^4.1.10",
|
|
51
49
|
"@systemfsoftware/oxlint-config": "^0.1.0",
|
|
50
|
+
"@systemfsoftware/tsconfig": "^1.3.2",
|
|
51
|
+
"@systemfsoftware/effect-gherkin-spec": "^1.0.0",
|
|
52
52
|
"@systemfsoftware/vitest-config": "^0.1.0"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@effect/vitest": "0.
|
|
56
|
-
"effect": "
|
|
55
|
+
"@effect/vitest": "4.0.0-rc.108",
|
|
56
|
+
"effect": "4.0.0-rc.108",
|
|
57
57
|
"vitest": "*"
|
|
58
58
|
},
|
|
59
59
|
"publishConfig": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"test:run": "vitest run",
|
|
68
68
|
"api:check": "api-extractor run",
|
|
69
69
|
"api:update": "api-extractor run --local",
|
|
70
|
-
"lint": "oxlint .
|
|
70
|
+
"lint": "f=${OXLINT_FORMAT:-${AGENT:+agent}}; oxlint . --format=${f:-default}",
|
|
71
71
|
"attw": "attw --pack ."
|
|
72
72
|
}
|
|
73
73
|
}
|