@atscript/moost-validator 0.1.38 → 0.1.39
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 +26 -79
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,108 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://atscript.moost.org/logo.svg" alt="Atscript" width="120" />
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">@atscript/moost-validator</h1>
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
<p align="center">
|
|
8
|
+
<strong>Define your models once</strong> — get TypeScript types, runtime validation, and DB metadata from a single <code>.as</code> model.
|
|
9
|
+
</p>
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
- 🧩 **Nice errors out of the box** – interceptor converts `ValidatorError` → `HttpError(400)`.
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://atscript.moost.org">Documentation</a> · <a href="https://atscript.moost.org/packages/moost-validator/">Moost Validator Guide</a>
|
|
13
|
+
</p>
|
|
13
14
|
|
|
14
15
|
---
|
|
15
16
|
|
|
17
|
+
Drop-in Atscript validation for the [Moost](https://moost.org) framework. Automatically validates handler parameters against `.as` model constraints — no manual `validate()` calls needed.
|
|
18
|
+
|
|
16
19
|
## Installation
|
|
17
20
|
|
|
18
21
|
```bash
|
|
19
|
-
npm i @atscript/moost-validator
|
|
20
|
-
# Or
|
|
21
22
|
pnpm add @atscript/moost-validator
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
## Quick start
|
|
25
|
+
Peer dependencies: `moost`, `@moostjs/event-http`, `@atscript/core`, `@atscript/typescript`.
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
#### a) Globally – affects every parameter/property
|
|
27
|
+
## Quick Start
|
|
31
28
|
|
|
32
29
|
```ts
|
|
33
30
|
import { Moost } from 'moost'
|
|
34
|
-
import { validatorPipe } from '@atscript/moost-validator'
|
|
31
|
+
import { validatorPipe, validationErrorTransform } from '@atscript/moost-validator'
|
|
35
32
|
|
|
36
33
|
const app = new Moost()
|
|
37
34
|
app.applyGlobalPipes(validatorPipe())
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
#### b) Per controller / handler
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
import { Controller, Pipe } from 'moost'
|
|
44
|
-
import { Post, Body } from '@moostjs/event-http'
|
|
45
|
-
import { UseValidatorPipe } from '@atscript/moost-validator'
|
|
46
|
-
import { CreateUserDto } from './user.dto.as'
|
|
47
|
-
|
|
48
|
-
@UseValidatorPipe() // controller‑wide
|
|
49
|
-
@Controller('users')
|
|
50
|
-
export class UsersController {
|
|
51
|
-
@Post()
|
|
52
|
-
@UseValidatorPipe() // or per‑method
|
|
53
|
-
async create(@Body() dto: CreateUserDto) {}
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
### 2. Catch validation errors (optional)
|
|
58
|
-
|
|
59
|
-
Global:
|
|
60
|
-
|
|
61
|
-
```ts
|
|
62
|
-
import { validationErrorTransform } from '@atscript/moost-validator'
|
|
63
|
-
|
|
64
35
|
app.applyGlobalInterceptors(validationErrorTransform())
|
|
65
36
|
```
|
|
66
37
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
import { UseValidationErrorTransform } from '@atscript/moost-validator'
|
|
71
|
-
|
|
72
|
-
@Post()
|
|
73
|
-
@UseValidationErrorTransform()
|
|
74
|
-
async create(@Body() dto: CreateUserDto) {}
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
---
|
|
38
|
+
Any handler parameter typed with an Atscript-compiled class is now automatically validated. On failure, a `400 Bad Request` response is returned with structured error details.
|
|
78
39
|
|
|
79
|
-
##
|
|
80
|
-
|
|
81
|
-
| Export | Type | Description |
|
|
82
|
-
| ------------------------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
83
|
-
| `validatorPipe(opts?)` | `TPipeFn` | Low‑level factory. Returns a pipe that runs `type.validator(opts).validate(value)` on the argument **if** the type was produced by atscript. Registered with priority `VALIDATE`. |
|
|
84
|
-
| `UseValidatorPipe(opts?)` | `Decorator` | Sugar over `validatorPipe`. Apply to a class, method, parameter, or property. |
|
|
85
|
-
| `validationErrorTransform()` | `TInterceptorFn` | Catches `ValidatorError`, wraps it into `HttpError(400)` with `{ message, statusCode, _body }`. Priority `CATCH_ERROR`. |
|
|
86
|
-
| `UseValidationErrorTransform()` | `Decorator` | Sugar over `validationErrorTransform()`. |
|
|
87
|
-
|
|
88
|
-
### `opts` (`Partial<TValidatorOptions>`)
|
|
89
|
-
|
|
90
|
-
Any options accepted by `atscript.validator(opts)`. E.g. `{ abortEarly: false }`.
|
|
91
|
-
|
|
92
|
-
---
|
|
40
|
+
## Features
|
|
93
41
|
|
|
94
|
-
|
|
42
|
+
- **Automatic validation** — runs during the `VALIDATE` pipeline stage before business logic
|
|
43
|
+
- **Composable** — apply globally or per-controller/handler via `@UseValidatorPipe()`
|
|
44
|
+
- **Clean error responses** — interceptor converts `ValidatorError` to `HttpError(400)`
|
|
45
|
+
- **Validator options** — `partial`, `unknownProps`, `errorLimit`, `plugins`
|
|
46
|
+
- **Zero runtime dependencies** — everything via peer deps
|
|
95
47
|
|
|
96
|
-
|
|
97
|
-
2. If the declared type has a `validator()` factory (i.e. it was generated from
|
|
98
|
-
`.as` file with atscript), the pipe instantiates the validator **once** and
|
|
99
|
-
runs `validate(value)`.
|
|
100
|
-
3. On failure the validator throws `ValidatorError`.
|
|
101
|
-
4. **Interceptor** catches that error and converts it to a standard Moost
|
|
102
|
-
`HttpError` so your REST adapter sends a clean `400 Bad Request` body.
|
|
48
|
+
## Documentation
|
|
103
49
|
|
|
104
|
-
|
|
50
|
+
- [Moost Validator Guide](https://atscript.moost.org/packages/moost-validator/)
|
|
51
|
+
- [Full Documentation](https://atscript.moost.org)
|
|
105
52
|
|
|
106
53
|
## License
|
|
107
54
|
|
|
108
|
-
|
|
55
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/moost-validator",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.39",
|
|
4
4
|
"description": "Validator pipe and utils for Moost.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@moostjs/event-http": "^0.6.2",
|
|
45
45
|
"moost": "^0.6.2",
|
|
46
|
-
"@atscript/core": "^0.1.
|
|
47
|
-
"@atscript/typescript": "^0.1.
|
|
46
|
+
"@atscript/core": "^0.1.39",
|
|
47
|
+
"@atscript/typescript": "^0.1.39"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"pub": "pnpm publish --access public",
|