Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion default.project.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "Validator",
"globIgnorePaths": ["**.spec.luau"],
"tree": {
"$path": "src"
}
Expand Down
9 changes: 9 additions & 0 deletions src/Boolean/BooleanChecks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--!strict

local BooleanChecks = {}

function BooleanChecks.IsBoolean(value: any): boolean
return typeof(value) == "boolean"
end

return BooleanChecks
26 changes: 26 additions & 0 deletions src/Boolean/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--!strict

local BooleanChecks = require(script.BooleanChecks)
local CoreValidator = require(script.Parent.Core)
local Helpers = require(script.Parent.Helpers)
local ValidatorTypes = require(script.Parent.ValidatorTypes)

local BooleanValidator = {}
setmetatable(BooleanValidator, CoreValidator)
BooleanValidator.__index = BooleanValidator

function BooleanValidator.new(): ValidatorTypes.PublicBooleanValidator
local self = setmetatable(CoreValidator.new() :: any, BooleanValidator)
Helpers.AddCheck(self, BooleanChecks.IsBoolean)
return self
end

function BooleanValidator.IsBoolean(
self: ValidatorTypes.PrivateValidator
): ValidatorTypes.PublicBooleanValidator
local new = setmetatable(self :: any, BooleanValidator)
Helpers.AddCheck(new, BooleanChecks.IsBoolean)
return new
end

return BooleanValidator
23 changes: 23 additions & 0 deletions src/Core/CoreChecks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--!strict

local CoreChecks = {}

function CoreChecks.IsInTable(value: any, t: { [any]: any }): boolean
for _, v in pairs(t) do
if v == value then
return true
end
end

return false
end

function CoreChecks.IsKeyOf(value: any, t: { [any]: any }): boolean
return if t[value] ~= nil then true else false
end

function CoreChecks.IsEqual(value: any, otherValue: any): boolean
return value == otherValue
end

return CoreChecks
111 changes: 111 additions & 0 deletions src/Core/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
--!strict

local CoreChecks = require(script.CoreChecks)
local Helpers = require(script.Parent.Helpers)
local ValidatorTypes = require(script.Parent.ValidatorTypes)

local CoreValidator = {}
CoreValidator.__index = CoreValidator

local rootValidatorClass: ValidatorTypes.PrivateRootValidator

function CoreValidator.new(): ValidatorTypes.PrivateValidator
local self = {
_checksGroups = { {} },
}

local validator = setmetatable(self :: any, CoreValidator)

return validator
end

function CoreValidator.Check(self: ValidatorTypes.PrivateValidator, value: any): boolean
for _, group in pairs(self._checksGroups) do
local passed = if next(group) then true else false

local isNegated = false

for _, check in pairs(group) do
if not check then
isNegated = not isNegated
continue
end

assert(type(check) == "table", "Check is not table, which is not possible.")

local res = check._func(value, table.unpack(check._params))

if (isNegated and res) or (not isNegated and not res) then
passed = false
break
end
isNegated = false
end

if passed then
return true
end
end

return false
end

function CoreValidator.Assert(
self: ValidatorTypes.PrivateValidator,
value: any,
message: string?
): any
if not self:Check(value) then
error(message or `Value {value} does not satisfy the validator checks.`)
end

return value
end

function CoreValidator.Freeze(self: ValidatorTypes.PrivateValidator): ValidatorTypes.Checker
-- TODO add actual freeze (prohibit calling other methods)
return self
end

function CoreValidator.Or(
self: ValidatorTypes.PrivateValidator
): ValidatorTypes.PrivateRootValidator
table.insert(self._checksGroups, {})
local new = setmetatable(self :: any, rootValidatorClass)
return new
end

