0.3.2 • Published 5 months ago

@axel669/joker v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Joker

Validation (and soon masking) library that is small, fast, and simple.

TODO

  • add some more built in validators
  • allow custom validators
  • maybe other stuff if people suggest it

Usage

import joker from "@axel669/joker"

const validate = joker.compile(schema)
console.log(
    validate(data)
)

Schema

const schema = {
    //  root defines the top level the data
    root: {
        //  use object syntax for object validation
        //  with the key being the validator function to use
        "id": joker.number(),
        //  start a key name with "?" to mark as nullable (can be null/undefined)
        "?thing": joker.bool(),
        //  nested objects generate nested checks with the validator
        "nested": {
            "tagged": joker.bool()
        },
        //  keys with "[]" are checked as arrays, with each item using the
        //  given schema for validation
        "wat[]": {
            "name": joker.string({max: 20}),
            "count": joker.number({min: 5}),
        },
        //  an array marked with "[?]" allows individual items to be nullable
        "tags[?]": joker.string({length: 10})
    }
}

Built-in Validators

  • int {min, max}
  • number {min, max}
  • string {min, max, length}
  • bool

Custom Validators

Joker contains a utility to help make custom validators. Custom validator functions should return a string with the code to run for the the validation; Specifically, the code returned is used to mark a value as invalid. The function to generate the code will be given a name argument that represents the value being checked. The joker.buildType function can be used to help create custom validators that can take options for extended validation.

const hi = joker.buildType(
    //  name of the type to display in validation errors
    "hi",
    //  basic validation of the type (regardless of options)
    (name) => `typeof ${name} !== "string" || ${name}.startsWith("hi") === false`,
    //  any number of [key, function] pairs for options that can generate
    //  more specific validation of a type
    ["end", (name, optionValue) => `${name}.endsWith("${optionValue}") === false`]
)
0.3.3-beta.0

5 months ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.0-beta.0

2 years ago

0.1.0

2 years ago