Skip to content

Commit

Permalink
fix: avoid polluting the given schema (#42)
Browse files Browse the repository at this point in the history
When generating the validator, this library will add `id` field in the
schema to store useful data. We should purge it after.
  • Loading branch information
spacewander committed Nov 26, 2020
1 parent 201542d commit 0049837
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/jsonschema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,11 @@ return {
parse_ipv6 = custom and custom.parse_ipv6 or parse_ipv6
}
local name = custom and custom.name
return generate_main_validator_ctx(schema, custom):as_func(name, validatorlib, customlib)
local ctx = generate_main_validator_ctx(schema, custom):as_func(name, validatorlib, customlib)
if type(schema) == "table" then
schema.id = nil
end
return ctx
end,
-- debug only
generate_validator_code = function(schema, custom)
Expand Down
1 change: 1 addition & 0 deletions t/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local rule = {
-- print(code)

local validator = jsonschema.generate_validator(rule)
assert(rule.id == nil, "fail: schema is polluted")

local conf = {}
local ok = validator(conf)
Expand Down

0 comments on commit 0049837

Please sign in to comment.