@databehave/schema 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.
Files changed (4) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/LICENSE +21 -0
  3. package/README.md +110 -0
  4. package/package.json +69 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,27 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@databehave/schema` are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-06-03
11
+
12
+ Initial public release of `@databehave/schema`.
13
+
14
+ ### Added
15
+
16
+ - Schema vocabulary for distributions, domains, derived fields, invariants,
17
+ exceptions, and relations.
18
+ - Deterministic generation engine with seeded PRNG (mulberry32) and
19
+ string-keyed seeds.
20
+ - `mock`, `replay`, and `expectStable` for deterministic value generation.
21
+ - `parse` / `safeParse` runtime conformance with a closed, snapshot-locked
22
+ `IssueCode` catalog (required `Issue.code` field).
23
+ - IR walker, plus `serializeSchema` / `deserializeSchema` envelope with
24
+ `IR_VERSION` for cross-process / on-disk schemas.
25
+ - Capability gating that splits numeric vs continuous distributions and
26
+ preserves type-level narrowing on `.weighted` / `.normal` / `.typically`.
27
+ - `./internal` subpath export for plugin authors.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ochairo
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,110 @@
1
+ <!-- markdownlint-disable MD033 MD041 -->
2
+
3
+ <div align="center">
4
+
5
+ # @databehave/schema
6
+
7
+ <b>Schemas for how data behaves</b>
8
+
9
+ [![npm version](https://img.shields.io/npm/v/@databehave/schema.svg)](https://www.npmjs.com/package/@databehave/schema)
10
+ [![CI](https://github.com/ochairo/databehave/actions/workflows/ci.yml/badge.svg)](https://github.com/ochairo/databehave/actions/workflows/ci.yml)
11
+ [![Node.js](https://img.shields.io/badge/node-%E2%89%A520.19-brightgreen)](https://nodejs.org/)
12
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
13
+
14
+ </div>
15
+
16
+ Most schema libraries describe what shape your data has. @databehave/schema describes how data behaves. It has a rich vocabulary for distributions, domains, derived fields, invariants, exceptions, and relations. It also has a determinism model that keeps data consistent across requests and code changes.
17
+
18
+ ## Install
19
+
20
+ ```sh
21
+ npm install @databehave/schema
22
+ # or: pnpm add @databehave/schema
23
+ # or: yarn add @databehave/schema
24
+ ```
25
+
26
+ ## A 60-second tour
27
+
28
+ **1. Distribution.** Shape where values concentrate inside the bounds.
29
+
30
+ ```ts
31
+ const Price = decimal(10, 2).min('0').max('1000').typically(100, 300)
32
+ ```
33
+
34
+ **2. Domain.** Closed candidate set, optionally keyed off a sibling field.
35
+
36
+ ```ts
37
+ const Region = str().in(['us', 'eu', 'jp'])
38
+ const Currency = str().in({
39
+ kind: 'lookup', fromField: 'region',
40
+ map: { us: ['USD'], eu: ['EUR'], jp: ['JPY'] },
41
+ })
42
+ ```
43
+
44
+ **3. Derived.** Computed from sibling fields, never sampled — stays
45
+ consistent by construction.
46
+
47
+ ```ts
48
+ const Total = int().derivedFrom(
49
+ ctx => (ctx.parent.qty as number) * (ctx.parent.price as number),
50
+ )
51
+ ```
52
+
53
+ **4. Invariants.** Single- and multi-field predicates enforced by rejection
54
+ sampling.
55
+
56
+ ```ts
57
+ const Even = int().min(0).max(100).invariant(v => (v as number) % 2 === 0)
58
+ const Box = obj({ w: int(), h: int() }).correlate(o => o.w >= o.h)
59
+ ```
60
+
61
+ **5. Occasional & periodic overrides.** Rare random or deterministic
62
+ exceptions stacked over the base distribution.
63
+
64
+ ```ts
65
+ const Quantity = int().min(0).max(10).occasionally(-1, 0.01) // 1% chance of -1
66
+ const Heartbeat = int().min(0).max(99).eventually(10, 999) // every 10th value is 999
67
+ ```
68
+
69
+ **6. Discriminated unions.** Shape switches on a literal field.
70
+
71
+ ```ts
72
+ const Variant = discriminated('kind', {
73
+ digital: obj({ kind: literal('digital'), downloadUrl: str() }),
74
+ physical: obj({ kind: literal('physical'), weightKg: decimal(6, 2) }),
75
+ })
76
+ ```
77
+
78
+ **7. Identity & relations.** Stable records keyed by identity, plus
79
+ cross-dataset foreign keys.
80
+
81
+ ```ts
82
+ const products = mockDataset({
83
+ name: 'products', identity: ['sku'], n: 100,
84
+ schema: obj({ sku: str(), name: str() }),
85
+ })
86
+ const orders = mockDataset({
87
+ name: 'orders', identity: ['id'], n: 50,
88
+ schema: obj({ id: str(), productSku: str().derivedFrom(relate(products, 'sku')) }),
89
+ })
90
+ ```
91
+
92
+ **8. Weighted choice.** Non-uniform discrete sampling.
93
+
94
+ ```ts
95
+ const Tier = enum_(['A', 'B', 'C']).weighted([['A', 0.7], ['B', 0.2], ['C', 0.1]])
96
+ ```
97
+
98
+ ## Documentation
99
+
100
+ - [Docs](docs/index.md) — documentation hub. Start here.
101
+ - [Getting started](docs/getting-started.md) — install + 5-minute hello-world.
102
+ - Reference: [DSL](docs/schema/index.md) · [Generator](docs/generator/index.md) · [Validator](docs/validator/index.md) · [Dataset](docs/dataset/index.md) · [Foundation](docs/foundation/index.md) · [Errors](docs/errors.md).
103
+ - Deeper: [Design](docs/design.md) · [Recipes](docs/recipes.md) · [Extending](docs/extending.md) · [Stability](docs/stability.md).
104
+
105
+ <br /><br />
106
+ <div align="center">
107
+
108
+ © 2026-present ochairo. See [LICENSE](LICENSE)
109
+
110
+ </div>
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "name": "@databehave/schema",
3
+ "version": "0.1.0",
4
+ "description": "Schemas for how data behaves, not just its shape.",
5
+ "type": "module",
6
+ "author": "ochairo",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/ochairo/databehave/tree/main/packages/schema#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/ochairo/databehave.git",
12
+ "directory": "packages/schema"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/ochairo/databehave/issues"
16
+ },
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.js"
22
+ },
23
+ "./internal": {
24
+ "types": "./dist/internal.d.ts",
25
+ "import": "./dist/internal.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "README.md",
31
+ "LICENSE",
32
+ "CHANGELOG.md"
33
+ ],
34
+ "engines": {
35
+ "node": ">=20.19"
36
+ },
37
+ "dependencies": {},
38
+ "peerDependencies": {},
39
+ "devDependencies": {
40
+ "typescript": "6.0.3",
41
+ "@types/node": "25.9.1"
42
+ },
43
+ "keywords": [
44
+ "schema",
45
+ "data-schema",
46
+ "mock",
47
+ "mock-data",
48
+ "fixture",
49
+ "generator",
50
+ "validator",
51
+ "deterministic",
52
+ "seeded",
53
+ "zero-dependency",
54
+ "typescript"
55
+ ],
56
+ "publishConfig": {
57
+ "access": "public",
58
+ "provenance": true
59
+ },
60
+ "scripts": {
61
+ "build": "tsc -p tsconfig.build.json",
62
+ "build:test": "tsc -p tsconfig.test.json",
63
+ "pretest": "pnpm build:test",
64
+ "test": "find dist-test/test -name '*.test.js' -print0 | xargs -0 node --test",
65
+ "test:watch": "tsc -p tsconfig.test.json --watch & find dist-test/test -name '*.test.js' -print0 | xargs -0 node --test --watch",
66
+ "typecheck": "tsc --noEmit",
67
+ "clean": "rm -rf dist dist-test"
68
+ }
69
+ }