@atscript/typescript 0.1.26 → 0.1.27
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 +50 -0
- package/dist/cli.cjs +4 -4
- package/dist/index.cjs +7 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +5 -9
- package/dist/utils.cjs +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +5 -5
- package/scripts/setup-skills.js +23 -13
- package/skills/atscript-typescript/SKILL.md +22 -14
- package/skills/atscript-typescript/annotations.md +51 -50
- package/skills/atscript-typescript/codegen.md +14 -10
- package/skills/atscript-typescript/core.md +23 -21
- package/skills/atscript-typescript/runtime.md +66 -54
- package/skills/atscript-typescript/syntax.md +31 -31
- package/skills/atscript-typescript/utilities.md +94 -67
- package/skills/atscript-typescript/validation.md +39 -39
|
@@ -18,7 +18,7 @@ validator.validate(data)
|
|
|
18
18
|
// Safe mode — returns boolean, no throw
|
|
19
19
|
if (validator.validate(data, true)) {
|
|
20
20
|
// data is narrowed to User (type guard)
|
|
21
|
-
data.name
|
|
21
|
+
data.name // TypeScript knows this exists
|
|
22
22
|
}
|
|
23
23
|
```
|
|
24
24
|
|
|
@@ -30,11 +30,11 @@ import { Validator } from '@atscript/typescript/utils'
|
|
|
30
30
|
// Create from any annotated type
|
|
31
31
|
const validator = new Validator(someAnnotatedType, {
|
|
32
32
|
// Options (all optional):
|
|
33
|
-
partial: false,
|
|
34
|
-
unknownProps: 'error',
|
|
35
|
-
errorLimit: 10,
|
|
36
|
-
plugins: [],
|
|
37
|
-
skipList: new Set(),
|
|
33
|
+
partial: false, // allow missing required props
|
|
34
|
+
unknownProps: 'error', // 'error' | 'strip' | 'ignore'
|
|
35
|
+
errorLimit: 10, // max errors before stopping
|
|
36
|
+
plugins: [], // custom validator plugins
|
|
37
|
+
skipList: new Set(), // property paths to skip
|
|
38
38
|
})
|
|
39
39
|
```
|
|
40
40
|
|
|
@@ -68,7 +68,7 @@ User.validator({ partial: 'deep' }).validate(data, true)
|
|
|
68
68
|
User.validator({
|
|
69
69
|
partial: (objectType, path) => {
|
|
70
70
|
return path === '' // only root object is partial
|
|
71
|
-
}
|
|
71
|
+
},
|
|
72
72
|
}).validate(data, true)
|
|
73
73
|
```
|
|
74
74
|
|
|
@@ -91,7 +91,7 @@ User.validator({ unknownProps: 'ignore' }).validate(data, true)
|
|
|
91
91
|
|
|
92
92
|
```ts
|
|
93
93
|
User.validator({
|
|
94
|
-
skipList: new Set(['password', 'address.zip'])
|
|
94
|
+
skipList: new Set(['password', 'address.zip']),
|
|
95
95
|
}).validate(data, true)
|
|
96
96
|
```
|
|
97
97
|
|
|
@@ -102,7 +102,7 @@ User.validator({
|
|
|
102
102
|
replace: (type, path) => {
|
|
103
103
|
if (path === 'status') return customStatusType
|
|
104
104
|
return type
|
|
105
|
-
}
|
|
105
|
+
},
|
|
106
106
|
}).validate(data, true)
|
|
107
107
|
```
|
|
108
108
|
|
|
@@ -130,9 +130,9 @@ try {
|
|
|
130
130
|
|
|
131
131
|
```ts
|
|
132
132
|
interface TError {
|
|
133
|
-
path: string
|
|
134
|
-
message: string
|
|
135
|
-
details?: TError[]
|
|
133
|
+
path: string // dot-separated path, e.g. "address.city"
|
|
134
|
+
message: string // human-readable error message
|
|
135
|
+
details?: TError[] // nested errors (for unions — shows why each branch failed)
|
|
136
136
|
}
|
|
137
137
|
```
|
|
138
138
|
|
|
@@ -190,23 +190,23 @@ User.validator({ errorLimit: 50 }).validate(data, true)
|
|
|
190
190
|
|
|
191
191
|
## What Gets Validated
|
|
192
192
|
|
|
193
|
-
| Type Kind
|
|
194
|
-
|
|
195
|
-
| `string`
|
|
196
|
-
| `number`
|
|
197
|
-
| `boolean`
|
|
198
|
-
| `null`
|
|
199
|
-
| `undefined`
|
|
200
|
-
| `any`
|
|
201
|
-
| `never`
|
|
202
|
-
| `phantom`
|
|
203
|
-
| `object`
|
|
204
|
-
| `array`
|
|
205
|
-
| `union`
|
|
206
|
-
| `intersection` | All branches must pass
|
|
207
|
-
| `tuple`
|
|
208
|
-
| `literal`
|
|
209
|
-
| `optional`
|
|
193
|
+
| Type Kind | Validation |
|
|
194
|
+
| -------------- | --------------------------------------------------------------------------------------------- |
|
|
195
|
+
| `string` | Type check + `@meta.required` (non-empty) + `@expect.minLength/maxLength` + `@expect.pattern` |
|
|
196
|
+
| `number` | Type check + `@expect.int` + `@expect.min/max` |
|
|
197
|
+
| `boolean` | Type check + `@meta.required` (must be true) |
|
|
198
|
+
| `null` | Exact `null` check |
|
|
199
|
+
| `undefined` | Exact `undefined` check |
|
|
200
|
+
| `any` | Always passes |
|
|
201
|
+
| `never` | Always fails |
|
|
202
|
+
| `phantom` | Always passes (skipped) |
|
|
203
|
+
| `object` | Recursively validates all props, handles unknown props, pattern props |
|
|
204
|
+
| `array` | Type check + `@expect.minLength/maxLength` + recursively validates each element |
|
|
205
|
+
| `union` | At least one branch must pass |
|
|
206
|
+
| `intersection` | All branches must pass |
|
|
207
|
+
| `tuple` | Array length must match + each element validated against its position |
|
|
208
|
+
| `literal` | Exact value match |
|
|
209
|
+
| `optional` | `undefined` is accepted; if value is present, validated against inner type |
|
|
210
210
|
|
|
211
211
|
## Custom Validator Plugins
|
|
212
212
|
|
|
@@ -227,11 +227,11 @@ type TValidatorPlugin = (
|
|
|
227
227
|
|
|
228
228
|
```ts
|
|
229
229
|
interface TValidatorPluginContext {
|
|
230
|
-
opts: TValidatorOptions
|
|
231
|
-
validateAnnotatedType(def, value)
|
|
232
|
-
error(message, path?, details?)
|
|
233
|
-
path: string
|
|
234
|
-
context: unknown
|
|
230
|
+
opts: TValidatorOptions // current validator options
|
|
231
|
+
validateAnnotatedType(def, value) // call default validation for a specific type
|
|
232
|
+
error(message, path?, details?) // report an error
|
|
233
|
+
path: string // current dot-separated path
|
|
234
|
+
context: unknown // external context from validate(data, safe, context)
|
|
235
235
|
}
|
|
236
236
|
```
|
|
237
237
|
|
|
@@ -261,10 +261,10 @@ User.validator({ plugins: [datePlugin] }).validate(data, true)
|
|
|
261
261
|
|
|
262
262
|
### Plugin Return Values
|
|
263
263
|
|
|
264
|
-
| Return
|
|
265
|
-
|
|
266
|
-
| `true`
|
|
267
|
-
| `false`
|
|
264
|
+
| Return | Meaning |
|
|
265
|
+
| ----------- | ----------------------------------------------------------------------------------- |
|
|
266
|
+
| `true` | Value is accepted — skip all further validation for this node |
|
|
267
|
+
| `false` | Value is rejected — error should be reported via `ctx.error()` before returning |
|
|
268
268
|
| `undefined` | Plugin doesn't handle this type — fall through to next plugin or default validation |
|
|
269
269
|
|
|
270
270
|
### Plugin Example — Coerce String to Number
|
|
@@ -288,6 +288,6 @@ Plugins run in order. The first plugin to return `true` or `false` wins — subs
|
|
|
288
288
|
|
|
289
289
|
```ts
|
|
290
290
|
User.validator({
|
|
291
|
-
plugins: [authPlugin, datePlugin, coercePlugin]
|
|
291
|
+
plugins: [authPlugin, datePlugin, coercePlugin],
|
|
292
292
|
}).validate(data, true)
|
|
293
293
|
```
|