From 2cb36f0c8016165e8dd75fe3afdc32ff6278c4ed Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 19:03:40 +0100 Subject: [PATCH 01/19] Added optional validation to DataStore:Set and DataStore:Update --- DataStore2/init.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index b5689c9..220461b 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -179,13 +179,34 @@ function DataStore:GetTableAsync(default, ...) end) end +function DataStore:SetValidator(validator) + assert( + type(validator) == "function", + "function expected, got "..type(validator) + ) + self.validator = validator +end + function DataStore:Set(value, _dontCallOnUpdate) + if self.validator then + assert( + self.validator(value), + "Invalid data" + ) + end self.value = clone(value) self:_Update(_dontCallOnUpdate) end function DataStore:Update(updateFunc) - self.value = updateFunc(self.value) + local updateFuncReturn = updateFunc(self.value) + if self.validator then + assert( + self.validator(updateFuncReturn), + "Invalid data" + ) + end + self.value = updateFuncReturn self:_Update() end From 21fd0e50a7483b4a339056d66f46f55f48ff57c8 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 21:54:49 +0100 Subject: [PATCH 02/19] Change 2 --- DataStore2/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 220461b..bcfe337 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -182,7 +182,7 @@ end function DataStore:SetValidator(validator) assert( type(validator) == "function", - "function expected, got "..type(validator) + "function expected, got " .. type(validator) ) self.validator = validator end From c6fecb1b331246ff37c5888632cc1038910214a8 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 22:55:33 +0100 Subject: [PATCH 03/19] Added request 1 and 3 --- DataStore2/init.lua | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index bcfe337..72c2e6c 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -187,10 +187,23 @@ function DataStore:SetValidator(validator) self.validator = validator 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 +end + function DataStore:Set(value, _dontCallOnUpdate) if self.validator then - assert( - self.validator(value), + AssertValidatorWithDefaultError( + self.validator, + value, "Invalid data" ) end @@ -201,8 +214,9 @@ end function DataStore:Update(updateFunc) local updateFuncReturn = updateFunc(self.value) if self.validator then - assert( - self.validator(updateFuncReturn), + AssertValidatorWithDefaultError( + self.validator, + updateFuncReturn, "Invalid data" ) end From e25255a05fcc2a68374575e6856faf2510962b49 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 23:10:53 +0100 Subject: [PATCH 04/19] Added unit test for validation --- Tests/tests/DataStore2.spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index e7c9a60..589ccf3 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -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) + if dataToValidate == "yepp" then + return true + end + return false + end + dataStore:SetValidator(testValidator) + expect(dataStore.validator).to.be.a("function") + expect(dataStore:Set("nope")).to.throw() + expect(dataStore:Set("yepp")).to.equal(true) + end) + it("should set", function() local dataStore = DataStore2(UUID(), fakePlayer) dataStore:Set(1) From c12c18f1e74d695a25546886002e89aa97340f08 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 23:23:19 +0100 Subject: [PATCH 05/19] assertValidatorWithDefaultError no longer uses pcall --- DataStore2/init.lua | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 72c2e6c..d17a251 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -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 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 end function DataStore:Set(value, _dontCallOnUpdate) if self.validator then - AssertValidatorWithDefaultError( + assertValidatorWithDefaultError( self.validator, value, "Invalid data" @@ -214,7 +209,7 @@ end function DataStore:Update(updateFunc) local updateFuncReturn = updateFunc(self.value) if self.validator then - AssertValidatorWithDefaultError( + assertValidatorWithDefaultError( self.validator, updateFuncReturn, "Invalid data" From d808ebb76dd68fa7260387732a5e9c4ed302d4ab Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 23:28:01 +0100 Subject: [PATCH 06/19] Fixed unit test --- Tests/tests/DataStore2.spec.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index 589ccf3..bd854e8 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -84,12 +84,15 @@ return function() local function testValidator(dataToValidate) if dataToValidate == "yepp" then return true + elseif dataToValidate == "definitelyNot" then + error("A validation error message") end return false end dataStore:SetValidator(testValidator) expect(dataStore.validator).to.be.a("function") - expect(dataStore:Set("nope")).to.throw() + expect(dataStore:Set("nope")).to.equal(false) + expect(dataStore:Set("definitelyNot")).to.throw("A validation error message") expect(dataStore:Set("yepp")).to.equal(true) end) From 81a1d81c7e7d4c8d7492c3cd0452a2cd7a57a009 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 23:32:22 +0100 Subject: [PATCH 07/19] Edited error messages --- DataStore2/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index d17a251..a65ea79 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -199,7 +199,7 @@ function DataStore:Set(value, _dontCallOnUpdate) assertValidatorWithDefaultError( self.validator, value, - "Invalid data" + "Attempted to set datastore to an invalid value" ) end self.value = clone(value) @@ -212,7 +212,7 @@ function DataStore:Update(updateFunc) assertValidatorWithDefaultError( self.validator, updateFuncReturn, - "Invalid data" + "Attempted to update datastore to an invalid value" ) end self.value = updateFuncReturn From f2e065bcd86208adf8f6c2d8b7acb0397c825a23 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Fri, 1 Jan 2021 23:59:45 +0100 Subject: [PATCH 08/19] Validator can return a message --- DataStore2/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index a65ea79..80d0664 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -188,9 +188,9 @@ function DataStore:SetValidator(validator) end local function assertValidatorWithDefaultError(validator, input, defaultError) - local isValid = validator(input) + local isValid, message = validator(input) if not isValid then - error(defaultError) + error(message or defaultError) end end From 903f0d85b9e3a68b5b9b531639580fe82bb9e090 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Sat, 2 Jan 2021 00:00:38 +0100 Subject: [PATCH 09/19] Fixed validate set unit test and added validate update unit test --- Tests/tests/DataStore2.spec.lua | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index bd854e8..301f414 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -79,7 +79,7 @@ return function() expect(DataStore2(nonNilKey, fakePlayer):Get("badDefault")).to.equal("abc") end) - it("should validate the data", function() + it("should validate set", function() local dataStore = DataStore2(UUID(), fakePlayer) local function testValidator(dataToValidate) if dataToValidate == "yepp" then @@ -90,10 +90,31 @@ return function() return false end dataStore:SetValidator(testValidator) - expect(dataStore.validator).to.be.a("function") - expect(dataStore:Set("nope")).to.equal(false) + 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.equal(true) + expect(dataStore:Set("yepp")).to.be.ok() + end) + + it("should validate update", function() + local dataStore = DataStore2(UUID(), fakePlayer) + local function testValidator(dataToValidate) + if dataToValidate == "yepp" then + return true + elseif dataToValidate == "definitelyNot" then + error("A validation error message") + end + return false + end + 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() From 4d9cd6c5f2b6cdc7f916bccb1f5aacfd0c32f707 Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:09:14 +0100 Subject: [PATCH 10/19] Update Tests/tests/DataStore2.spec.lua Co-authored-by: boyned//Kampfkarren --- Tests/tests/DataStore2.spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index 301f414..d6ff026 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -79,7 +79,7 @@ return function() expect(DataStore2(nonNilKey, fakePlayer):Get("badDefault")).to.equal("abc") end) - it("should validate set", function() + it("should validate Set", function() local dataStore = DataStore2(UUID(), fakePlayer) local function testValidator(dataToValidate) if dataToValidate == "yepp" then @@ -600,4 +600,4 @@ return function() expect(called2).to.equal(1) end) end) -end \ No newline at end of file +end From d79c4bbc54e55665994ab889673b519b1d5c623e Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:09:27 +0100 Subject: [PATCH 11/19] Update Tests/tests/DataStore2.spec.lua Co-authored-by: boyned//Kampfkarren --- Tests/tests/DataStore2.spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index d6ff026..16b0d1b 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -95,7 +95,7 @@ return function() expect(dataStore:Set("yepp")).to.be.ok() end) - it("should validate update", function() + it("should validate Update", function() local dataStore = DataStore2(UUID(), fakePlayer) local function testValidator(dataToValidate) if dataToValidate == "yepp" then From 23103280e3213ee1075cede9cfa2db0a2c49a5f9 Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:10:08 +0100 Subject: [PATCH 12/19] Update DataStore2/init.lua Co-authored-by: boyned//Kampfkarren --- DataStore2/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 80d0664..8109c7e 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -199,7 +199,7 @@ function DataStore:Set(value, _dontCallOnUpdate) assertValidatorWithDefaultError( self.validator, value, - "Attempted to set datastore to an invalid value" + "Attempted to set data store to an invalid value during :Set" ) end self.value = clone(value) From 998df5c070f19c4c0467d7edfc50058ce2b8b779 Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:10:19 +0100 Subject: [PATCH 13/19] Update DataStore2/init.lua Co-authored-by: boyned//Kampfkarren --- DataStore2/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 8109c7e..89d25e2 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -195,7 +195,7 @@ local function assertValidatorWithDefaultError(validator, input, defaultError) end function DataStore:Set(value, _dontCallOnUpdate) - if self.validator then + if self.validator ~= nil then assertValidatorWithDefaultError( self.validator, value, From b336b08e0f9c6d279a99eb18cb199185a666fb5f Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:10:39 +0100 Subject: [PATCH 14/19] Update DataStore2/init.lua Co-authored-by: boyned//Kampfkarren --- DataStore2/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 89d25e2..0e8f0c4 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -208,7 +208,7 @@ end function DataStore:Update(updateFunc) local updateFuncReturn = updateFunc(self.value) - if self.validator then + if self.validator ~= nil then assertValidatorWithDefaultError( self.validator, updateFuncReturn, From d36a7fbbc07850f7cb6ef3aea0e37c1a818fd15a Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:10:51 +0100 Subject: [PATCH 15/19] Update DataStore2/init.lua Co-authored-by: boyned//Kampfkarren --- DataStore2/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 0e8f0c4..2ffb9cb 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -212,7 +212,7 @@ function DataStore:Update(updateFunc) assertValidatorWithDefaultError( self.validator, updateFuncReturn, - "Attempted to update datastore to an invalid value" + "Attempted to set data store to an invalid value during :Update" ) end self.value = updateFuncReturn From 26579c4224c7d2f282302746c7c80bcf1dff9139 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Sat, 2 Jan 2021 00:14:26 +0100 Subject: [PATCH 16/19] Whitespace --- Tests/tests/DataStore2.spec.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index 16b0d1b..d468ea5 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -106,12 +106,15 @@ return function() return false end 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() From 85b221b6ef95bd1da9ed9d3297edcc4058ac0a30 Mon Sep 17 00:00:00 2001 From: Masken8 Date: Sat, 2 Jan 2021 00:17:32 +0100 Subject: [PATCH 17/19] Validators in unit tests no longer error themselves --- Tests/tests/DataStore2.spec.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index d468ea5..54461e1 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -85,7 +85,7 @@ return function() if dataToValidate == "yepp" then return true elseif dataToValidate == "definitelyNot" then - error("A validation error message") + return false, "A validation error message" end return false end @@ -101,7 +101,7 @@ return function() if dataToValidate == "yepp" then return true elseif dataToValidate == "definitelyNot" then - error("A validation error message") + return false, "A validation error message" end return false end @@ -114,7 +114,7 @@ return function() expect(dataStore:Update(function() return "definitelyNot" end)).to.throw("A validation error message") - + expect(dataStore:Update(function() return "yepp" end)).to.be.ok() From 7d80f3d2cb0a17efafca9f42f9df2f723e40a7d6 Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 2 Jan 2021 00:25:20 +0100 Subject: [PATCH 18/19] Apply suggestions from code review Co-authored-by: boyned//Kampfkarren --- DataStore2/init.lua | 4 ++++ Tests/tests/DataStore2.spec.lua | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/DataStore2/init.lua b/DataStore2/init.lua index 2ffb9cb..11443a4 100644 --- a/DataStore2/init.lua +++ b/DataStore2/init.lua @@ -184,6 +184,7 @@ function DataStore:SetValidator(validator) type(validator) == "function", "function expected, got " .. typeof(validator) ) + self.validator = validator end @@ -202,12 +203,14 @@ function DataStore:Set(value, _dontCallOnUpdate) "Attempted to set data store to an invalid value during :Set" ) end + self.value = clone(value) self:_Update(_dontCallOnUpdate) end function DataStore:Update(updateFunc) local updateFuncReturn = updateFunc(self.value) + if self.validator ~= nil then assertValidatorWithDefaultError( self.validator, @@ -215,6 +218,7 @@ function DataStore:Update(updateFunc) "Attempted to set data store to an invalid value during :Update" ) end + self.value = updateFuncReturn self:_Update() end diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index 54461e1..e546bf8 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -81,14 +81,17 @@ return function() it("should validate Set", function() local dataStore = DataStore2(UUID(), fakePlayer) + local function testValidator(dataToValidate) if dataToValidate == "yepp" then return true elseif dataToValidate == "definitelyNot" then return false, "A validation error message" end + return false end + dataStore:SetValidator(testValidator) expect(dataStore:Set("nope")).to.throw("Attempted to set datastore to an invalid value") expect(dataStore:Set("definitelyNot")).to.throw("A validation error message") @@ -97,14 +100,17 @@ return function() it("should validate Update", function() local dataStore = DataStore2(UUID(), fakePlayer) + local function testValidator(dataToValidate) if dataToValidate == "yepp" then return true elseif dataToValidate == "definitelyNot" then return false, "A validation error message" end + return false end + dataStore:SetValidator(testValidator) expect(dataStore:Update(function() From 1f2b37eff1944b5190fc2c55505bc6400d9b74eb Mon Sep 17 00:00:00 2001 From: Masken8 Date: Sat, 2 Jan 2021 23:05:16 +0100 Subject: [PATCH 19/19] Unit test fixed for real this time I think --- Tests/tests/DataStore2.spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/tests/DataStore2.spec.lua b/Tests/tests/DataStore2.spec.lua index e546bf8..a87d9bb 100644 --- a/Tests/tests/DataStore2.spec.lua +++ b/Tests/tests/DataStore2.spec.lua @@ -93,7 +93,7 @@ return function() end dataStore:SetValidator(testValidator) - expect(dataStore:Set("nope")).to.throw("Attempted to set datastore to an invalid value") + expect(dataStore:Set("nope")).to.throw("Attempted to set data store to an invalid value during :Set") expect(dataStore:Set("definitelyNot")).to.throw("A validation error message") expect(dataStore:Set("yepp")).to.be.ok() end) @@ -115,7 +115,7 @@ return function() expect(dataStore:Update(function() return "nope" - end)).to.throw("Attempted to set datastore to an invalid value") + end)).to.throw("Attempted to set data store to an invalid value during :Update") expect(dataStore:Update(function() return "definitelyNot"