Skip to content

Decorator that validates class properties upon object initialization. Based on class-validator package.

Notifications You must be signed in to change notification settings

JakubStiburek/validate-class-properties

Repository files navigation

Validate-class-properties

  • A simple class decorator that validates the properties of a class upon instance initialization. On validation error, the ValidationException is thrown.
  • It's based on class-validator library.
  • Repository can be found at github.

Installation

npm install validate-class-properties

Usage

import { ValidateProperties } from 'validate-class-properties';
import { IsNumber, Min } from 'class-validator';

@ValidateProperties
export class ValueObject {
    @IsNumber({ maxDecimalPlaces: 2 })
    @Min(0)
    readonly value: number;

    constructor(value: number) {
        this.value = value;
    }
}

ValidationException handling

function getInsanceOfValueObject(value: number): ValueObject {
    try {
        return new ValueObject(value);
    } catch (e) {
        if (e instanceof ValidationException) {
            // handle validation error
        }
        throw e;
    }
}

About

Decorator that validates class properties upon object initialization. Based on class-validator package.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published