Skip to content

Commit 0049837

Browse files
authored
fix: avoid polluting the given schema (#42)
When generating the validator, this library will add `id` field in the schema to store useful data. We should purge it after.
1 parent 201542d commit 0049837

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/jsonschema.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,11 @@ return {
11491149
parse_ipv6 = custom and custom.parse_ipv6 or parse_ipv6
11501150
}
11511151
local name = custom and custom.name
1152-
return generate_main_validator_ctx(schema, custom):as_func(name, validatorlib, customlib)
1152+
local ctx = generate_main_validator_ctx(schema, custom):as_func(name, validatorlib, customlib)
1153+
if type(schema) == "table" then
1154+
schema.id = nil
1155+
end
1156+
return ctx
11531157
end,
11541158
-- debug only
11551159
generate_validator_code = function(schema, custom)

t/default.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ local rule = {
1515
-- print(code)
1616

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

1920
local conf = {}
2021
local ok = validator(conf)

0 commit comments

Comments
 (0)