1.2.5 • Published 1 year ago

decorators-utils v1.2.5

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

AOP Decorators

The module contain decorators that help us to apply Aspect Oriented Programming (AOP) into NodeJs project. It uses Winston as based logger

Notes:

  • It will process from top to bottom.
class SampleClass {
    @IsString()
    @Transform((x) => x.concat(' transformed'))
    @Mapping('renamedFieldName')
    @MaxStringSize(5)
    private sampleProperty: boolean;
}

// The flow will be:
//  - Validate isString
//  - Transform data
//  - Mapping field name
//  - Validate String length

Table of Contents

Installation

npm install decorators-utils

or

yarn add decorators-utils

Usage

@logContext()
class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @validate
  @logInputParams()
  public sampleMethod(@required() _params: any): any {
    this.logger.info('dummy message');
  }
}

Validator

The module supports testing for string, number, boolean ,object, array, and Test to support custom Test

String

Possible options for String: IsEmail, IsMatched, IsString, MaxStringSize, MinStringSize

//Define class schema
class SampleClass {
    @IsString()
    @IsMatched(/^dog/)
    @IsEmail()
    @MaxStringSize(5)
    @MinStringSize(1)
    private sampleProperty: string;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: 'sample' });

//Sample returned error
{
  message: 'sampleProperty must be string',
  name: 'IS_STRING',
  path: 'sampleProperty',
}

Number

Possible options for String: IsLarger, IsLess, IsNumber, MaxDigits, MinDigits

//Define class schema
class SampleClass {
    @IsLarger(2)
    @IsLess(5)
    @IsNumber()
    @MaxDigits(3)
    @MinDigits(1)
    private sampleProperty: number;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: 123 });

//Sample returned error
{
  message: 'sampleProperty must be number',
  name: 'IS_NUMBER',
  path: 'sampleProperty',
}

Boolean

Possible options for String: IsBoolean

//Define class schema
class SampleClass {
    @IsBoolean()
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: true });

//Sample returned error
{
  message: 'sampleProperty must be boolean',
  name: 'IS_BOOLEAN',
  path: 'sampleProperty',
}

Object

Possible options for String: IsValidObject

//Define class schema
class ChildClass {
  @IsString()
  private requestId: string
}

class SampleClass {
    @IsValidObject()
    private sampleProperty: ChildClass;
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: { requestId:'string' } });

//Sample returned error
{
  message: 'requestId must be string',
  name: 'IS_VALID_OBJECT',
  path: 'sampleProperty.requestId',
}

Array

Possible options for String: IsValidArray, ArraySize

//Define class schema
class ChildClass {
  @IsString()
  private requestId: string
}

class SampleClass {
    @IsValidArray(ChildClass)
    private sampleProperty: ChildClass[];
}

class SampleClass {
    @IsValidArray(String)
    private sampleProperty: String[];
}

class SampleClass {
    @IsValidArray(Number)
    private sampleProperty: Number[];
}

class SampleClass {
    @IsValidArray(Boolean)
    private sampleProperty: Boolean[];
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: [{ requestId:'string' }] });

//Sample returned error
{
  message: 'must be a valid array element',
  name: 'IS_VALID_ARRAY',
  path: 'sampleProperty[2]',
};
class SampleClass {
    @ArraySize({ max: 2, min: 1 })
    private sampleProperty: Boolean[];
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: [{ requestId:'string' }] });

//Sample returned error
{
  message: 'sampleProperty size must be less than 2, greater than 1',
  name: 'IS_VALID_ARRAY',
  path: 'sampleProperty',
};

Custom Test

//Define class schema
const canConvertStringToNum = (value: string): boolean => {
    return !!parseInt(value);
};

class SampleClass {
  @Test(canConvertStringToNum)
  private sampleProperty: string;

  public getData(): void {
    console.log(this.sampleProperty);
  }
}

//Run function to validate data based on the schema above
validateSchema(SampleClass, { sampleProperty: '123' });

//Sample returned error
{
  message: 'sampleProperty custom test failed',
  name: 'canConvertStringToNum',
  path: 'sampleProperty',
};

Transform

modify the input data

//Define class schema
class SampleClass {
    @Transform((x) => x.concat(' transformed'))
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
const data = { sampleProperty: 'sampleData' }
validateSchema(SampleClass, data);

//New data
console.log(data.sampleProperty) // => sampleData transformed

Mapping

Change field name

//Define class schema
class SampleClass {
    @Mapping('renamedFieldName')
    private sampleProperty: boolean;
}

//Run function to validate data based on the schema above
const data = { sampleProperty: 'sampleData' }
validateSchema(SampleClass, data);

//New data
console.log(data.renamedFieldName) // => sampleData transformed

LogContext

@logContext()
class SampleClass {
  public logger: ILogger = new LoggerImpl();

  public sampleMethod(_params: any): any {
    this.logger.info('dummy message');
  }
}

The message will be [SampleClass] [sampleMethod] dummy message

LogInputParam

class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @logInputParams()
  public sampleMethod(_params: any): any {
    this.logger.info('dummy message');
  }
}

When the method is called, an additional log record will appear method is called with param with the input param value

Parameters

class SampleClass {
  public logger: ILogger = new LoggerImpl();

  @validate
  public sampleMethod(@required() _params: any): any {}
}

Although _params is a required parameters, it still accept undefined value. It will throw error when @required(error), or even return boolean value (true/false) by @required(false)

CHANGELOG

CHANGELOG

1.2.5

1 year ago

1.2.5-alpha

1 year ago

1.2.3-alpha

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.3-beta

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.9

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.12

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago