Skip to content

New Validator Decorator: @IsNotEmpty #22

@waji-io

Description

@waji-io

I have created a custom validator that others might find useful as well.
It covers the use case of a property not being empty, null or undefined.

export type ValidatorOptionType = {
    message?: string;
}

export const IsNotEmpty = ({ message = 'Property must not be empty' }: ValidatorOptionType = {}): PropertyDecorator =>
  createDecorator(
    (prop: unknown) => prop !== '' && prop !== null && prop !== undefined,
    { errorMessage: message, constraints: [] }
  );

Here is an example of how to use it:

@IsNotEmpty({ message: MSG_VALIDATION.required })
initials: string;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions