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 17 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
32 changes: 31 additions & 1 deletion DataStore2/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,43 @@ function DataStore:GetTableAsync(default, ...)
end)
end

function DataStore:SetValidator(validator)
assert(
type(validator) == "function",
"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 isValid, message = validator(input)
if not isValid then
error(message or defaultError)
end
end

function DataStore:Set(value, _dontCallOnUpdate)
if self.validator ~= nil then
assertValidatorWithDefaultError(
self.validator,
value,
"Attempted to set data store to an invalid value during :Set"
)
end
self.value = clone(value)
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
self:_Update(_dontCallOnUpdate)
end

function DataStore:Update(updateFunc)
self.value = updateFunc(self.value)
local updateFuncReturn = updateFunc(self.value)
if self.validator ~= nil then
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
assertValidatorWithDefaultError(
self.validator,
updateFuncReturn,
"Attempted to set data store to an invalid value during :Update"
)
end
self.value = updateFuncReturn
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
self:_Update()
end

Expand Down
43 changes: 42 additions & 1 deletion Tests/tests/DataStore2.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,47 @@ return function()
expect(DataStore2(nonNilKey, fakePlayer):Get("badDefault")).to.equal("abc")
end)

it("should validate Set", 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
elseif dataToValidate == "definitelyNot" then
return false, "A validation error message"
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:Set("nope")).to.throw("Attempted to set datastore to an invalid value")
expect(dataStore:Set("definitelyNot")).to.throw("A validation error message")
expect(dataStore:Set("yepp")).to.be.ok()
end)

it("should validate Update", 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
elseif dataToValidate == "definitelyNot" then
return false, "A validation error message"
end
return false
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
end
Masken8 marked this conversation as resolved.
Show resolved Hide resolved
dataStore:SetValidator(testValidator)

expect(dataStore:Update(function()
return "nope"
end)).to.throw("Attempted to set datastore to an invalid value")

expect(dataStore:Update(function()
return "definitelyNot"
end)).to.throw("A validation error message")

expect(dataStore:Update(function()
return "yepp"
end)).to.be.ok()
end)

it("should set", function()
local dataStore = DataStore2(UUID(), fakePlayer)
dataStore:Set(1)
Expand Down Expand Up @@ -562,4 +603,4 @@ return function()
expect(called2).to.equal(1)
end)
end)
end
end