https://github.com/equilaterus/simple-ts-validation-mapping
https://github.com/equilaterus/simple-ts-validation-mapping
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/equilaterus/simple-ts-validation-mapping
- Owner: equilaterus
- License: mit
- Created: 2024-06-04T03:00:29.000Z (over 1 year ago)
- Default Branch: dev
- Last Pushed: 2024-08-08T03:51:56.000Z (over 1 year ago)
- Last Synced: 2025-06-26T18:43:23.507Z (8 months ago)
- Language: TypeScript
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Typescript Validation and Mapping
* Clean **entity validation** and **auto-mapping**.
* Prevents **overposting** attacks.
* Simple and extensible.
## Usage
Define your target model:
```ts
export class TargetModel {
requiredField: string = "";
optionalField: string | null = null;
}
```
And validate it! For example, if you want to check a request on ExpressJS:
```ts
const [targetModel, isValid, validationErrors] = validateModel(TargetModel, req.body);
if (!isValid) {
res.status(400).send(validationErrors);
} else {
const result = await doSomethingWithTargetModel({
additionalFields: true,
...command,
} as targetWithAdditionalFields)
res.send(result);
}
```
For more examples, see **sample.ts**. To run this project locally simply use these commands:
```
npm install
npm start
```
## FAQ
* **How to integrate this with my project?** Just copy the contents from *validator.ts* into your project, extend and modify as required.
* **Why there is no NPM package?** This is a *base code* that you can use, extend and customize so there isn't (and probably won't be) an available NPM package.