From 3082babff9ac9cbc3d3ad71a49b2d8ef7c799f47 Mon Sep 17 00:00:00 2001 From: Thales Macedo Garitezi Date: Wed, 4 Sep 2024 16:51:29 -0300 Subject: [PATCH] fix: convert `extends` to proplist before checking Fixes https://emqx.atlassian.net/browse/EMQX-12017 --- src/jesse_validator_draft3.erl | 5 ++- test/jesse_tests_draft3_SUITE.erl | 20 +++++++++++ .../extra/extendsExtra.json | 33 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/jesse_tests_draft3_SUITE_data/extra/extendsExtra.json diff --git a/src/jesse_validator_draft3.erl b/src/jesse_validator_draft3.erl index 13c3fc7..e0b8713 100644 --- a/src/jesse_validator_draft3.erl +++ b/src/jesse_validator_draft3.erl @@ -903,7 +903,10 @@ check_extends(Value, Extends, State) -> TmpState = case jesse_lib:is_json_object(Extends) of true -> - check_value(Value, Extends, set_current_schema(State, Extends)); + check_value( Value + , jesse_json_path:unwrap_value(Extends) + , set_current_schema(State, Extends) + ); false -> case is_list(Extends) of true -> check_extends_array(Value, Extends, State); diff --git a/test/jesse_tests_draft3_SUITE.erl b/test/jesse_tests_draft3_SUITE.erl index 4308333..66bd571 100644 --- a/test/jesse_tests_draft3_SUITE.erl +++ b/test/jesse_tests_draft3_SUITE.erl @@ -138,6 +138,26 @@ uniqueItems(Config) -> %% Extra +%% The original bug originated from starting from a map schema input, so it was +%% not triggered by `do_test', which loads the schema as proplists rather than +%% maps. +extends_smoke_test(_Config) -> + Schema = #{ + <<"$schema">> => <<"http://json-schema.org/draft-03/schema#">>, + <<"description">> => <<"a description">>, + <<"extends">> => + #{<<"properties">> => + #{<<"disallow">> => + #{<<"disallow">> => [<<"number">>],<<"required">> => true}}}, + <<"id">> => <<"http://json-schema.org/draft-03/schema#">>, + <<"title">> => <<"title">>, + <<"type">> => <<"object">>}, + Data = #{<<"disallow">> => <<"a">>}, + ?assertEqual({ok, Data}, jesse:validate_with_schema(Schema, Data)). + +extendsExtra(Config) -> + do_test("extendsExtra", Config). + itemsExtra(Config) -> do_test("itemsExtra", Config). diff --git a/test/jesse_tests_draft3_SUITE_data/extra/extendsExtra.json b/test/jesse_tests_draft3_SUITE_data/extra/extendsExtra.json new file mode 100644 index 0000000..1213ff6 --- /dev/null +++ b/test/jesse_tests_draft3_SUITE_data/extra/extendsExtra.json @@ -0,0 +1,33 @@ +[ + { + "description": "simple example using `extends` with `disallow` in the base schema", + "schema": { + "type": "object", + "extends": { + "properties": { + "disallow": { + "disallow": ["number"], + "required": true + } + } + } + }, + "tests": [ + { + "description": "valid value: string", + "data": {"disallow": "a"}, + "valid": true + }, + { + "description": "valid value: array", + "data": {"disallow": ["a", 1]}, + "valid": true + }, + { + "description": "invalid value: number", + "data": {"disallow": 1}, + "valid": false + } + ] + } +]