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

Make validate optional #56

Merged
merged 2 commits into from
Jun 20, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
* Allow `defaultData` to be a function. The return value will be validated when a new document is created. ([#53])
* Migrations can now be marked as `backwardsCompatible`. This allows documents to be loaded on servers with an older version as long as they are compatible.
For more information, see the [docs](https://nezuo.github.io/lapis/docs/Migrations#backwards-compatibility). ([#54])
* The `validate` option for collections is now optional. ([#56])

[#50]: https://github.com/nezuo/lapis/pull/50
[#51]: https://github.com/nezuo/lapis/pull/51
[#52]: https://github.com/nezuo/lapis/pull/52
[#53]: https://github.com/nezuo/lapis/pull/53
[#54]: https://github.com/nezuo/lapis/pull/54
[#56]: https://github.com/nezuo/lapis/pull/56

### 0.3.0 - April 14, 2024
* **BREAKING CHANGE**: `Collection:load` no longer caches promises. Each call will now return a unique promise and attempt to load the document separately. This is to fix an edge case that can result in data loss. More information can be found in the pull request. ([#48])
Expand Down
26 changes: 15 additions & 11 deletions src/Collection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local Collection = {}
Collection.__index = Collection

function Collection.new(name, options, data, autoSave, config)
if typeof(options.defaultData) ~= "function" then
if typeof(options.defaultData) ~= "function" and options.validate ~= nil then
assert(options.validate(options.defaultData))
end

Expand Down Expand Up @@ -74,11 +74,13 @@ function Collection:load(key, defaultUserIds)
return "fail", `'defaultData' threw an error: {tailoredDefaultData}`
end

local validateOk, valid, message = pcall(self.options.validate, tailoredDefaultData)
if not validateOk then
return "fail", `'validate' threw an error: {valid}`
elseif not valid then
return "fail", `Invalid data: {message}`
if self.options.validate ~= nil then
local validateOk, valid, message = pcall(self.options.validate, tailoredDefaultData)
if not validateOk then
return "fail", `'validate' threw an error: {valid}`
elseif not valid then
return "fail", `Invalid data: {message}`
end
end

defaultData = copyDeep(tailoredDefaultData)
Expand Down Expand Up @@ -109,11 +111,13 @@ function Collection:load(key, defaultUserIds)
return "fail", migrated
end

local validateOk, valid, message = pcall(self.options.validate, migrated)
if not validateOk then
return "fail", `'validate' threw an error: {valid}`
elseif not valid then
return "fail", `Invalid data: {message}`
if self.options.validate ~= nil then
local validateOk, valid, message = pcall(self.options.validate, migrated)
if not validateOk then
return "fail", `'validate' threw an error: {valid}`
elseif not valid then
return "fail", `Invalid data: {message}`
end
end

local data = {
Expand Down
5 changes: 4 additions & 1 deletion src/Document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ end
]=]
function Document:write(data)
assert(not self.closed, "Cannot write to a closed document")
assert(self.validate(data))

if self.validate ~= nil then
assert(self.validate(data))
end

freezeDeep(data)

Expand Down
3 changes: 0 additions & 3 deletions src/Document.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ return function(x)

x.test("freezes document data", function(context)
local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = {},
})

Expand Down
4 changes: 2 additions & 2 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type PartialLapisConfig = {
export type CollectionOptions<T> = {
defaultData: T | (key: string) -> T,
migrations: { Migration }?,
validate: (any) -> (boolean, string?),
validate: ((any) -> (boolean, string?))?,
[any]: nil,
}

Expand Down Expand Up @@ -90,7 +90,7 @@ end
--[=[
@interface CollectionOptions<T>
@within Lapis
.validate (any) -> true | (false, string) -- Takes a document's data and returns true on success or false and an error on fail.
.validate ((any) -> true | (false, string))? -- Takes a document's data and returns true on success or false and an error on fail.
.defaultData T | (key: string) -> T -- If set to a function, it's called when a new document is created and is passed the key of the document.
.migrations { Migration }? -- Migrations take old data and return new data. Order is first to last. For more information, see: [Migrations](../docs/Migrations).
]=]
Expand Down
42 changes: 0 additions & 42 deletions src/init.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ return function(x)
local defaultData = { a = { b = { c = 5 } } }

context.lapis.createCollection("baz", {
validate = function()
return true
end,
defaultData = defaultData,
})

Expand Down Expand Up @@ -145,9 +142,6 @@ return function(x)
defaultData = function()
return "default"
end,
validate = function()
return true
end,
})

local document = collection:load("document"):expect()
Expand All @@ -162,9 +156,6 @@ return function(x)
key = passed
return {}
end,
validate = function()
return true
end,
})

collection:load("document"):expect()
Expand All @@ -177,9 +168,6 @@ return function(x)
defaultData = function()
return returned
end,
validate = function()
return true
end,
})

local document = collection:load("document"):expect()
Expand All @@ -193,9 +181,6 @@ return function(x)
defaultData = function()
return {}
end,
validate = function()
return true
end,
})

local document = collection:load("document"):expect()
Expand Down Expand Up @@ -346,9 +331,6 @@ return function(x)

x.test("error is thrown if a migration returns nil", function(context)
local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = {},
migrations = {
function() end,
Expand Down Expand Up @@ -414,9 +396,6 @@ return function(x)

x.test("migrations should work with tables and functions", function(context)
local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
migrations = {
{
Expand Down Expand Up @@ -445,9 +424,6 @@ return function(x)
"throws when migration version is ahead of latest version and is not backwards compatible",
function(context)
local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
migrations = {
function(old)
Expand Down Expand Up @@ -476,9 +452,6 @@ return function(x)
end

local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
migrations = {
{ migrate = migrate, backwardsCompatible = true },
Expand All @@ -491,9 +464,6 @@ return function(x)
otherLapis.setConfig({ dataStoreService = context.dataStoreService, loadAttempts = 1 })

local otherCollection = otherLapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
})

Expand All @@ -503,9 +473,6 @@ return function(x)

x.test("handles lastCompatibleVersion == nil", function(context)
local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
})

Expand All @@ -526,9 +493,6 @@ return function(x)
end

local collection = context.lapis.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
migrations = {
{ migrate = migrate, backwardsCompatible = false },
Expand All @@ -549,9 +513,6 @@ return function(x)
lapisWithV0.setConfig({ dataStoreService = context.dataStoreService, loadAttempts = 1 })

local collectionWithV0 = lapisWithV0.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
})

Expand All @@ -563,9 +524,6 @@ return function(x)
lapisWithV1.setConfig({ dataStoreService = context.dataStoreService, loadAttempts = 1 })

local collectionWithV1 = lapisWithV1.createCollection("collection", {
validate = function()
return true
end,
defaultData = "a",
migrations = {
{ migrate = migrate, backwardsCompatible = false },
Expand Down