https://github.com/ivandotv/validar-decorators
Decorators for the validar package. https://github.com/ivandotv/validar
https://github.com/ivandotv/validar-decorators
object-schema typescript validation
Last synced: 11 months ago
JSON representation
Decorators for the validar package. https://github.com/ivandotv/validar
- Host: GitHub
- URL: https://github.com/ivandotv/validar-decorators
- Owner: ivandotv
- License: mit
- Created: 2019-09-14T11:02:47.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2025-07-29T21:50:23.000Z (11 months ago)
- Last Synced: 2025-07-30T00:01:25.983Z (11 months ago)
- Topics: object-schema, typescript, validation
- Language: TypeScript
- Homepage:
- Size: 594 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 19
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Validar Decorators
[](https://circleci.com/gh/ivandotv/validar-decorators)
[](https://codecov.io/gh/ivandotv/validar-decorators)

[](https://www.npmjs.com/package/validar-decorators)
Decorators for the [validar package](https://github.com/ivandotv/validar).
## Install
```js
npm install validar-decorators
```
As decorators are a part of future [ECMAScript standard](https://github.com/tc39/proposals) they can only be used with transpilers such as [Babel](http://babeljs.io/) or [Typescript](https://www.typescriptlang.org/).
This package is written in typescript.
## Example
```js
// person.ts
import { isValid, validateClass, validateClassAsync } from 'validar-decorators'
import { validation } from 'validar'
class Person {
// @isValid decorator accepts validation
@isValid(validation(() => true))
public name: string
// or array of validations
@isValid([validation(() => true),validation(() => true)])
public lastName: string
@isValid({
address: {
street: validation(() => true),
appartmentNumber: validation(() => true),
},
city: validation(() => true),
country: validation(() => false),
})
public location: Location
}
```
```js
// application.ts
const person = new Person()
person.name = 'Sam'
person.lastName = 'Fisher'
person.location = {
address: {
street: 'Beverly Hills',
appartmentNumber: 33,
city: 'LA',
country: 'USA',
},
}
// actual validation step
const result = validateClass(person)
// if you have async tests
validateClassAsync(person).then(result => {
console.log(result)
})
```
### Static properties
You can also validate static properties.
```js
class Person {
static totalCount: number = 3000
static males: number = 1500
static females: number = 1500
}
const result = validateClass(Person)
// if you have async tests
validateClassAsync(person).then(result => {
console.log(result)
})
```
### Working with subclasses
Subclasses inherit validation checks from the parent class, but they can also override them.
```js
class Person {
@isValid(validation(() => true))
name: string = 'Sam'
@isValid(validation(() => true))
lastName: string
}
class Pilot extends Person {
// override validation
@isValid(validation(() => false))
name: string
// override validation
@isValid(validation(() => false))
lastName: string
@isValid(validation(() => false))
nick: string
}
const pilot = new Pilot()
pilot.name = 'Pete'
pilot.lastName = 'Mitchell'
pilot.nick = 'Maveric'
// actual validation step
const result = validateClass(pilot)
```
For more info about [`validar`](https://ivandotv.github.io/validar/) package check out the [documentation](https://ivandotv.github.io/validar/)
### API docs
`Validar Decorators` is written in TypeScript, [auto generated API docs](https://github.com/ivandotv/validar-decorators/blob/master/docs/api/README.md) are available.
##### Author
- **Ivan Vlatković**
##### License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details