Skip to content

Commit

Permalink
Dev (#10)
Browse files Browse the repository at this point in the history
* More descriptive docs for path_or_nil and adding luacheck lints

* Have to run luarocks install separately for each package

* Adding more contributing docs

* Adding link to valid.lua

* Conistent function calling conventions in docs

* Adding allof and anyof validation functions

* Adding optional case-insensitive comparisons for string literals

* Consistent notes in readme

* Consistent notes in readme

* Adding more examples for comparing table literals

* Removing extraneous newline

* Return the path_or_nil for custom functions

* * Indicate supported Lua versions
* Add license preamble to valid.lua
* Add reference optimizations for non-JIT'd Lua versions
* Explicitely indicate what is returned from every function
* Add docstrings to all functions in valid.lua

* * Add valid.boolean() function
* Allow implicit literals to be used in valid.anyof and valid.allof

* LuaJIT has stopped publishing tarballs on LuaJIT.org, so the workflow has broken.  In the meantime, we'll just test against OpenResty's LuaJIT distribution.
  • Loading branch information
benwilber authored Aug 17, 2024
1 parent 03a3097 commit 4bf4192
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
luaVersion: ["5.1", "5.2", "5.3", "5.4", "luajit"]
luaVersion: ["5.1", "5.2", "5.3", "5.4", "luajit-openresty"]

steps:
- name: Checkout code
Expand Down
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ lint:

test:
busted tests.lua

34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ A library for Lua to validate various values and table structures.
## Table of Contents

- [Features](#features)
- [Supported Lua Versions](#supported-lua-versions)
- [Installation](#installation)
- [Basic Usage](#basic-usage)
- [Validating Simple Data Types](#validating-simple-data-types)
- [Validating Complex Data Types](#validating-complex-data-types)
- [Validation Definition Functions](#validation-definition-functions)
- [`valid.literal`](#validliteral)
- [`valid.boolean`](#validboolean)
- [`valid.number`](#validnumber)
- [`valid.string`](#validstring)
- [`valid.table`](#validtable)
Expand All @@ -32,6 +34,15 @@ A library for Lua to validate various values and table structures.
- Detailed error reporting with paths to invalid keys or indices.
- Nested validations for complex table structures.

## Supported Lua Versions

`valid.lua` is tested with:

- Lua 5.1 (including LuaJIT)
- Lua 5.2
- Lua 5.3
- Lua 5.4

## Installation

Copy the [`valid.lua`](valid.lua) file to a directory in your `LUA_PATH`.
Expand Down Expand Up @@ -132,6 +143,29 @@ assert(is_valid) -- true
* `func`: A custom validation function to call after the literal check.


### `valid.boolean`

Validates that a value is a literal boolean either `true` or `false`.

This is a shorthand for `valid.anyof {true, false}`.

#### Usage

```lua
local valid = require "valid"

local is_valid = valid.boolean()(true)
assert(is_valid) -- true

local is_valid = valid.boolean()("false")
assert(not is_valid) -- false, not the literal boolean false
```

#### Parameters

*(none)*


### `valid.number`

Validates that a value is a number within an optional range.
Expand Down
Loading

0 comments on commit 4bf4192

Please sign in to comment.