metatablesnow/issa

A runtime type validator with pretty errors and real types



A runtime type validator with pretty errors and real types

Github · Wally Page · Pesde Page



Info

Issa is in beta, expect bugs and API changes!

Features

  • Types from runtime checks
  • Pretty yet precise errors
  • Small, composable predicates

Installation

Wally

Add Issa to your wally.toml dependencies list:

[dependencies]
Is = "metatablesnow/[email protected]"

Pesde

Add Issa to your pesde.toml dependencies list:

[dependencies]
Is = { name = "metatablesnow/issa", version = "0.2.0" }

Manual

Copy and paste the source code into a module script.

Guide

Every function in Is returns a Predicate.

local Is = require(path.to.Issa)

IsAge = Is.Number()

You can call a predicate:

local age = IsAge(18) -- This will pass, and return the input (18)
IsAge("Oops!") -- This will error

Try a predicate:

local success, data = IsAge:Try("Oops!")
-- success is false, and data is the error message

local success, data = IsAge:Try(18)
-- success is true, and data is the input (18)

Type a predicate:

type Age = typeof(IsAge.Type)

local bobsAge: Age = "Oops!" -- This will warn you!

use them to build more complex predicates:

local IsAnimal = Is.Interface({
    Name = Is.String(),
    Age = IsAge
}) 

or even refine them:

local IsAdult = Is.Refine(IsAnimal, function(self, animal)
    -- Refinements run last, so no need to check if this is a table!
    if animal.Age < 18 then
        return Is.Fail(animal, "number over 18", "animals age is under 18", animal, "Age", "field")
    end

    return Is.Pass()
end)

IsAdult(13) -- This will error!