|
| 1 | +-- This file is part of spec.lua |
| 2 | +-- |
| 3 | +-- Repository: https://github.com/atomicptr/spec.lua |
| 4 | +-- |
| 5 | +-- License: |
| 6 | +-- |
| 7 | +-- Copyright 2025 Christopher Kaster <[email protected]> |
| 8 | +-- |
| 9 | +-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | +-- documentation files (the “Software”), to deal in the Software without restriction, including without limitation |
| 11 | +-- the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 12 | +-- and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | +-- |
| 14 | +-- The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 15 | +-- of the Software. |
| 16 | +-- |
| 17 | +-- THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 18 | +-- TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | +-- THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 20 | +-- CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 | +-- DEALINGS IN THE SOFTWARE. |
| 22 | + |
| 23 | +local M = {} |
| 24 | + |
| 25 | +-- predicates |
| 26 | + |
| 27 | +---Tests if value is a string |
| 28 | +---@param value any |
| 29 | +---@return boolean |
| 30 | +function M.string(value) |
| 31 | + return type(value) == "string" |
| 32 | +end |
| 33 | + |
| 34 | +---Tests if value is a number |
| 35 | +---@param value any |
| 36 | +---@return boolean |
| 37 | +function M.number(value) |
| 38 | + return type(value) == "number" |
| 39 | +end |
| 40 | + |
| 41 | +---Tests if value is a boolean |
| 42 | +---@param value any |
| 43 | +---@return boolean |
| 44 | +function M.boolean(value) |
| 45 | + return type(value) == "boolean" |
| 46 | +end |
| 47 | + |
| 48 | +---Tests if value is a table |
| 49 | +---@param value any |
| 50 | +---@return boolean |
| 51 | +function M.table(value) |
| 52 | + return type(value) == "table" |
| 53 | +end |
| 54 | + |
| 55 | +---Tests if value is nil |
| 56 | +---@param value any |
| 57 | +---@return boolean |
| 58 | +function M.null(value) |
| 59 | + return value == nil |
| 60 | +end |
| 61 | + |
| 62 | +---Tests if value exists |
| 63 | +---@param value any |
| 64 | +---@return boolean |
| 65 | +function M.some(value) |
| 66 | + return value ~= nil |
| 67 | +end |
| 68 | + |
| 69 | +---Test if table matches a shape |
| 70 | +---@param spec_map table<string, fun(value: any): boolean> |
| 71 | +---@return fun(value: any): boolean |
| 72 | +function M.keys(spec_map) |
| 73 | + return function(value) |
| 74 | + if not M.table(value) then |
| 75 | + return false |
| 76 | + end |
| 77 | + |
| 78 | + for key, sub_spec in pairs(spec_map) do |
| 79 | + local val = value[key] |
| 80 | + |
| 81 | + if val == nil or not (type(sub_spec) == "function" and sub_spec(val)) then |
| 82 | + return false |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + return true |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +---Check if a value matches with the given spec |
| 91 | +---@param spec fun(value: any): boolean |
| 92 | +---@param value any |
| 93 | +---@return boolean |
| 94 | +function M.valid(spec, value) |
| 95 | + if type(spec) ~= "function" then |
| 96 | + error "Spec must be a function (e.g. spec.keys for tables)" |
| 97 | + end |
| 98 | + |
| 99 | + return spec(value) |
| 100 | +end |
| 101 | + |
| 102 | +---Returns value if it matches the spec, nil otherwise |
| 103 | +---@generic T |
| 104 | +---@param spec fun(value: any): boolean |
| 105 | +---@param value T |
| 106 | +---@return T|nil |
| 107 | +function M.conform(spec, value) |
| 108 | + if M.valid(spec, value) then |
| 109 | + return value |
| 110 | + end |
| 111 | + |
| 112 | + return nil |
| 113 | +end |
| 114 | + |
| 115 | +---Asserts that a spec is valid |
| 116 | +---@param spec any |
| 117 | +---@param value any |
| 118 | +function M.assert(spec, value) |
| 119 | + assert(M.valid(spec, value)) |
| 120 | +end |
| 121 | + |
| 122 | +return M |
0 commit comments