@systemfsoftware/effect-schema-law 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ryan Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # @systemfsoftware/effect-schema-law
2
+
3
+ Codec-law property tests for [Effect](https://effect.website) `Schema`, in one call.
4
+
5
+ A schema is a two-way codec. `ruleOfSchemas` asserts the two laws every well-formed codec must obey, generating its inputs from the schema itself (via `@effect/vitest`'s `it.prop` + fast-check):
6
+
7
+ - **Round-trip identity** — `decode(encode(x))` equals `x` (by the schema's type equivalence).
8
+ - **Encode stability** — re-encoding the decoded value reproduces the original encoded form (by the encoded-side equivalence).
9
+
10
+ ```ts
11
+ import { ruleOfSchemas } from '@systemfsoftware/effect-schema-law'
12
+ import { Schema as S } from 'effect'
13
+
14
+ const Email = S.String.pipe(S.brand('Email'))
15
+
16
+ // inside a Vitest file — registers two property tests
17
+ ruleOfSchemas('Email', Email)
18
+ ```
19
+
20
+ ## Recursive schemas
21
+
22
+ `ruleOfSchemas` generates its inputs from the schema. Effect bounds that generation at array seams, but a **union that recurses through a non-array field** — `Binary.left: Expression`, `Member.object: Expression` — is generated as an unbounded `fc.oneof(...members)`. A single sample can then recurse until the call stack overflows, and the test crashes before it ever checks a law.
23
+
24
+ `boundedUnion` builds the same union — decode, encode, and equivalence are identical to `S.Union(...)` — but caps generation depth. Past `maxDepth` (default `2`), generation collapses to the base case, so every variant stays reachable while the recursion always terminates. Split the members into the non-recursive `base` (the leaves, used as the base case) and the self-referential `recur`:
25
+
26
+ ```ts
27
+ import { boundedUnion, ruleOfSchemas } from '@systemfsoftware/effect-schema-law'
28
+ import { Schema as S } from 'effect'
29
+
30
+ interface Lit {
31
+ readonly _tag: 'Lit'
32
+ readonly value: number
33
+ }
34
+ interface Add {
35
+ readonly _tag: 'Add'
36
+ readonly left: Expr
37
+ readonly right: Expr
38
+ }
39
+ type Expr = Lit | Add
40
+
41
+ const Lit = S.Struct({ _tag: S.Literal('Lit'), value: S.JsonNumber })
42
+ const Add: S.Schema<Add> = S.suspend((): S.Schema<Add> => S.Struct({ _tag: S.Literal('Add'), left: Expr, right: Expr }))
43
+
44
+ const Expr: S.Schema<Expr> = boundedUnion('Expr', {
45
+ base: [Lit],
46
+ recur: [Add],
47
+ })
48
+
49
+ ruleOfSchemas('Expr', Expr) // generates and law-tests, no stack overflow
50
+ ```
51
+
52
+ The first argument is both the schema's `identifier` and the `depthIdentifier` fast-check counts depth against — keep it unique per recursive cycle.
53
+
54
+ ## Install
55
+
56
+ ```bash
57
+ pnpm add -D @systemfsoftware/effect-schema-law
58
+ ```
59
+
60
+ Install it as a **devDependency** — it's a test helper. `effect`, `vitest`, and `@effect/vitest` are peer dependencies: you bring your own (you already have them to run your tests), so the helper shares your single test-runner instance. Call `ruleOfSchemas(name, schema)` at the top level of a Vitest test file; it registers the two `it.prop` cases for you.
@@ -0,0 +1,13 @@
1
+ import { Schema } from "effect";
2
+
3
+ //#region src/bounded-union.d.ts
4
+ declare const boundedUnion: <Base extends readonly [Schema.Schema.Any, ...ReadonlyArray<Schema.Schema.Any>], Recur extends readonly [Schema.Schema.Any, ...ReadonlyArray<Schema.Schema.Any>]>(identifier: string, options: {
5
+ readonly base: Base;
6
+ readonly recur: Recur;
7
+ readonly maxDepth?: number;
8
+ }) => Schema.Schema<Schema.Schema.Type<Base[number] | Recur[number]>, Schema.Schema.Encoded<Base[number] | Recur[number]>, Schema.Schema.Context<Base[number] | Recur[number]>>;
9
+ //#endregion
10
+ //#region src/schema.d.ts
11
+ declare const ruleOfSchemas: <A, I>(name: string, schema: Schema.Schema<A, I, never>) => void;
12
+ //#endregion
13
+ export { boundedUnion, ruleOfSchemas };
package/dist/index.mjs ADDED
@@ -0,0 +1,35 @@
1
+ import { Arbitrary, Either, Schema } from "effect";
2
+ import { it } from "@effect/vitest";
3
+ //#region src/bounded-union.ts
4
+ const boundedUnion = (identifier, options) => {
5
+ const { base, maxDepth = 2, recur } = options;
6
+ const baseArbitraries = base.map((member) => Arbitrary.make(member));
7
+ const recurArbitraries = recur.map((member) => Arbitrary.make(member));
8
+ return Schema.Union(...base, ...recur).annotations({
9
+ identifier,
10
+ arbitrary: () => (fc) => fc.oneof({
11
+ depthIdentifier: identifier,
12
+ maxDepth
13
+ }, fc.oneof(...baseArbitraries), ...recurArbitraries)
14
+ });
15
+ };
16
+ //#endregion
17
+ //#region src/schema.ts
18
+ const ruleOfSchemas = (name, schema) => {
19
+ const decodeEither = Schema.decodeEither(schema);
20
+ const encodeSync = Schema.encodeSync(schema);
21
+ const typeEq = Schema.equivalence(schema);
22
+ const encodedEq = Schema.equivalence(Schema.encodedSchema(schema));
23
+ it.prop(`∀x_${name}Enc_=x`, [schema], ([value]) => {
24
+ const encoded = encodeSync(value);
25
+ const result = decodeEither(encoded);
26
+ if (Either.isLeft(result)) return false;
27
+ return encodedEq(encodeSync(result.right), encoded);
28
+ });
29
+ it.prop(`∀x_${name}_=x`, [schema], ([value]) => {
30
+ const result = decodeEither(encodeSync(value));
31
+ return Either.isRight(result) && typeEq(result.right, value);
32
+ });
33
+ };
34
+ //#endregion
35
+ export { boundedUnion, ruleOfSchemas };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@systemfsoftware/effect-schema-law",
3
+ "license": "MIT",
4
+ "version": "0.1.0",
5
+ "author": "Ryan Lee <drdgvhbh@gmail.com>",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/systemfsoftware/systemfsoftware.git",
9
+ "directory": "packages/effect-schema-law"
10
+ },
11
+ "homepage": "https://github.com/systemfsoftware/systemfsoftware/tree/main/packages/effect-schema-law#readme",
12
+ "bugs": "https://github.com/systemfsoftware/systemfsoftware/issues",
13
+ "description": "Property-test the codec laws of any Effect Schema in one call — decode/encode round-trip identity and encode stability, generated with @effect/vitest and fast-check.",
14
+ "keywords": [
15
+ "effect",
16
+ "effect-ts",
17
+ "schema",
18
+ "codec",
19
+ "property-testing",
20
+ "fast-check",
21
+ "vitest",
22
+ "roundtrip",
23
+ "law-testing",
24
+ "serialization",
25
+ "typescript"
26
+ ],
27
+ "type": "module",
28
+ "exports": {
29
+ ".": "./dist/index.mjs",
30
+ "./package.json": "./package.json"
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "devDependencies": {
36
+ "@effect/vitest": "0.29.0",
37
+ "@types/node": "^24",
38
+ "effect": "^3.21.2",
39
+ "fast-check": "^3",
40
+ "rimraf": "^6.1.3",
41
+ "tsdown": "^0.22.3",
42
+ "vite-tsconfig-paths": "^6.1.1",
43
+ "vitest": "^4",
44
+ "@systemfsoftware/vitest-config": "^0.1.0",
45
+ "@systemfsoftware/oxlint-config": "^0.1.0",
46
+ "@systemfsoftware/tsconfig": "^1.0.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@effect/vitest": "*",
50
+ "effect": "*",
51
+ "vitest": "*"
52
+ },
53
+ "scripts": {
54
+ "clean": "rimraf dist",
55
+ "build": "tsdown",
56
+ "typecheck": "tsgo --noEmit --incremental",
57
+ "test": "vitest run",
58
+ "test:run": "vitest run",
59
+ "lint": "oxlint . ${AGENT:+--format=unix --quiet}"
60
+ }
61
+ }