Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented optional data validation #118

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
21 changes: 8 additions & 13 deletions DataStore2/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,26 +182,21 @@ end
function DataStore:SetValidator(validator)
assert(
type(validator) == "function",
"function expected, got " .. type(validator)
"function expected, got " .. typeof(validator)
)
self.validator = validator
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
end

local function AssertValidatorWithDefaultError(validator, input, defaultError)
local success, err = pcall(validator, input)

-- if success but result is false
if success and err ~= true then
error(defaultError)
-- if not success and there is an error message
elseif not success and err ~= nil then
error(err)
end
local function assertValidatorWithDefaultError(validator, input, defaultError)
local isValid = validator(input)
if not isValid then
error(defaultError)
end
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
end

function DataStore:Set(value, _dontCallOnUpdate)
if self.validator then
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
AssertValidatorWithDefaultError(
assertValidatorWithDefaultError(
self.validator,
value,
"Invalid data"
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -214,7 +209,7 @@ end
function DataStore:Update(updateFunc)
local updateFuncReturn = updateFunc(self.value)
if self.validator then
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
AssertValidatorWithDefaultError(
assertValidatorWithDefaultError(
self.validator,
updateFuncReturn,
"Invalid data"
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
14 changes: 14 additions & 0 deletions Tests/tests/DataStore2.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ return function()
expect(DataStore2(nonNilKey, fakePlayer):Get("badDefault")).to.equal("abc")
end)

it("should validate the data", function()
local dataStore = DataStore2(UUID(), fakePlayer)
local function testValidator(dataToValidate)
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
if dataToValidate == "yepp" then
return true
end
return false
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
end
dataStore:SetValidator(testValidator)
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
expect(dataStore.validator).to.be.a("function")
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
expect(dataStore:Set("nope")).to.throw()
expect(dataStore:Set("yepp")).to.equal(true)
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
end)

it("should set", function()
local dataStore = DataStore2(UUID(), fakePlayer)
dataStore:Set(1)
Expand Down