Options
All
  • Public
  • Public/Protected
  • All
Menu

Number handler

Hierarchy

Index

Constructors

constructor

Properties

Protected _rules

_rules: Rule<number>[] = ...

_type

_type: number

Methods

between

  • Only allows numbers in a range between min and max

    import yxc from "@dotvirus/yxc"
    
    yxc.number().between(0, 5).validate(-1) // -> Fails
    yxc.number().between(0, 5).validate(5.5) // -> Fails
    yxc.number().between(0, 5).validate(0) // -> OK
    yxc.number().between(0, 5).validate(3.2) // -> OK
    yxc.number().between(0, 5).validate(5) // -> OK
    

    Parameters

    • min: number
    • max: number

    Returns NumberHandler

check

custom

enum

  • Only allows a selection of values

    import yxc from "@dotvirus/yxc"
    
    const allowedValues = ["test", "test2", "test3"]
    
    yxc.string().enum(allowedValues).validate("test") // -> OK
    yxc.string().enum(allowedValues).validate("asdasd") // -> Fails
    

    Parameters

    • values: number[]

    Returns NumberHandler

eq

equal

equals

  • Only allows a certain value Value is checked using strict equality (===)

    import yxc from "@dotvirus/yxc"
    
    yxc.string().equals("test").validate("test") // -> OK
    yxc.string().equals("test").validate("test2") // -> Fails
    

    Parameters

    • expected: number

    Returns NumberHandler

int

integer

  • Only allows integers (whole numbers)

    import yxc from "@dotvirus/yxc"
    
    yxc.number().validate(4.5) // -> OK
    yxc.number().integer().validate(4.5) // -> Fails
    yxc.number().integer().validate(4) // -> OK
    

    Returns NumberHandler

max

  • Only allows numbers equal to or smaller than min

    import yxc from "@dotvirus/yxc"
    
    yxc.number().max(0).validate(1) // -> Fails
    yxc.number().max(0).validate(0) // -> OK
    yxc.number().max(0).validate(-1) // -> OK
    

    Parameters

    • max: number

    Returns NumberHandler

min

  • Only allows numbers equal to or greater than min

    import yxc from "@dotvirus/yxc"
    
    yxc.number().min(0).validate(-1) // -> Fails
    yxc.number().min(0).validate(0) // -> OK
    yxc.number().min(0).validate(1) // -> OK
    

    Parameters

    • min: number

    Returns NumberHandler

natural

negative

  • Only allows numbers smaller than 0

    import yxc from "@dotvirus/yxc"
    
    yxc.number().validate(0) // -> Fails
    yxc.number().integer().validate(4) // -> Fails
    yxc.number().integer().validate(-4) // -> OK
    

    Parameters

    • Optional opts: Partial<{ withZero: boolean }>

    Returns NumberHandler

nullable

optional

positive

  • Only allows numbers greater than 0

    import yxc from "@dotvirus/yxc"
    
    yxc.number().validate(0) // -> Fails
    yxc.number().integer().validate(-4) // -> Fails
    yxc.number().integer().validate(4) // -> OK
    

    Parameters

    • Optional opts: Partial<{ withZero: boolean }>

    Returns NumberHandler

rule

test

use

validate

Generated using TypeDoc