Validator
Table of contents
Public class
class Validator<T> implements Validatable<T> {}
packages/validate/src/lib/validation.ts
Constructor
Public constructor
Constructs a new instance of the Validator
class
Name | Description |
---|---|
|
|
Properties
validationFn
Public readonly property
readonly validationFn: ValidatorFn<T>
Methods
check()
Public method
Refine this validation with an additional check that doesn't change the resulting value.
Example
const numberLessThan10Validator = T.number.check((value) => {
if (value >= 10) {
throw new ValidationError(`Expected number less than 10, got ${value}`)
}
})
check(name: string, checkFn: (value: T) => void): Validator<T>
Name | Description |
---|---|
|
|
|
|
Validator<T>
check()
Public method
check(checkFn: (value: T) => void): Validator<T>
Name | Description |
---|---|
|
|
Validator<T>
isValid()
Public method
Checks that the passed value is of the correct type.
isValid(value: unknown): value is T
Name | Description |
---|---|
|
|
value is T
nullable()
Public method
Returns a new validator that also accepts null or undefined. The resulting value will always be null.
nullable(): Validator<null | T>
optional()
Public method
Returns a new validator that also accepts null or undefined. The resulting value will always be null.
optional(): Validator<T | undefined>
refine()
Public method
Refine this validation to a new type. The passed-in validation function should throw an error if the value can't be converted to the new type, or return the new type otherwise.
refine<U>(otherValidationFn: (value: T) => U): Validator<U>
Name | Description |
---|---|
|
|
Validator<U>
validate()
Public method
Asserts that the passed value is of the correct type and returns it. The returned value is guaranteed to be referentially equal to the passed value.
validate(value: unknown): T
Name | Description |
---|---|
|
|
T