@plasius/schema 1.0.18 → 1.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/.github/workflows/cd.yml +51 -1
- package/CHANGELOG.md +35 -1
- package/README.md +140 -1
- package/package.json +1 -1
- package/sbom.cdx.json +6 -6
- package/src/field.builder.ts +121 -1
- package/src/field.ts +139 -0
- package/src/schema.ts +78 -11
- package/src/validation/dateTime.ISO8601.ts +56 -5
- package/src/validation/languageCode.BCP47.ts +299 -0
- package/src/validation/version.SEMVER2.0.0.ts +3 -1
- package/tests/field.builder.test.ts +81 -0
- package/tests/fields.test.ts +213 -0
- package/tests/pii.test.ts +2 -2
- package/tests/schema.test.ts +26 -26
- package/tests/test-utils.ts +7 -7
- package/tests/validate.test.ts +6 -6
package/.github/workflows/cd.yml
CHANGED
|
@@ -2,6 +2,21 @@ name: CD (Publish to npm)
|
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
workflow_dispatch:
|
|
5
|
+
inputs:
|
|
6
|
+
bump:
|
|
7
|
+
description: "Version bump type"
|
|
8
|
+
required: true
|
|
9
|
+
type: choice
|
|
10
|
+
default: patch
|
|
11
|
+
options:
|
|
12
|
+
- patch
|
|
13
|
+
- minor
|
|
14
|
+
- major
|
|
15
|
+
- none
|
|
16
|
+
preid:
|
|
17
|
+
description: "Pre-release id (e.g. beta, rc). Leave blank for stable"
|
|
18
|
+
required: false
|
|
19
|
+
type: string
|
|
5
20
|
|
|
6
21
|
permissions:
|
|
7
22
|
contents: write
|
|
@@ -26,11 +41,46 @@ jobs:
|
|
|
26
41
|
id: pkg
|
|
27
42
|
env:
|
|
28
43
|
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
44
|
+
BUMP: ${{ inputs.bump }}
|
|
45
|
+
PREID: ${{ inputs.preid }}
|
|
29
46
|
run: |
|
|
30
47
|
set -euo pipefail
|
|
31
48
|
git config user.name "github-actions[bot]"
|
|
32
49
|
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
33
|
-
|
|
50
|
+
|
|
51
|
+
# Decide bump command
|
|
52
|
+
BUMP_CMD=""
|
|
53
|
+
case "${BUMP:-patch}" in
|
|
54
|
+
major|minor|patch)
|
|
55
|
+
if [ -n "${PREID:-}" ]; then
|
|
56
|
+
# Convert to pre* if preid provided
|
|
57
|
+
case "${BUMP}" in
|
|
58
|
+
major) BUMP_CMD="npm version premajor --preid ${PREID} -m 'chore: release v%s [skip ci]'" ;;
|
|
59
|
+
minor) BUMP_CMD="npm version preminor --preid ${PREID} -m 'chore: release v%s [skip ci]'" ;;
|
|
60
|
+
patch) BUMP_CMD="npm version prepatch --preid ${PREID} -m 'chore: release v%s [skip ci]'" ;;
|
|
61
|
+
esac
|
|
62
|
+
else
|
|
63
|
+
BUMP_CMD="npm version ${BUMP} -m 'chore: release v%s [skip ci]'"
|
|
64
|
+
fi
|
|
65
|
+
;;
|
|
66
|
+
none)
|
|
67
|
+
# No version bump; use existing version
|
|
68
|
+
BUMP_CMD="echo"
|
|
69
|
+
;;
|
|
70
|
+
*)
|
|
71
|
+
echo "Unknown bump type: ${BUMP}" >&2
|
|
72
|
+
exit 1
|
|
73
|
+
;;
|
|
74
|
+
esac
|
|
75
|
+
|
|
76
|
+
if [ "${BUMP}" = "none" ]; then
|
|
77
|
+
CURRENT_VER=$(node -p "require('./package.json').version")
|
|
78
|
+
NEW_VER="v${CURRENT_VER}"
|
|
79
|
+
else
|
|
80
|
+
# shellcheck disable=SC2086
|
|
81
|
+
NEW_VER=$(sh -lc "$BUMP_CMD")
|
|
82
|
+
fi
|
|
83
|
+
|
|
34
84
|
echo "New version: $NEW_VER"
|
|
35
85
|
git push --follow-tags
|
|
36
86
|
|
package/CHANGELOG.md
CHANGED
|
@@ -21,6 +21,39 @@ The format is based on **[Keep a Changelog](https://keepachangelog.com/en/1.1.0/
|
|
|
21
21
|
- **Security**
|
|
22
22
|
- (placeholder)
|
|
23
23
|
|
|
24
|
+
## [1.1.0] - 2025-09-18
|
|
25
|
+
|
|
26
|
+
- **Added**
|
|
27
|
+
- field().upgrade() function now added to allow upgrades of older data sets to newer data.
|
|
28
|
+
- min/max/pattern/default FieldBuilder elements added for validation.
|
|
29
|
+
- Added new validator for language code BCP 47 format.
|
|
30
|
+
- Added new validator options for ISO DATE TIME filtering to Date or Time or Both
|
|
31
|
+
- Added new pre-built field() types including PII flags and validators for:
|
|
32
|
+
- email
|
|
33
|
+
- phone
|
|
34
|
+
- url
|
|
35
|
+
- uuid
|
|
36
|
+
- dateTimeISO
|
|
37
|
+
- dateISO
|
|
38
|
+
- timeISO
|
|
39
|
+
- richText
|
|
40
|
+
- generalText
|
|
41
|
+
- latitude
|
|
42
|
+
- longitude
|
|
43
|
+
- version
|
|
44
|
+
- countryCode
|
|
45
|
+
- languageCode
|
|
46
|
+
- New field().xxx tests for the above types.
|
|
47
|
+
|
|
48
|
+
- **Changed**
|
|
49
|
+
- Updated CD Pipeline to accept a new param for version Major, Minor or Patch update
|
|
50
|
+
|
|
51
|
+
- **Fixed**
|
|
52
|
+
- validateISODateTime for dateTime now accepts string matches that might not be the same as the date.toISOString() return value but are still valid ISO Date Time Strings.
|
|
53
|
+
|
|
54
|
+
- **Security**
|
|
55
|
+
- (placeholder)
|
|
56
|
+
|
|
24
57
|
## [1.0.18] - 2025-09-17
|
|
25
58
|
|
|
26
59
|
- **Fixed**
|
|
@@ -79,8 +112,9 @@ The format is based on **[Keep a Changelog](https://keepachangelog.com/en/1.1.0/
|
|
|
79
112
|
|
|
80
113
|
---
|
|
81
114
|
|
|
82
|
-
[Unreleased]: https://github.com/Plasius-LTD/schema/compare/v1.0
|
|
115
|
+
[Unreleased]: https://github.com/Plasius-LTD/schema/compare/v1.1.0...HEAD
|
|
83
116
|
[1.0.0]: https://github.com/Plasius-LTD/schema/releases/tag/v1.0.0
|
|
84
117
|
[1.0.13]: https://github.com/Plasius-LTD/schema/releases/tag/v1.0.13
|
|
85
118
|
[1.0.17]: https://github.com/Plasius-LTD/schema/releases/tag/v1.0.17
|
|
86
119
|
[1.0.18]: https://github.com/Plasius-LTD/schema/releases/tag/v1.0.18
|
|
120
|
+
[1.1.0]: https://github.com/Plasius-LTD/schema/releases/tag/v1.1.0
|
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
|
package/package.json
CHANGED
package/sbom.cdx.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json",
|
|
3
3
|
"bomFormat": "CycloneDX",
|
|
4
4
|
"specVersion": "1.5",
|
|
5
|
-
"serialNumber": "urn:uuid:
|
|
5
|
+
"serialNumber": "urn:uuid:329219f1-3f86-497c-9d0e-3dc025a3a272",
|
|
6
6
|
"version": 1,
|
|
7
7
|
"metadata": {
|
|
8
|
-
"timestamp": "2025-09-
|
|
8
|
+
"timestamp": "2025-09-18T14:38:35.724Z",
|
|
9
9
|
"lifecycles": [
|
|
10
10
|
{
|
|
11
11
|
"phase": "build"
|
|
@@ -19,14 +19,14 @@
|
|
|
19
19
|
}
|
|
20
20
|
],
|
|
21
21
|
"component": {
|
|
22
|
-
"bom-ref": "@plasius/schema@1.0
|
|
22
|
+
"bom-ref": "@plasius/schema@1.1.0",
|
|
23
23
|
"type": "library",
|
|
24
24
|
"name": "schema",
|
|
25
|
-
"version": "1.0
|
|
25
|
+
"version": "1.1.0",
|
|
26
26
|
"scope": "required",
|
|
27
27
|
"author": "Plasius LTD",
|
|
28
28
|
"description": "Entity schema definition & validation helpers for Plasius ecosystem",
|
|
29
|
-
"purl": "pkg:npm/%40plasius/schema@1.0
|
|
29
|
+
"purl": "pkg:npm/%40plasius/schema@1.1.0",
|
|
30
30
|
"properties": [
|
|
31
31
|
{
|
|
32
32
|
"name": "cdx:npm:package:path",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"components": [],
|
|
60
60
|
"dependencies": [
|
|
61
61
|
{
|
|
62
|
-
"ref": "@plasius/schema@1.0
|
|
62
|
+
"ref": "@plasius/schema@1.1.0",
|
|
63
63
|
"dependsOn": []
|
|
64
64
|
}
|
|
65
65
|
]
|
package/src/field.builder.ts
CHANGED
|
@@ -11,7 +11,17 @@ export class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
|
|
|
11
11
|
isRequired = true;
|
|
12
12
|
_validator?: (value: any) => boolean;
|
|
13
13
|
_description: string = "";
|
|
14
|
-
_version: string = "1.0";
|
|
14
|
+
_version: string = "1.0.0";
|
|
15
|
+
_default?: TInternal | (() => TInternal);
|
|
16
|
+
_upgrade?: (
|
|
17
|
+
value: any,
|
|
18
|
+
ctx: {
|
|
19
|
+
entityFrom: string;
|
|
20
|
+
entityTo: string;
|
|
21
|
+
fieldTo: string;
|
|
22
|
+
fieldName: string;
|
|
23
|
+
}
|
|
24
|
+
) => { ok: boolean; value?: any; error?: string };
|
|
15
25
|
_shape?: Record<string, FieldBuilder<any>>;
|
|
16
26
|
itemType?: FieldBuilder<any>;
|
|
17
27
|
refType?: string;
|
|
@@ -68,6 +78,41 @@ export class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
|
|
|
68
78
|
return this;
|
|
69
79
|
}
|
|
70
80
|
|
|
81
|
+
default(
|
|
82
|
+
value: TInternal | (() => TInternal)
|
|
83
|
+
): FieldBuilder<TExternal, TInternal> {
|
|
84
|
+
this._default = value;
|
|
85
|
+
// Supplying a default implies the value may be omitted at input time.
|
|
86
|
+
// Do not couple defaulting with validation.
|
|
87
|
+
this.isRequired = false;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Configure an upgrader used when validating older entities against a newer schema.
|
|
93
|
+
* The upgrader receives the current field value and version context, and should
|
|
94
|
+
* return { ok: true, value } with the upgraded value, or { ok: false, error }.
|
|
95
|
+
*/
|
|
96
|
+
upgrade(
|
|
97
|
+
fn: (
|
|
98
|
+
value: any,
|
|
99
|
+
ctx: {
|
|
100
|
+
entityFrom: string;
|
|
101
|
+
entityTo: string;
|
|
102
|
+
fieldTo: string;
|
|
103
|
+
fieldName: string;
|
|
104
|
+
}
|
|
105
|
+
) => { ok: boolean; value?: any; error?: string }
|
|
106
|
+
): FieldBuilder<TExternal, TInternal> {
|
|
107
|
+
this._upgrade = fn;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
getDefault(): TInternal | undefined {
|
|
112
|
+
const v = this._default;
|
|
113
|
+
return typeof v === "function" ? (v as () => TInternal)() : v;
|
|
114
|
+
}
|
|
115
|
+
|
|
71
116
|
version(ver: string): FieldBuilder<TExternal, TInternal> {
|
|
72
117
|
this._version = ver;
|
|
73
118
|
return this;
|
|
@@ -80,6 +125,72 @@ export class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
|
|
|
80
125
|
return this;
|
|
81
126
|
}
|
|
82
127
|
|
|
128
|
+
min(min: number): FieldBuilder<TExternal, TInternal> {
|
|
129
|
+
if (this.type === "number") {
|
|
130
|
+
const prevValidator = this._validator;
|
|
131
|
+
this._validator = (value: any) => {
|
|
132
|
+
const valid = typeof value === "number" && value >= min;
|
|
133
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
134
|
+
};
|
|
135
|
+
} else if (this.type === "string") {
|
|
136
|
+
const prevValidator = this._validator;
|
|
137
|
+
this._validator = (value: any) => {
|
|
138
|
+
const valid = typeof value === "string" && value.length >= min;
|
|
139
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
140
|
+
};
|
|
141
|
+
} else if (this.type === "array") {
|
|
142
|
+
const prevValidator = this._validator;
|
|
143
|
+
this._validator = (value: any) => {
|
|
144
|
+
const valid = Array.isArray(value) && value.length >= min;
|
|
145
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
146
|
+
};
|
|
147
|
+
} else {
|
|
148
|
+
throw new Error(
|
|
149
|
+
"Min is only supported on number, string, or array fields."
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
max(max: number): FieldBuilder<TExternal, TInternal> {
|
|
156
|
+
if (this.type === "number") {
|
|
157
|
+
const prevValidator = this._validator;
|
|
158
|
+
this._validator = (value: any) => {
|
|
159
|
+
const valid = typeof value === "number" && value <= max;
|
|
160
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
161
|
+
};
|
|
162
|
+
} else if (this.type === "string") {
|
|
163
|
+
const prevValidator = this._validator;
|
|
164
|
+
this._validator = (value: any) => {
|
|
165
|
+
const valid = typeof value === "string" && value.length <= max;
|
|
166
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
167
|
+
};
|
|
168
|
+
} else if (this.type === "array") {
|
|
169
|
+
const prevValidator = this._validator;
|
|
170
|
+
this._validator = (value: any) => {
|
|
171
|
+
const valid = Array.isArray(value) && value.length <= max;
|
|
172
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
173
|
+
};
|
|
174
|
+
} else {
|
|
175
|
+
throw new Error(
|
|
176
|
+
"Max is only supported on number, string, or array fields."
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
pattern(regex: RegExp): FieldBuilder<TExternal, TInternal> {
|
|
183
|
+
if (this.type !== "string") {
|
|
184
|
+
throw new Error("Pattern is only supported on string fields.");
|
|
185
|
+
}
|
|
186
|
+
const prevValidator = this._validator;
|
|
187
|
+
this._validator = (value: any) => {
|
|
188
|
+
const valid = typeof value === "string" && regex.test(value);
|
|
189
|
+
return prevValidator ? prevValidator(value) && valid : valid;
|
|
190
|
+
};
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
|
|
83
194
|
enum<const U extends readonly TInternal[]>(
|
|
84
195
|
values: U
|
|
85
196
|
): FieldBuilder<U[number]> {
|
|
@@ -99,10 +210,16 @@ export class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
|
|
|
99
210
|
return this as any;
|
|
100
211
|
}
|
|
101
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Create a shallow clone with a different external type parameter.
|
|
215
|
+
* Note: shape and itemType are passed by reference (shallow). If you need
|
|
216
|
+
* deep isolation of nested FieldBuilders, clone them explicitly.
|
|
217
|
+
*/
|
|
102
218
|
as<U>(): FieldBuilder<U, TInternal> {
|
|
103
219
|
const clone = new FieldBuilder<U, TInternal>(this.type, {
|
|
104
220
|
shape: this._shape,
|
|
105
221
|
itemType: this.itemType,
|
|
222
|
+
refType: this.refType,
|
|
106
223
|
});
|
|
107
224
|
clone.enumValues = this.enumValues;
|
|
108
225
|
clone.isImmutable = this.isImmutable;
|
|
@@ -112,6 +229,9 @@ export class FieldBuilder<TExternal = unknown, TInternal = TExternal> {
|
|
|
112
229
|
clone._version = this._version;
|
|
113
230
|
clone._pii = this._pii;
|
|
114
231
|
clone._validator = this._validator as any;
|
|
232
|
+
clone._default = this._default as any;
|
|
233
|
+
clone._upgrade = this._upgrade;
|
|
234
|
+
// refType already provided in constructor options above
|
|
115
235
|
return clone;
|
|
116
236
|
}
|
|
117
237
|
}
|
package/src/field.ts
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
|
+
import { version } from "os";
|
|
1
2
|
import { FieldBuilder } from "./field.builder.js";
|
|
2
3
|
import { Infer } from "./infer.js";
|
|
3
4
|
import { SchemaShape } from "./types.js";
|
|
5
|
+
import {
|
|
6
|
+
validateCountryCode,
|
|
7
|
+
validateDateTimeISO,
|
|
8
|
+
validateEmail,
|
|
9
|
+
validatePhone,
|
|
10
|
+
validateRichText,
|
|
11
|
+
validateSafeText,
|
|
12
|
+
validateSemVer,
|
|
13
|
+
validateUrl,
|
|
14
|
+
validateUUID,
|
|
15
|
+
} from "./validation/index.js";
|
|
16
|
+
import { validateLanguage } from "./validation/languageCode.BCP47.js";
|
|
4
17
|
|
|
5
18
|
export const field = {
|
|
6
19
|
string: () => new FieldBuilder<string>("string"),
|
|
@@ -11,4 +24,130 @@ export const field = {
|
|
|
11
24
|
array: (itemType: FieldBuilder) => new FieldBuilder("array", { itemType }),
|
|
12
25
|
ref: <S extends SchemaShape>(refType: string) =>
|
|
13
26
|
new FieldBuilder<Infer<S>>("ref", { refType }),
|
|
27
|
+
email: () =>
|
|
28
|
+
new FieldBuilder<string>("string")
|
|
29
|
+
.validator(validateEmail)
|
|
30
|
+
.PID({
|
|
31
|
+
classification: "high",
|
|
32
|
+
action: "encrypt",
|
|
33
|
+
logHandling: "redact",
|
|
34
|
+
purpose: "an email address",
|
|
35
|
+
})
|
|
36
|
+
.description("An email address"),
|
|
37
|
+
phone: () =>
|
|
38
|
+
new FieldBuilder<string>("string")
|
|
39
|
+
.validator(validatePhone)
|
|
40
|
+
.PID({
|
|
41
|
+
classification: "high",
|
|
42
|
+
action: "encrypt",
|
|
43
|
+
logHandling: "redact",
|
|
44
|
+
purpose: "a phone number",
|
|
45
|
+
})
|
|
46
|
+
.description("A phone number"),
|
|
47
|
+
url: () =>
|
|
48
|
+
new FieldBuilder<string>("string")
|
|
49
|
+
.validator(validateUrl)
|
|
50
|
+
.PID({
|
|
51
|
+
classification: "low",
|
|
52
|
+
action: "hash",
|
|
53
|
+
logHandling: "pseudonym",
|
|
54
|
+
purpose: "a URL",
|
|
55
|
+
})
|
|
56
|
+
.description("A URL"),
|
|
57
|
+
uuid: () =>
|
|
58
|
+
new FieldBuilder<string>("string")
|
|
59
|
+
.PID({
|
|
60
|
+
classification: "low",
|
|
61
|
+
action: "hash",
|
|
62
|
+
logHandling: "pseudonym",
|
|
63
|
+
purpose: "a UUID",
|
|
64
|
+
})
|
|
65
|
+
.validator(validateUUID)
|
|
66
|
+
.description("A UUID"),
|
|
67
|
+
dateTimeISO: () =>
|
|
68
|
+
new FieldBuilder<string>("string")
|
|
69
|
+
.PID({
|
|
70
|
+
classification: "none",
|
|
71
|
+
action: "none",
|
|
72
|
+
logHandling: "plain",
|
|
73
|
+
purpose: "a date string",
|
|
74
|
+
})
|
|
75
|
+
.validator(validateDateTimeISO)
|
|
76
|
+
.description("A date string in ISO 8601 format"),
|
|
77
|
+
dateISO: () =>
|
|
78
|
+
new FieldBuilder<string>("string")
|
|
79
|
+
.PID({
|
|
80
|
+
classification: "none",
|
|
81
|
+
action: "none",
|
|
82
|
+
logHandling: "plain",
|
|
83
|
+
purpose: "a date string",
|
|
84
|
+
})
|
|
85
|
+
.validator((s) => validateDateTimeISO(s, { mode: "date" }))
|
|
86
|
+
.description("A date string in ISO 8601 format (date only)"),
|
|
87
|
+
timeISO: () =>
|
|
88
|
+
new FieldBuilder<string>("string")
|
|
89
|
+
.PID({
|
|
90
|
+
classification: "none",
|
|
91
|
+
action: "none",
|
|
92
|
+
logHandling: "plain",
|
|
93
|
+
purpose: "a time string",
|
|
94
|
+
})
|
|
95
|
+
.validator((s) => validateDateTimeISO(s, { mode: "time" }))
|
|
96
|
+
.description("A time string in ISO 8601 format (time only)"),
|
|
97
|
+
richText: () =>
|
|
98
|
+
new FieldBuilder<string>("string")
|
|
99
|
+
.PID({
|
|
100
|
+
classification: "low",
|
|
101
|
+
action: "clear",
|
|
102
|
+
logHandling: "omit",
|
|
103
|
+
purpose: "rich text content",
|
|
104
|
+
})
|
|
105
|
+
.validator(validateRichText)
|
|
106
|
+
.description("Rich text content, may include basic HTML formatting"),
|
|
107
|
+
generalText: () =>
|
|
108
|
+
new FieldBuilder<string>("string")
|
|
109
|
+
.PID({
|
|
110
|
+
classification: "none",
|
|
111
|
+
action: "none",
|
|
112
|
+
logHandling: "plain",
|
|
113
|
+
purpose: "Plain text content",
|
|
114
|
+
})
|
|
115
|
+
.validator(validateSafeText)
|
|
116
|
+
.description("Standard text content, no HTML allowed"),
|
|
117
|
+
latitude: () =>
|
|
118
|
+
new FieldBuilder<number>("number")
|
|
119
|
+
.PID({
|
|
120
|
+
classification: "low",
|
|
121
|
+
action: "clear",
|
|
122
|
+
logHandling: "omit",
|
|
123
|
+
purpose: "Latitude in decimal degrees, WGS 84 (ISO 6709)",
|
|
124
|
+
})
|
|
125
|
+
.min(-90)
|
|
126
|
+
.max(90)
|
|
127
|
+
.description("Latitude in decimal degrees, WGS 84 (ISO 6709)"),
|
|
128
|
+
longitude: () =>
|
|
129
|
+
new FieldBuilder<number>("number")
|
|
130
|
+
.PID({
|
|
131
|
+
classification: "low",
|
|
132
|
+
action: "clear",
|
|
133
|
+
logHandling: "omit",
|
|
134
|
+
purpose: "Longitude in decimal degrees, WGS 84 (ISO 6709)",
|
|
135
|
+
})
|
|
136
|
+
.min(-180)
|
|
137
|
+
.max(180)
|
|
138
|
+
.description("Longitude in decimal degrees, WGS 84 (ISO 6709)"),
|
|
139
|
+
version: () =>
|
|
140
|
+
new FieldBuilder<string>("string")
|
|
141
|
+
.validator(validateSemVer)
|
|
142
|
+
.description("A semantic version string, e.g. '1.0.0'"),
|
|
143
|
+
countryCode: () =>
|
|
144
|
+
new FieldBuilder<string>("string")
|
|
145
|
+
.validator(validateCountryCode)
|
|
146
|
+
.description("An ISO 3166 country code, e.g. 'US', 'GB', 'FR'"),
|
|
147
|
+
languageCode: () =>
|
|
148
|
+
new FieldBuilder<string>("string")
|
|
149
|
+
.validator(validateLanguage)
|
|
150
|
+
.description(
|
|
151
|
+
"An BCP 47 structured language code, primarily ISO 639-1 and optionally with ISO 3166-1 alpha-2 country code, e.g. 'en', 'en-US', 'fr', 'fr-FR'"
|
|
152
|
+
),
|
|
14
153
|
};
|