Skip to content
On this page

Validators

ts
import { Exception, StringValidator, ValueObject } from '@sohailalam2/abu';

class MyCustomException extends Exception {}

@StringValidator.matches(/^Hello World!$/, { exceptionType: MyCustomException })
class MyValueObject extends ValueObject<string> {}

MyValueObject.from<string>('Hello World!');
import { Exception, StringValidator, ValueObject } from '@sohailalam2/abu';

class MyCustomException extends Exception {}

@StringValidator.matches(/^Hello World!$/, { exceptionType: MyCustomException })
class MyValueObject extends ValueObject<string> {}

MyValueObject.from<string>('Hello World!');

ValidatorOptions

OptionOption TypeComment
exceptionTypeClass<Exception>The given class shall be thrown upon a validation failure

Boolean Validator

DecoratorPropertiesComment
@BooleanValidator.isBoolean(options?)ValidatorOptionsEnsures that the input is a Boolean
@BooleanValidator.isTrue(options?)ValidatorOptionsEnsures that the input is true
@BooleanValidator.isFalse(options?)ValidatorOptionsEnsures that the input is false

Number Validator

DecoratorPropertiesComment
@NumberValidator.isNumber(options?)ValidatorOptionsEnsures that the input is a Number
@NumberValidator.isInteger(options?)ValidatorOptionsEnsures that the input is an integer
@NumberValidator.isPositive(options?)ValidatorOptionsEnsures that the input is a positive number
@NumberValidator.isNegative(options?)ValidatorOptionsEnsures that the input is a negative number
@NumberValidator.isLessThan(compare, options?)(number, ValidatorOptions)Ensures that the input is less than the given number
@NumberValidator.isLessThanEquals(compare, options?)(number, ValidatorOptions)Ensures that the input is less than or equal to the given number
@NumberValidator.isGreaterThan(compare, options?)(number, ValidatorOptions)Ensures that the input is greater than the given number
@NumberValidator.isGreaterThanEquals(compare, options?)(number, ValidatorOptions)Ensures that the input is greater than or equal to the given number
@NumberValidator.isEquals(compare, options?)(number, ValidatorOptions)Ensures that the input is equal to the given number

String Validator

DecoratorPropertiesComment
@StringValidator.isString(options?)ValidatorOptionsEnsures that the input is a string
@StringValidator.isEmpty(options?)ValidatorOptionsEnsures that the input is an empty string
@StringValidator.isNotEmpty(options?)ValidatorOptionsEnsures that the input is not an empty string
@StringValidator.isNumberAsString(options?)ValidatorOptionsEnsures that the input is a string representation of a number
@StringValidator.isBooleanAsString(options?)ValidatorOptionsEnsures that the input is a string representation of a boolean
@StringValidator.isDateAsString(options?)ValidatorOptionsEnsures that the input is a string representation of a date
@StringValidator.isJSON(options?)ValidatorOptionsEnsures that the input is a string representation of a json string
@StringValidator.matches(matches, options?)(RegExp, ValidatorOptions)Ensures that the input matches with the given regular expression
@StringValidator.equals(matches, options?)(string, ValidatorOptions)Ensures that the input equals to the given string
@StringValidator.contains(partial, options?)(string, ValidatorOptions)Ensures that the input contains the partial given string
@StringValidator.startsWith(partial, options?)(string, ValidatorOptions)Ensures that the input starts with the partial given string
@StringValidator.endsWith(partial, options?)(string, ValidatorOptions)Ensures that the input ends with the partial given string
@StringValidator.minLength(length, options?)(number, ValidatorOptions)Ensures that the input's length is less than the given length
@StringValidator.maxLength(length, options?)(number, ValidatorOptions)Ensures that the input's length is more than or equal to the given length

Released under the MIT License.