diff --git a/default.project.json b/default.project.json index c33ddbb..2385a21 100644 --- a/default.project.json +++ b/default.project.json @@ -1,6 +1,5 @@ { "name": "Validator", - "globIgnorePaths": ["**.spec.luau"], "tree": { "$path": "src" } diff --git a/src/Boolean/BooleanChecks.lua b/src/Boolean/BooleanChecks.lua new file mode 100644 index 0000000..2cbb30a --- /dev/null +++ b/src/Boolean/BooleanChecks.lua @@ -0,0 +1,9 @@ +--!strict + +local BooleanChecks = {} + +function BooleanChecks.IsBoolean(value: any): boolean + return typeof(value) == "boolean" +end + +return BooleanChecks diff --git a/src/Boolean/init.lua b/src/Boolean/init.lua new file mode 100644 index 0000000..5836a5c --- /dev/null +++ b/src/Boolean/init.lua @@ -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 diff --git a/src/Core/CoreChecks.lua b/src/Core/CoreChecks.lua new file mode 100644 index 0000000..aaa06be --- /dev/null +++ b/src/Core/CoreChecks.lua @@ -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 diff --git a/src/Core/init.lua b/src/Core/init.lua new file mode 100644 index 0000000..f68d02c --- /dev/null +++ b/src/Core/init.lua @@ -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 diff --git a/src/Enum/EnumChecks.lua b/src/Enum/EnumChecks.lua new file mode 100644 index 0000000..82c8033 --- /dev/null +++ b/src/Enum/EnumChecks.lua @@ -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 diff --git a/src/Enum/init.lua b/src/Enum/init.lua new file mode 100644 index 0000000..cd55590 --- /dev/null +++ b/src/Enum/init.lua @@ -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 diff --git a/src/Helpers/init.lua b/src/Helpers/init.lua new file mode 100644 index 0000000..a2db2a2 --- /dev/null +++ b/src/Helpers/init.lua @@ -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 diff --git a/src/Instance/InstanceChecks.lua b/src/Instance/InstanceChecks.lua new file mode 100644 index 0000000..8671660 --- /dev/null +++ b/src/Instance/InstanceChecks.lua @@ -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 diff --git a/src/Instance/init.lua b/src/Instance/init.lua new file mode 100644 index 0000000..9f62c65 --- /dev/null +++ b/src/Instance/init.lua @@ -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 diff --git a/src/Nan/NanChecks.lua b/src/Nan/NanChecks.lua new file mode 100644 index 0000000..62bf660 --- /dev/null +++ b/src/Nan/NanChecks.lua @@ -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 diff --git a/src/Nan/init.lua b/src/Nan/init.lua new file mode 100644 index 0000000..030baf4 --- /dev/null +++ b/src/Nan/init.lua @@ -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 diff --git a/src/Nil/NilChecks.lua b/src/Nil/NilChecks.lua new file mode 100644 index 0000000..df10d83 --- /dev/null +++ b/src/Nil/NilChecks.lua @@ -0,0 +1,9 @@ +--!strict + +local NilChecks = {} + +function NilChecks.IsNil(value: number): boolean + return value == nil +end + +return NilChecks diff --git a/src/Nil/init.lua b/src/Nil/init.lua new file mode 100644 index 0000000..1c45408 --- /dev/null +++ b/src/Nil/init.lua @@ -0,0 +1,26 @@ +--!strict + +local NilChecks = require(script.NilChecks) +local CoreValidator = require(script.Parent.Core) +local Helpers = require(script.Parent.Helpers) +local ValidatorTypes = require(script.Parent.ValidatorTypes) + +local NilValidator = {} +setmetatable(NilValidator, CoreValidator) +NilValidator.__index = NilValidator + +function NilValidator.new(): ValidatorTypes.PublicNilValidator + local self = setmetatable(CoreValidator.new() :: any, NilValidator) + Helpers.AddCheck(self, NilChecks.IsNil) + return self +end + +function NilValidator.IsNil( + self: ValidatorTypes.PrivateValidator +): ValidatorTypes.PublicNilValidator + local new = setmetatable(self :: any, NilValidator) + Helpers.AddCheck(new, NilChecks.IsNil) + return new +end + +return NilValidator diff --git a/src/Number/NumberChecks.lua b/src/Number/NumberChecks.lua new file mode 100644 index 0000000..6d65534 --- /dev/null +++ b/src/Number/NumberChecks.lua @@ -0,0 +1,17 @@ +--!strict + +local NumberChecks = {} + +function NumberChecks.IsNumber(value: any): boolean + return typeof(value) == "number" and value == value -- NaN check +end + +function NumberChecks.IsInteger(value: any): boolean + return NumberChecks.IsNumber(value) and (value % 1 == 0) +end + +function NumberChecks.IsGreater(value: number, param: number): boolean + return value > param +end + +return NumberChecks diff --git a/src/Number/init.lua b/src/Number/init.lua new file mode 100644 index 0000000..6feeccf --- /dev/null +++ b/src/Number/init.lua @@ -0,0 +1,49 @@ +--!strict + +local NumberChecks = require(script.NumberChecks) +local CoreValidator = require(script.Parent.Core) +local Helpers = require(script.Parent.Helpers) +local ValidatorTypes = require(script.Parent.ValidatorTypes) + +export type PrivateValidator = ValidatorTypes.PrivateValidator +export type PrivateNumberValidator = ValidatorTypes.PrivateNumberValidator + +local NumberValidator = {} +setmetatable(NumberValidator, CoreValidator) +NumberValidator.__index = NumberValidator + +function NumberValidator.newIsNumber(): ValidatorTypes.PublicNumberValidator + local self = setmetatable(CoreValidator.new() :: any, NumberValidator) + Helpers.AddCheck(self, NumberChecks.IsNumber) + return self +end + +function NumberValidator.newIsInteger(): ValidatorTypes.PublicNumberValidator + local self = setmetatable(CoreValidator.new() :: any, NumberValidator) + Helpers.AddCheck(self, NumberChecks.IsInteger) + return self +end + +function NumberValidator.IsNumber(self: PrivateValidator): PrivateNumberValidator + local new = setmetatable(self :: any, NumberValidator) + Helpers.AddCheck(new, NumberChecks.IsNumber) + return new +end + +function NumberValidator.IsInteger( + self: ValidatorTypes.PrivateValidator? +): ValidatorTypes.PrivateNumberValidator + local new = setmetatable(self :: any, NumberValidator) + Helpers.AddCheck(new, NumberChecks.IsInteger) + return new +end + +function NumberValidator.IsGreater( + self: ValidatorTypes.PrivateNumberValidator, + than: number +): ValidatorTypes.PrivateNumberValidator + Helpers.AddCheck(self, NumberChecks.IsGreater, than) + return self +end + +return NumberValidator diff --git a/src/Root/init.lua b/src/Root/init.lua new file mode 100644 index 0000000..1590e0c --- /dev/null +++ b/src/Root/init.lua @@ -0,0 +1,29 @@ +--!strict + +local BooleanValidator = require(script.Parent.Boolean) +local EnumValidator = require(script.Parent.Enum) +local InstanceValidator = require(script.Parent.Instance) +local NanValidator = require(script.Parent.Nan) +local NilValidator = require(script.Parent.Nil) +local NumberValidator = require(script.Parent.Number) +local StringValidator = require(script.Parent.String) +local TableValidator = require(script.Parent.Table) +local TypeValidator = require(script.Parent.Type) + +local RootValidator = {} +RootValidator.__index = RootValidator + +RootValidator.IsNumber = NumberValidator.IsNumber +RootValidator.IsInteger = NumberValidator.IsInteger +RootValidator.IsNil = NilValidator.IsNil +RootValidator.IsString = StringValidator.IsString +RootValidator.IsBoolean = BooleanValidator.IsBoolean +RootValidator.IsNan = NanValidator.IsNan +RootValidator.IsAnInstance = InstanceValidator.IsAnInstance +RootValidator.IsAnInstanceStrict = InstanceValidator.IsAnInstanceStrict +RootValidator.IsTable = TableValidator.IsTable +RootValidator.IsTableStrict = TableValidator.IsTableStrict +RootValidator.IsTypeOf = TypeValidator.IsTypeOf +RootValidator.IsEnumItem = EnumValidator.IsEnumItem + +return RootValidator diff --git a/src/String/StringChecks.lua b/src/String/StringChecks.lua new file mode 100644 index 0000000..c69860d --- /dev/null +++ b/src/String/StringChecks.lua @@ -0,0 +1,31 @@ +--!strict + +local StringChecks = {} + +function StringChecks.IsString(value: any): boolean + return type(value) == "string" +end + +function StringChecks.IsUTF8(value: string): boolean + return if utf8.len(value) then true else false +end + +function StringChecks.MaxLen(value: any, len: number): boolean + return string.len(value) <= len +end + +function StringChecks.MinLen(value: any, len: number): boolean + return string.len(value) >= len +end + +function StringChecks.MaxUTF8Len(value: any, len: number): boolean + local result = utf8.len(value) + return if result then result <= len else false +end + +function StringChecks.MinUTF8Len(value: any, len: number): boolean + local result = utf8.len(value) + return if result then result >= len else false +end + +return StringChecks diff --git a/src/String/init.lua b/src/String/init.lua new file mode 100644 index 0000000..86f16b1 --- /dev/null +++ b/src/String/init.lua @@ -0,0 +1,65 @@ +--!strict + +local StringChecks = require(script.StringChecks) +local CoreValidator = require(script.Parent.Core) +local Helpers = require(script.Parent.Helpers) +local ValidatorTypes = require(script.Parent.ValidatorTypes) + +local StringValidator = {} +setmetatable(StringValidator, CoreValidator) +StringValidator.__index = StringValidator + +function StringValidator.new(): ValidatorTypes.PublicStringValidator + local self = setmetatable(CoreValidator.new() :: any, StringValidator) + Helpers.AddCheck(self, StringChecks.IsString) + return self +end + +function StringValidator.IsString( + self: ValidatorTypes.PrivateValidator +): ValidatorTypes.PrivateStringValidator + local new = setmetatable(self :: any, StringValidator) + Helpers.AddCheck(new, StringChecks.IsString) + return new +end + +function StringValidator.IsUTF8( + self: ValidatorTypes.PrivateStringValidator +): ValidatorTypes.PrivateStringValidator + Helpers.AddCheck(self, StringChecks.IsUTF8) + return self +end + +function StringValidator.MaxLen( + self: ValidatorTypes.PrivateStringValidator, + len: number +): ValidatorTypes.PrivateStringValidator + Helpers.AddCheck(self, StringChecks.MaxLen, len) + return self +end + +function StringValidator.MinLen( + self: ValidatorTypes.PrivateStringValidator, + len: number +): ValidatorTypes.PrivateStringValidator + Helpers.AddCheck(self, StringChecks.MinLen, len) + return self +end + +function StringValidator.MaxUTF8Len( + self: ValidatorTypes.PrivateStringValidator, + len: number +): ValidatorTypes.PrivateStringValidator + Helpers.AddCheck(self, StringChecks.MaxUTF8Len, len) + return self +end + +function StringValidator.MinUTF8Len( + self: ValidatorTypes.PrivateStringValidator, + len: number +): ValidatorTypes.PrivateStringValidator + Helpers.AddCheck(self, StringChecks.MinUTF8Len, len) + return self +end + +return StringValidator diff --git a/src/Table/TableChecks.lua b/src/Table/TableChecks.lua new file mode 100644 index 0000000..7b3de71 --- /dev/null +++ b/src/Table/TableChecks.lua @@ -0,0 +1,43 @@ +--!strict + +local ValidatorTypes = require(script.Parent.Parent.ValidatorTypes) + +local TableChecks = {} + +function TableChecks.IsTable(value: any, schema: { [any]: ValidatorTypes.Checker }?) + if typeof(value) ~= "table" then + return false + end + + if not schema then + return true + end + + for k, checker in pairs(schema) do + if not checker:Check(value[k]) then + return false + end + end + + return true +end + +function TableChecks.IsTableStrict(value: any, schema: { [any]: ValidatorTypes.Checker }?) + if not TableChecks.IsTable(value, schema) then + return false + end + + if not schema then + return true + end + + for k, _ in pairs(value) do + if not schema[k] then + return false + end + end + + return true +end + +return TableChecks diff --git a/src/Table/init.lua b/src/Table/init.lua new file mode 100644 index 0000000..4f63378 --- /dev/null +++ b/src/Table/init.lua @@ -0,0 +1,46 @@ +--!strict + +local TableChecks = require(script.TableChecks) +local CoreValidator = require(script.Parent.Core) +local Helpers = require(script.Parent.Helpers) +local ValidatorTypes = require(script.Parent.ValidatorTypes) + +local TableValidator = {} +setmetatable(TableValidator, CoreValidator) +TableValidator.__index = TableValidator + +function TableValidator.newIsTable( + schema: { [any]: ValidatorTypes.Checker }? +): ValidatorTypes.PublicTableValidator + local self = setmetatable(CoreValidator.new() :: any, TableValidator) + Helpers.AddCheck(self, TableChecks.IsTable, schema) + return self +end + +function TableValidator.newIsTableStrict( + schema: { [any]: ValidatorTypes.Checker }? +): ValidatorTypes.PublicTableValidator + local self = setmetatable(CoreValidator.new() :: any, TableValidator) + Helpers.AddCheck(self, TableChecks.IsTableStrict, schema) + return self +end + +function TableValidator.IsTable( + self: ValidatorTypes.PrivateValidator, + schema: { [any]: ValidatorTypes.Checker }? +): ValidatorTypes.PrivateTableValidator + local new = setmetatable(self :: any, TableValidator) + Helpers.AddCheck(new, TableChecks.IsTable, schema) + return new +end + +function TableValidator.IsTableStrict( + self: ValidatorTypes.PrivateValidator, + schema: { [any]: ValidatorTypes.Checker }? +): ValidatorTypes.PrivateTableValidator + local new = setmetatable(self :: any, TableValidator) + Helpers.AddCheck(new, TableChecks.IsTableStrict, schema) + return new +end + +return TableValidator diff --git a/src/Type/TypeChecks.lua b/src/Type/TypeChecks.lua new file mode 100644 index 0000000..0fcc560 --- /dev/null +++ b/src/Type/TypeChecks.lua @@ -0,0 +1,9 @@ +--!strict + +local TypeChecks = {} + +function TypeChecks.IsTypeOf(value: any, type: string): boolean + return typeof(value) == type +end + +return TypeChecks diff --git a/src/Type/init.lua b/src/Type/init.lua new file mode 100644 index 0000000..25b75ea --- /dev/null +++ b/src/Type/init.lua @@ -0,0 +1,27 @@ +--!strict + +local TypeChecks = require(script.TypeChecks) +local CoreValidator = require(script.Parent.Core) +local Helpers = require(script.Parent.Helpers) +local ValidatorTypes = require(script.Parent.ValidatorTypes) + +local TypeValidator = {} +setmetatable(TypeValidator, CoreValidator) +TypeValidator.__index = TypeValidator + +function TypeValidator.new(type: string): ValidatorTypes.PublicTypeValidator + local self = setmetatable(CoreValidator.new() :: any, TypeValidator) + Helpers.AddCheck(self, TypeChecks.IsTypeOf, type) + return self +end + +function TypeValidator.IsTypeOf( + self: ValidatorTypes.PrivateValidator, + type: string +): ValidatorTypes.PublicTypeValidator + local new = setmetatable(self :: any, TypeValidator) + Helpers.AddCheck(new, TypeChecks.IsTypeOf, type) + return new +end + +return TypeValidator diff --git a/src/Validator.spec.luau b/src/Validator.spec.luau new file mode 100644 index 0000000..153bf3c --- /dev/null +++ b/src/Validator.spec.luau @@ -0,0 +1,463 @@ +--!strict + +local Validator = require(script.Parent) + +return function() + describe("00. Core", function() + it("00. Creates, freezes and checks", function() + local validator = Validator.IsNumber():Freeze() + + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(nil)).to.be.equal(false) + end) + + it("01. Combines complex expressions", function() + local validator = + Validator.IsInteger():IsGreater(5):Not():IsGreater(14):Or():IsNil():Freeze() + + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(5)).to.be.equal(false) + expect(validator:Check(10)).to.be.equal(true) + expect(validator:Check(15)).to.be.equal(false) + expect(validator:Check(nil)).to.be.equal(true) + expect(validator:Check("10")).to.be.equal(false) + end) + + it("02. Asserts", function() + local validator = + Validator.IsInteger():IsGreater(5):Not():IsGreater(14):Or():IsNil():Freeze() + + local success, value = pcall(function() + return validator:Assert(0, "Error") + end) + + expect(success).to.be.equal(false) + expect(value).never.to.be.equal(0) + + success, value = pcall(function() + return validator:Assert(10, "Error") + end) + + expect(success).to.be.equal(true) + expect(value).to.be.equal(10) + end) + + it("03. Methods: IsEqual", function() + local validator = Validator.IsString():Not():IsEqual("aaa"):Freeze() + + expect(validator:Check(1)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(true) + expect(validator:Check("aaa")).to.be.equal(false) + expect(validator:Check(nil)).to.be.equal(false) + end) + + it("04. Methods: IsInTable", function() + local values = { + "aaa", + "bbb", + "ccc", + } + + local validator = Validator.IsString():IsInTable(values):Freeze() + + expect(validator:Check(1)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check("aaa")).to.be.equal(true) + expect(validator:Check(nil)).to.be.equal(false) + + validator = Validator.IsString():Not():IsInTable(values):Freeze() + + expect(validator:Check(1)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(true) + expect(validator:Check("aaa")).to.be.equal(false) + expect(validator:Check(nil)).to.be.equal(false) + end) + + it("05. Methods: IsKeyOf", function() + local values = { + "aaa", + "bbb", + "ccc", + } + + local validator = Validator.IsInteger():IsKeyOf(values):Freeze() + + expect(validator:Check(1)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(4)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check("aaa")).to.be.equal(false) + expect(validator:Check(nil)).to.be.equal(false) + end) + end) + + describe("01. Strings", function() + it("00. Creates", function() + local validator = Validator.IsString():Freeze() + + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check("0")).to.be.equal(true) + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(string.pack("BBBB", 0xFF, 0xFE, 0x00, 0x80))).to.be.equal(true) + end) + + it("01. Combines", function() + local validator = Validator.IsNumber():Or():IsString():Freeze() + + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check("0")).to.be.equal(true) + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(string.pack("BBBB", 0xFF, 0xFE, 0x00, 0x80))).to.be.equal(true) + end) + + it("02. Methods: IsUTF8", function() + local validator = Validator.IsString():IsUTF8():Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check("abcde")).to.be.equal(true) + expect(validator:Check(string.pack("BBBB", 0xFF, 0xFE, 0x00, 0x80))).to.be.equal(false) + end) + + it("03. Methods: MinLen and MaxLen", function() + local validator = Validator.IsString():MinLen(5):MaxLen(10):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check("abcd")).to.be.equal(false) + expect(validator:Check("abcde")).to.be.equal(true) + expect(validator:Check("abcdeabcde")).to.be.equal(true) + expect(validator:Check("abcdeabcdef")).to.be.equal(false) + expect(validator:Check("абвгдеёжзи")).to.be.equal(false) + end) + + it("04. Methods: MinUTF8Len and MaxUTF8Len", function() + local validator = Validator.IsString():MinUTF8Len(5):MaxUTF8Len(10):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check("abcd")).to.be.equal(false) + expect(validator:Check("abcde")).to.be.equal(true) + expect(validator:Check("abcdeabcde")).to.be.equal(true) + expect(validator:Check("abcdeabcdef")).to.be.equal(false) + expect(validator:Check("абвгдеёжзи")).to.be.equal(true) + end) + end) + + describe("02. Booleans", function() + it("00. Creates", function() + local validator = Validator.IsBoolean():Freeze() + + expect(validator:Check(true)).to.be.equal(true) + expect(validator:Check(false)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(nil)).to.be.equal(false) + end) + + it("01. Combines", function() + local validator = Validator.IsNumber():Or():IsBoolean():Freeze() + + expect(validator:Check(true)).to.be.equal(true) + expect(validator:Check(false)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(nil)).to.be.equal(false) + end) + end) + + describe("03. Numbers and NaNs", function() + local nan = 0 / 0 + it("00. Creates and combines IsNumber", function() + local validator = Validator.IsNumber():Freeze() + + expect(validator:Check(nan)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(0.5)).to.be.equal(true) + expect(validator:Check("0")).to.be.equal(false) + + validator = Validator.IsString():Or():IsNumber():Freeze() + + expect(validator:Check(nan)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(0.5)).to.be.equal(true) + expect(validator:Check("0")).to.be.equal(true) + end) + + it("01. Creates and combines IsInteger", function() + local validator = Validator.IsInteger():Freeze() + + expect(validator:Check(nan)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(0.5)).to.be.equal(false) + expect(validator:Check("0")).to.be.equal(false) + + validator = Validator.IsString():Or():IsInteger():Freeze() + + expect(validator:Check(nan)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + expect(validator:Check(0.5)).to.be.equal(false) + expect(validator:Check("0")).to.be.equal(true) + end) + + it("02. Creates and combines IsNan", function() + local validator = Validator.IsNan():Freeze() + + expect(validator:Check(nan)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(0.5)).to.be.equal(false) + expect(validator:Check("0")).to.be.equal(false) + + validator = Validator.IsString():Or():IsNan():Freeze() + + expect(validator:Check(nan)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(0.5)).to.be.equal(false) + expect(validator:Check("0")).to.be.equal(true) + end) + + it("03. Methods: IsGreater", function() + local validator = Validator.IsNumber():IsGreater(5):Freeze() + + expect(validator:Check(nan)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(0.5)).to.be.equal(false) + expect(validator:Check(5)).to.be.equal(false) + expect(validator:Check(5.00001)).to.be.equal(true) + expect(validator:Check(6)).to.be.equal(true) + expect(validator:Check(-math.huge)).to.be.equal(false) + expect(validator:Check(math.huge)).to.be.equal(true) + expect(validator:Check("0")).to.be.equal(false) + end) + end) + + describe("04. Instances", function() + local part = Instance.new("Part") + part.Transparency = 0.6 + part:SetAttribute("test", "Hello") + + local model = Instance.new("Model") + model:SetAttribute("test", 5) + model:AddTag("Test") + + local decal = Instance.new("Decal") + decal.Transparency = 0.4 + decal:SetAttribute("test", false) + + it("00. Creates and combines IsAnInstance", function() + local validator = Validator.IsAnInstance("PVInstance"):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(true) + expect(validator:Check(model)).to.be.equal(true) + expect(validator:Check(decal)).to.be.equal(false) + end) + + it("01. Creates and combines IsAnInstanceStrict", function() + local validator = Validator.IsAnInstanceStrict("PVInstance"):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(false) + expect(validator:Check(model)).to.be.equal(false) + expect(validator:Check(decal)).to.be.equal(false) + + validator = Validator.IsAnInstanceStrict("Part"):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(true) + expect(validator:Check(model)).to.be.equal(false) + expect(validator:Check(decal)).to.be.equal(false) + end) + + it("02. Methods: IsProperty", function() + local validator = Validator.IsAnInstance("Object") + :IsProperty("Transparency", Validator.IsNumber():IsGreater(0.5)) + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(true) + expect(validator:Check(model)).to.be.equal(false) + expect(validator:Check(decal)).to.be.equal(false) + + validator = Validator.IsAnInstance("Object") + :IsProperty("Transparency", Validator.IsNumber():IsGreater(0)) + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(true) + expect(validator:Check(model)).to.be.equal(false) + expect(validator:Check(decal)).to.be.equal(true) + end) + + it("03. Methods: IsAttribute", function() + local validator = Validator.IsAnInstance("Object") + :IsAttribute("test", Validator.IsNumber():Or():IsBoolean()) + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(false) + expect(validator:Check(model)).to.be.equal(true) + expect(validator:Check(decal)).to.be.equal(true) + + validator = Validator.IsAnInstance("Object"):IsAttribute("test", Validator.IsNumber()) + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(false) + expect(validator:Check(model)).to.be.equal(true) + expect(validator:Check(decal)).to.be.equal(false) + end) + + it("04. Methods: HasTag", function() + local validator = Validator.IsAnInstance("Object"):HasTag("Test") + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + expect(validator:Check(part)).to.be.equal(false) + expect(validator:Check(model)).to.be.equal(true) + expect(validator:Check(decal)).to.be.equal(false) + end) + end) + + describe("05. Tables", function() + local table1 = { + a = 10, + } + local table2 = { + a = 20, + } + local table3 = { + a = 10, + b = 20, + } + + it("00. Creates and combines", function() + local validator = Validator.IsTable():Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(true) + expect(validator:Check(table1)).to.be.equal(true) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(false) + + validator = Validator.IsNumber():Or():IsTable():Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(true) + expect(validator:Check(table1)).to.be.equal(true) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(true) + expect(validator:Check(0)).to.be.equal(true) + end) + + it("01. Creates and combines with schema", function() + local validator = Validator.IsTable({ + a = Validator.IsInteger():IsGreater(15), + }):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(false) + expect(validator:Check(table1)).to.be.equal(false) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + + validator = Validator.IsNumber() + :Or() + :IsTable({ + a = Validator.IsInteger():IsGreater(15), + }) + :Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(false) + expect(validator:Check(table1)).to.be.equal(false) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + end) + + it("02. Creates and combines with schema strict", function() + local validator = Validator.IsTableStrict({ + a = Validator.IsInteger(), + }):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(false) + expect(validator:Check(table1)).to.be.equal(true) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + + validator = Validator.IsNumber() + :Or() + :IsTableStrict({ + a = Validator.IsInteger(), + }) + :Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check({})).to.be.equal(false) + expect(validator:Check(table1)).to.be.equal(true) + expect(validator:Check(table2)).to.be.equal(true) + expect(validator:Check(table3)).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + end) + end) + + describe("06. Types", function() + it("00. Creates and combines", function() + local validator = Validator.IsTypeOf("Vector3"):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(Vector3.new(10, 20, 30))).to.be.equal(true) + expect(validator:Check(Vector3.zero)).to.be.equal(true) + expect(validator:Check(UDim2.new())).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(false) + + validator = Validator.IsNumber():Or():IsTypeOf("Vector3"):Freeze() + + expect(validator:Check(nil)).to.be.equal(false) + expect(validator:Check(Vector3.new(10, 20, 30))).to.be.equal(true) + expect(validator:Check(Vector3.zero)).to.be.equal(true) + expect(validator:Check(UDim2.new())).to.be.equal(false) + expect(validator:Check(0)).to.be.equal(true) + end) + end) + + describe("07. Enums", function() + it("00. Creates and combines", function() + local validator = Validator.IsEnumItem():Freeze() + + expect(validator:Check(1)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check(Enum.KeyCode.A)).to.be.equal(true) + expect(validator:Check(Enum.PartType.Ball)).to.be.equal(true) + + validator = Validator.IsNumber():Or():IsEnumItem():Freeze() + + expect(validator:Check(1)).to.be.equal(true) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check(Enum.KeyCode.A)).to.be.equal(true) + expect(validator:Check(Enum.PartType.Ball)).to.be.equal(true) + end) + + it("01. Creates and combines with given Enum", function() + local validator = Validator.IsEnumItem(Enum.KeyCode):Freeze() + + expect(validator:Check(1)).to.be.equal(false) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check(Enum.KeyCode.A)).to.be.equal(true) + expect(validator:Check(Enum.PartType.Ball)).to.be.equal(false) + + validator = Validator.IsNumber():Or():IsEnumItem(Enum.KeyCode):Freeze() + + expect(validator:Check(1)).to.be.equal(true) + expect(validator:Check("1")).to.be.equal(false) + expect(validator:Check(Enum.KeyCode.A)).to.be.equal(true) + expect(validator:Check(Enum.PartType.Ball)).to.be.equal(false) + end) + end) +end diff --git a/src/ValidatorTypes.lua b/src/ValidatorTypes.lua new file mode 100644 index 0000000..8472367 --- /dev/null +++ b/src/ValidatorTypes.lua @@ -0,0 +1,178 @@ +--!strict + +--#region Core + +export type Check = false | { + _func: (any, ...any) -> boolean, + _params: typeof(table.pack(...)), +} + +export type Checker = typeof(setmetatable({}, {})) & { + Check: (self: Checker, data: any) -> boolean, + Assert: (self: Checker, data: any, message: string?) -> any, +} + +export type PublicValidator = Checker & { + Or: (self: PublicValidator) -> RootMethods, + Freeze: (self: PublicValidator) -> Checker, +} +export type PrivateValidator = PublicValidator & { + _checksGroups: { { Check } }, +} + +--#endregion + +--#region Root + +export type RootMethods = { + IsNumber: (self: RootMethods) -> PublicNumberValidator, + IsInteger: (self: RootMethods) -> PublicNumberValidator, + IsNil: (self: RootMethods) -> PublicNilValidator, + IsString: (self: RootMethods) -> PublicStringValidator, + IsBoolean: (self: RootMethods) -> PublicBooleanValidator, + IsNan: (self: RootMethods) -> PublicNanValidator, + IsAnInstance: (self: RootMethods, instanceClass: string) -> PublicInstanceValidator, + IsAnInstanceStrict: (self: RootMethods, instanceClass: string) -> PublicInstanceValidator, + IsTable: (self: RootMethods, schema: { [any]: Checker }?) -> PublicTableValidator, + IsTableStrict: (self: RootMethods, schema: { [any]: Checker }?) -> PublicTableValidator, + IsTypeOf: (self: RootMethods, type: string) -> PublicTypeValidator, + IsEnumItem: (self: RootMethods, enum: Enum?) -> PublicEnumValidator, +} +export type PublicRootValidator = PublicValidator & RootMethods +export type PrivateRootValidator = PublicRootValidator & PrivateValidator + +--#endregion + +--#region Number + +export type NumberValidatorMethods = { + IsGreater: (self: NumberValidatorMethods, than: number) -> PublicNumberValidator, + + IsInTable: (self: NumberValidatorMethods, table: { [any]: any }) -> PublicNumberValidator, + IsKeyOf: (self: NumberValidatorMethods, table: { [any]: any }) -> PublicNumberValidator, + IsEqual: (self: NumberValidatorMethods, otherValue: any) -> PublicNumberValidator, +} +export type PublicNumberValidator = PublicValidator & NumberValidatorMethods & { + Not: (self: PublicValidator) -> NumberValidatorMethods, +} +export type PrivateNumberValidator = PublicNumberValidator & PrivateValidator + +--#endregion + +--#region Nil + +export type PublicNilValidator = PublicValidator & {} +export type PrivateNilValidator = PublicNilValidator & PrivateValidator + +--#endregion + +--#region String + +export type StringValidatorMethods = { + IsUTF8: (self: StringValidatorMethods) -> PublicStringValidator, + MaxLen: (self: StringValidatorMethods, len: number) -> PublicStringValidator, + MinLen: (self: StringValidatorMethods, len: number) -> PublicStringValidator, + MaxUTF8Len: (self: StringValidatorMethods, len: number) -> PublicStringValidator, + MinUTF8Len: (self: StringValidatorMethods, len: number) -> PublicStringValidator, + + IsInTable: (self: StringValidatorMethods, table: { [any]: any }) -> PublicStringValidator, + IsKeyOf: (self: StringValidatorMethods, table: { [any]: any }) -> PublicStringValidator, + IsEqual: (self: StringValidatorMethods, otherValue: any) -> PublicStringValidator, +} +export type PublicStringValidator = PublicValidator & StringValidatorMethods & { + Not: (self: PublicValidator) -> StringValidatorMethods, +} +export type PrivateStringValidator = PublicStringValidator & PrivateValidator + +--#endregion + +--#region Boolean + +export type BooleanValidatorMethods = { + IsInTable: (self: BooleanValidatorMethods, table: { [any]: any }) -> PublicBooleanValidator, + IsKeyOf: (self: BooleanValidatorMethods, table: { [any]: any }) -> PublicBooleanValidator, + IsEqual: (self: BooleanValidatorMethods, otherValue: any) -> PublicBooleanValidator, +} +export type PublicBooleanValidator = PublicValidator & { + Not: (self: PublicValidator) -> BooleanValidatorMethods, +} +export type PrivateBooleanValidator = PublicBooleanValidator & PrivateValidator + +--#endregion + +--#region Nan + +export type PublicNanValidator = PublicValidator & {} +export type PrivateNanValidator = PublicNanValidator & PrivateValidator + +--#endregion + +--#region Instance + +export type InstanceValidatorMethods = { + IsProperty: ( + self: InstanceValidatorMethods, + property: string, + checker: Checker + ) -> PublicInstanceValidator, + IsAttribute: ( + self: InstanceValidatorMethods, + attribute: string, + checker: Checker + ) -> PublicInstanceValidator, + HasTag: (self: InstanceValidatorMethods, tag: string) -> PublicInstanceValidator, + + IsInTable: (self: InstanceValidatorMethods, table: { [any]: any }) -> PublicInstanceValidator, + IsKeyOf: (self: InstanceValidatorMethods, table: { [any]: any }) -> PublicInstanceValidator, + IsEqual: (self: InstanceValidatorMethods, otherValue: any) -> PublicInstanceValidator, +} +export type PublicInstanceValidator = PublicValidator & InstanceValidatorMethods & { + Not: (self: PublicValidator) -> InstanceValidatorMethods, +} +export type PrivateInstanceValidator = PublicInstanceValidator & PrivateValidator + +--#endregion + +--#region Table + +export type TableValidatorMethods = { + IsInTable: (self: TableValidatorMethods, table: { [any]: any }) -> PublicTableValidator, + IsKeyOf: (self: TableValidatorMethods, table: { [any]: any }) -> PublicTableValidator, + IsEqual: (self: TableValidatorMethods, otherValue: any) -> PublicTableValidator, +} +export type PublicTableValidator = PublicValidator & TableValidatorMethods & { + Not: (self: PublicValidator) -> TableValidatorMethods, +} +export type PrivateTableValidator = PublicTableValidator & PrivateValidator + +--#endregion + +--#region Type + +export type TypeValidatorMethods = { + IsInTable: (self: TypeValidatorMethods, table: { [any]: any }) -> PublicTypeValidator, + IsKeyOf: (self: TypeValidatorMethods, table: { [any]: any }) -> PublicTypeValidator, + IsEqual: (self: TypeValidatorMethods, otherValue: any) -> PublicTypeValidator, +} +export type PublicTypeValidator = PublicValidator & TypeValidatorMethods & { + Not: (self: PublicValidator) -> TypeValidatorMethods, +} +export type PrivateTypeValidator = PublicTypeValidator & PrivateValidator + +--#endregion + +--#region Enum + +export type EnumValidatorMethods = { + IsInTable: (self: EnumValidatorMethods, table: { [any]: any }) -> PublicEnumValidator, + IsKeyOf: (self: EnumValidatorMethods, table: { [any]: any }) -> PublicEnumValidator, + IsEqual: (self: EnumValidatorMethods, otherValue: any) -> PublicEnumValidator, +} +export type PublicEnumValidator = PublicValidator & EnumValidatorMethods & { + Not: (self: PublicValidator) -> EnumValidatorMethods, +} +export type PrivateEnumValidator = PublicEnumValidator & PrivateValidator + +--#endregion + +return {} diff --git a/src/init.lua b/src/init.lua index 6653a2a..c82e036 100644 --- a/src/init.lua +++ b/src/init.lua @@ -1,5 +1,32 @@ --!strict +local BooleanValidator = require(script.Boolean) +local CoreValidator = require(script.Core) +local EnumValidator = require(script.Enum) +local InstanceValidator = require(script.Instance) +local NanValidator = require(script.Nan) +local RootValidator = require(script.Root) +local NilValidator = require(script.Nil) +local NumberValidator = require(script.Number) +local StringValidator = require(script.String) +local TableValidator = require(script.Table) +local TypeValidator = require(script.Type) + local Validator = {} +CoreValidator._Setup(RootValidator :: any) + +Validator.IsNumber = NumberValidator.newIsNumber +Validator.IsInteger = NumberValidator.newIsInteger +Validator.IsNil = NilValidator.new +Validator.IsString = StringValidator.new +Validator.IsBoolean = BooleanValidator.new +Validator.IsNan = NanValidator.new +Validator.IsAnInstance = InstanceValidator.newIsAnInstance +Validator.IsAnInstanceStrict = InstanceValidator.newIsAnInstanceStrict +Validator.IsTable = TableValidator.newIsTable +Validator.IsTableStrict = TableValidator.newIsTableStrict +Validator.IsTypeOf = TypeValidator.new +Validator.IsEnumItem = EnumValidator.new + return Validator