@plasius/schema 1.0.18 → 1.1.1
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/README.md +140 -1
- package/dist/index.cjs +1934 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +391 -0
- package/dist/index.d.ts +391 -0
- package/dist/index.js +1883 -0
- package/dist/index.js.map +1 -0
- package/package.json +18 -6
- package/.eslintrc.cjs +0 -7
- package/.github/workflows/cd.yml +0 -186
- package/.github/workflows/ci.yml +0 -16
- package/.nvmrc +0 -1
- package/.vscode/launch.json +0 -15
- package/CHANGELOG.md +0 -86
- package/CODE_OF_CONDUCT.md +0 -79
- package/CONTRIBUTING.md +0 -201
- package/CONTRIBUTORS.md +0 -27
- package/SECURITY.md +0 -17
- package/docs/adrs/adr-0001: schema.md +0 -45
- package/docs/adrs/adr-template.md +0 -67
- package/legal/CLA-REGISTRY.csv +0 -2
- package/legal/CLA.md +0 -22
- package/legal/CORPORATE_CLA.md +0 -57
- package/legal/INDIVIDUAL_CLA.md +0 -91
- package/sbom.cdx.json +0 -66
- package/src/components.ts +0 -39
- package/src/field.builder.ts +0 -119
- package/src/field.ts +0 -14
- package/src/index.ts +0 -7
- package/src/infer.ts +0 -34
- package/src/pii.ts +0 -165
- package/src/schema.ts +0 -826
- package/src/types.ts +0 -156
- package/src/validation/countryCode.ISO3166.ts +0 -256
- package/src/validation/currencyCode.ISO4217.ts +0 -191
- package/src/validation/dateTime.ISO8601.ts +0 -9
- package/src/validation/email.RFC5322.ts +0 -9
- package/src/validation/generalText.OWASP.ts +0 -39
- package/src/validation/index.ts +0 -13
- package/src/validation/name.OWASP.ts +0 -25
- package/src/validation/percentage.ISO80000-1.ts +0 -8
- package/src/validation/phone.E.164.ts +0 -9
- package/src/validation/richtext.OWASP.ts +0 -34
- package/src/validation/url.WHATWG.ts +0 -16
- package/src/validation/user.MS-GOOGLE-APPLE.ts +0 -31
- package/src/validation/uuid.RFC4122.ts +0 -10
- package/src/validation/version.SEMVER2.0.0.ts +0 -8
- package/tests/pii.test.ts +0 -139
- package/tests/schema.test.ts +0 -501
- package/tests/test-utils.ts +0 -97
- package/tests/validate.test.ts +0 -97
- package/tests/validation.test.ts +0 -98
- package/tsconfig.build.json +0 -19
- package/tsconfig.json +0 -7
- package/tsup.config.ts +0 -10
- package/vitest.config.js +0 -20
package/README.md
CHANGED
|
@@ -38,10 +38,149 @@ This ensures your local development environment matches the version used in CI/C
|
|
|
38
38
|
|
|
39
39
|
## Usage Example
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
### Imports
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import {
|
|
45
|
+
// core
|
|
46
|
+
createSchema,
|
|
47
|
+
field,
|
|
48
|
+
getSchemaForType,
|
|
49
|
+
getAllSchemas
|
|
50
|
+
} from "@plasius/schema";
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 1) Define fields with the `field()` builder
|
|
54
|
+
|
|
55
|
+
> Below uses the fluent builder exported via `field`/`field.builder`.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const UserFields = {
|
|
59
|
+
id: field.string().uuid().required().description("Unique user id"),
|
|
60
|
+
email: field.email().required(),
|
|
61
|
+
name: field.name().optional(),
|
|
62
|
+
age: field.number().int().min(0).optional(),
|
|
63
|
+
roles: field.array(field.string().enum(["admin", "user"]))
|
|
64
|
+
.default(["user"]).description("RBAC roles"),
|
|
65
|
+
createdAt: field.dateTimeISO().default(() => new Date()),
|
|
66
|
+
};
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Common methods (non‑exhaustive): `.required()`, `.optional()`, `.default(v|fn)`, `.description(text)`, and type‑specific helpers like `.email()`, `.uuid()`, `.min()`, `.max()`, `.enum([...])`.
|
|
70
|
+
|
|
71
|
+
### 2) Create a **versioned** schema (enforces `type` + `version`)
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
export const UserSchema = createSchema({
|
|
75
|
+
entityType: "user",
|
|
76
|
+
version: "1.0.0",
|
|
77
|
+
fields: UserFields,
|
|
78
|
+
});
|
|
42
79
|
|
|
80
|
+
// Strongly-typed entity from a schema definition
|
|
81
|
+
export type User = Infer<typeof UserSchema>;
|
|
43
82
|
```
|
|
44
83
|
|
|
84
|
+
Schemas are discoverable at runtime if you register them during module init:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
// later in app code
|
|
88
|
+
const s = getSchemaForType("user"); // returns UserSchema
|
|
89
|
+
const all = getAllSchemas(); // Map<string, Map<string, Schema>> or similar
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### 3) Validate data against the schema
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const raw = {
|
|
96
|
+
type: "user",
|
|
97
|
+
version: "1.0.0",
|
|
98
|
+
id: crypto.randomUUID(),
|
|
99
|
+
email: "alice@example.com",
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const result = UserSchemavalidate(raw);
|
|
103
|
+
if (result.valid && result.errors.length == 0) {
|
|
104
|
+
// result.value is typed as User
|
|
105
|
+
const user: User = result.value;
|
|
106
|
+
} else {
|
|
107
|
+
// result.errors: ValidationError[] (path/code/message)
|
|
108
|
+
console.error(result.errors);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
> If your validation layer also exposes a throwing variant (e.g. `validateOrThrow(UserSchema, raw)`), you can use that in places where exceptions are preferred.
|
|
113
|
+
|
|
114
|
+
### 4) Version enforcement in action
|
|
115
|
+
|
|
116
|
+
If either `type` or `version` doesn’t match the schema, validation fails.
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
const wrong = { type: "User", version: "2.0.0", id: "123", email: "x@x" };
|
|
120
|
+
const bad = UserSchema.validate(wrong);
|
|
121
|
+
// bad.valid === false; errors will include mismatches for type/version
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 5) Evolving your schema
|
|
125
|
+
|
|
126
|
+
Keep new versions side‑by‑side and migrate at edges:
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
export const UserV2 = createSchema({
|
|
130
|
+
entityType: "user",
|
|
131
|
+
version: "2.0.0",
|
|
132
|
+
fields: {
|
|
133
|
+
...UserFields,
|
|
134
|
+
displayName: field.string().min(1).max(100).optional(),
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
> Write a small migration function in your app to transform `User (1.0.0)` → `User (2.0.0)` where needed.
|
|
140
|
+
|
|
141
|
+
### 6) Field-level upgrades
|
|
142
|
+
|
|
143
|
+
The schema supports a new `.upgrade()` method on fields to define field-level upgrade logic. This is useful when tightening restrictions on a field, such as reducing maximum length, strengthening format constraints, or normalizing values, without changing the field’s overall shape.
|
|
144
|
+
|
|
145
|
+
For example, suppose a `displayName` field previously allowed strings up to 60 characters, but you want to reduce the max length to 55 characters and normalize whitespace by trimming and collapsing spaces. You can define an upgrader function that attempts to fix old values to meet the new constraints:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
const UserV3Fields = {
|
|
149
|
+
...UserFields,
|
|
150
|
+
displayName: field.string().max(55).optional()
|
|
151
|
+
.upgrade((oldValue) => {
|
|
152
|
+
if (typeof oldValue !== "string") {
|
|
153
|
+
return { ok: false, error: "Expected string" };
|
|
154
|
+
}
|
|
155
|
+
// Normalize whitespace: trim and collapse multiple spaces
|
|
156
|
+
const normalized = oldValue.trim().replace(/\s+/g, " ");
|
|
157
|
+
if (normalized.length > 55) {
|
|
158
|
+
return { ok: false, error: "Display name too long after normalization" };
|
|
159
|
+
}
|
|
160
|
+
return { ok: true, value: normalized };
|
|
161
|
+
}),
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
export const UserV3 = createSchema({
|
|
165
|
+
entityType: "user",
|
|
166
|
+
version: "3.0.0",
|
|
167
|
+
fields: UserV3Fields,
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Other typical upgrade strategies include:
|
|
172
|
+
|
|
173
|
+
- Clamping numeric values to new min/max bounds
|
|
174
|
+
- Remapping enum values to new sets or keys
|
|
175
|
+
- Normalizing whitespace or case in strings
|
|
176
|
+
- Converting deprecated flag values to new formats
|
|
177
|
+
|
|
178
|
+
During validation, if the entity version is less than the schema version and the field's value fails validation, the upgrader function will be invoked to attempt to transform the old value into a valid new value. If the upgrade succeeds and the transformed value passes validation, the upgraded value is used. If the upgrade fails or the transformed value still does not validate, validation errors will be returned.
|
|
179
|
+
|
|
180
|
+
**Note:** Field-level upgrades only run when the schema version is greater than the entity version and the field validation initially fails. This provides a convenient way to handle incremental field changes without requiring full schema migrations.
|
|
181
|
+
|
|
182
|
+
You can still write schema-level migration functions for larger or more complex changes that affect multiple fields or require more extensive transformation logic. Field-level upgrades complement these by handling simpler, localized upgrades directly within the schema definition.
|
|
183
|
+
|
|
45
184
|
---
|
|
46
185
|
|
|
47
186
|
## Contributing
|