Validator

A lightweight value validator.

API

BooleanSchema

Methods

  • equals
  • notEquals
import { boolean } from "deno.land/x/validator/mod.ts"

const schema = boolean()
const value = true
schema.validate(value) // returns { valid: true, value: true, error: null }

NumberSchema

Methods

  • equals
  • notEquals
  • min
  • max
import { number } from "deno.land/x/validator/mod.ts"

const schema = number().min(1).max(100)
const value = 10
schema.validate(value) // returns { valid: true, value: 10, error: null }

StringSchema

Methods

  • equals

  • notEquals

  • length

  • min

  • max

  • match

  • includes

  • trim

import { string } from "deno.land/x/validator/mod.ts"

const schema = string().equals("foo")
const value = "foo"
schema.validate(value) // returns { valid: true, value: "foo", error: null }

DateSchema

Methods

  • equals
  • notEquals
  • min
  • max
import { date } from "deno.land/x/validator/mod.ts"

const schema = date().min(new Date(2010)).max(new Date(2020))
const value = new Date(2015)
schema.validate(value) // returns { valid: true, value: "foo", error: null }

ObjectSchema

Methods

  • strip
import { object, number, string } from "deno.land/x/validator/mod.ts"

const schema = object({
  foo: boolean(),
  bar: string()
})

const value = {
  foo: true,
  bar: "doe"
}
schema.validate(value) // returns { valid: true, value: { foo: true, bar: "doe" }, error: null }

ArraySchema

Methods

  • length
  • min
  • max
import { object, number, string } from "deno.land/x/validator/mod.ts"

const schema = array(string())

const value = ["foo", "bar"]

schema.validate(value) // returns { valid: true, value:  ["foo", "bar"], error: null }

or

import { object, number, string } from "deno.land/x/validator/mod.ts"

const schema = array([string(), number()])

const value = ["foo", 22]

schema.validate(value) // returns { valid: true, value: ["foo", 22], error: null }