function CoreValidator.Not(self: ValidatorTypes.PrivateValidator): ValidatorTypes.PrivateValidator
table.insert(self._checksGroups[#self._checksGroups], false)
return self
end

function CoreValidator.IsInTable(
self: ValidatorTypes.PrivateValidator,
t: { [any]: any }
): ValidatorTypes.PrivateValidator
Helpers.AddCheck(self, CoreChecks.IsInTable, t)
return self
end

function CoreValidator.IsKeyOf(
self: ValidatorTypes.PrivateValidator,
t: { [any]: any }
): ValidatorTypes.PrivateValidator
Helpers.AddCheck(self, CoreChecks.IsKeyOf, t)
return self
end

function CoreValidator.IsEqual(
self: ValidatorTypes.PrivateValidator,
otherValue: any
): ValidatorTypes.PrivateValidator
Helpers.AddCheck(self, CoreChecks.IsEqual, otherValue)
return self
end

function CoreValidator._Setup(rootValidator: ValidatorTypes.PrivateRootValidator)
rootValidatorClass = rootValidator
end

return CoreValidator
17 changes: 17 additions & 0 deletions src/Enum/EnumChecks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--!strict

local EnumChecks = {}

function EnumChecks.IsEnumItem(value: any, enum: Enum?)
if typeof(value) ~= "EnumItem" then
return false
end

if not enum or table.find(enum:GetEnumItems(), value) then
return true
end

return false
end

return EnumChecks
27 changes: 27 additions & 0 deletions src/Enum/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--!strict

local EnumChecks = require(script.EnumChecks)
local CoreValidator = require(script.Parent.Core)
local Helpers = require(script.Parent.Helpers)
local ValidatorTypes = require(script.Parent.ValidatorTypes)

local EnumValidator = {}
setmetatable(EnumValidator, CoreValidator)
EnumValidator.__index = EnumValidator

function EnumValidator.new(enum: Enum?): ValidatorTypes.PublicEnumValidator
local self = setmetatable(CoreValidator.new() :: any, EnumValidator)
Helpers.AddCheck(self, EnumChecks.IsEnumItem, enum)
return self
end

function EnumValidator.IsEnumItem(
self: ValidatorTypes.PrivateValidator,
enum: Enum?
): ValidatorTypes.PrivateEnumValidator
local new = setmetatable(self :: any, EnumValidator)
Helpers.AddCheck(new, EnumChecks.IsEnumItem, enum)
return new
end

return EnumValidator
18 changes: 18 additions & 0 deletions src/Helpers/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--!strict

local ValidatorTypes = require(script.Parent.ValidatorTypes)

local Helpers = {}

function Helpers.AddCheck(
validator: ValidatorTypes.PrivateValidator,
func: (any, ...any) -> boolean,
...
)
table.insert(validator._checksGroups[#validator._checksGroups], {
_func = func,
_params = table.pack(...),
})
end

return Helpers
38 changes: 38 additions & 0 deletions src/Instance/InstanceChecks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--!strict

local ValidatorTypes = require(script.Parent.Parent.ValidatorTypes)

local InstanceChecks = {}

function InstanceChecks.IsA(value: any, instanceClass: string): boolean
return typeof(value) == "Instance" and value:IsA(instanceClass)
end

function InstanceChecks.IsClass(value: any, instanceClass: string): boolean
return typeof(value) == "Instance" and value.ClassName == instanceClass
end

function InstanceChecks.IsProperty(
value: Instance,
property: string,
checker: ValidatorTypes.Checker
): boolean
local success, result = pcall(function(): boolean
return checker:Check((value :: any)[property])
end)
return if success and result then true else false
end

function InstanceChecks.IsAttribute(
value: Instance,
attribute: string,
checker: ValidatorTypes.Checker
): boolean
return checker:Check(value:GetAttribute(attribute))
end

function InstanceChecks.HasTag(value: Instance, tag: string): boolean
return value:HasTag(tag)
end

return InstanceChecks
72 changes: 72 additions & 0 deletions src/Instance/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--!strict

local InstanceChecks = require(script.InstanceChecks)
local CoreValidator = require(script.Parent.Core)
local Helpers = require(script.Parent.Helpers)
local ValidatorTypes = require(script.Parent.ValidatorTypes)

local InstanceValidator = {}
setmetatable(InstanceValidator, CoreValidator)
InstanceValidator.__index = InstanceValidator

function InstanceValidator.newIsAnInstance(
instanceClass: string
): ValidatorTypes.PublicInstanceValidator
local self = setmetatable(CoreValidator.new() :: any, InstanceValidator)
Helpers.AddCheck(self, InstanceChecks.IsA, instanceClass)
return self
end

function InstanceValidator.newIsAnInstanceStrict(
instanceClass: string
): ValidatorTypes.PublicInstanceValidator
local self = setmetatable(CoreValidator.new() :: any, InstanceValidator)
Helpers.AddCheck(self, InstanceChecks.IsClass, instanceClass)
return self
end

function InstanceValidator.IsAnInstance(
self: ValidatorTypes.PrivateValidator,
instanceClass: string
): ValidatorTypes.PrivateInstanceValidator
local new = setmetatable(self :: any, InstanceValidator)
Helpers.AddCheck(new, InstanceChecks.IsA, instanceClass)
return new
end

function InstanceValidator.IsAnInstanceStrict(
self: ValidatorTypes.PrivateValidator,
instanceClass: string
): ValidatorTypes.PrivateInstanceValidator
local new = setmetatable(self :: any, InstanceValidator)
Helpers.AddCheck(new, InstanceChecks.IsClass, instanceClass)
return new
end

function InstanceValidator.IsProperty(
self: ValidatorTypes.PrivateInstanceValidator,
property: string,
checker: ValidatorTypes.Checker
): ValidatorTypes.PrivateInstanceValidator
Helpers.AddCheck(self, InstanceChecks.IsProperty, property, checker)
return self
end

function InstanceValidator.IsAttribute(
self: ValidatorTypes.PrivateInstanceValidator,
attribute: string,
checker: ValidatorTypes.Checker
): ValidatorTypes.PrivateInstanceValidator
Helpers.AddCheck(self, InstanceChecks.IsAttribute, attribute, checker)
return self
end

function InstanceValidator.HasTag(
self: ValidatorTypes.PrivateInstanceValidator,
tag: string
): ValidatorTypes.PrivateInstanceValidator
Helpers.AddCheck(self, InstanceChecks.HasTag, tag)
return self
end

return InstanceValidator
9 changes: 9 additions & 0 deletions src/Nan/NanChecks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
--!strict

local NanChecks = {}

function NanChecks.IsNan(value: any): boolean
return typeof(value) == "number" and value ~= value -- NaN check
end

return NanChecks
26 changes: 26 additions & 0 deletions src/Nan/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--!strict

local NanChecks = require(script.NanChecks)
local CoreValidator = require(script.Parent.Core)
local Helpers = require(script.Parent.Helpers)
local ValidatorTypes = require(script.Parent.ValidatorTypes)

local NanValidator = {}
setmetatable(NanValidator, CoreValidator)
NanValidator.__index = NanValidator

function NanValidator.new(): ValidatorTypes.PublicNanValidator
local self = setmetatable(CoreValidator.new() :: any, NanValidator)
Helpers.AddCheck(self, NanChecks.IsNan)
return self
end

function NanValidator.IsNan(
self: ValidatorTypes.PrivateValidator
): ValidatorTypes.PublicNanValidator
local new = setmetatable(self :: any, NanValidator)
Helpers.AddCheck(new, NanChecks.IsNan)
return new
end

return NanValidator
Loading