Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiko Fernandez-Reyes authored and seriyps committed Jan 27, 2022
1 parent 8bfab5b commit ee24d77
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 54 deletions.
2 changes: 0 additions & 2 deletions src/jesse_schema_validator.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
%% Constant definitions for data errors
-define(data_error, 'data_error').
-define(data_invalid, 'data_invalid').
-define(missing_id_field, 'missing_id_field').
-define(missing_required_property, 'missing_required_property').
-define(missing_dependency, 'missing_dependency').
-define(no_match, 'no_match').
Expand All @@ -148,7 +147,6 @@
-define(more_than_one_schema_valid, 'more_than_one_schema_valid').
-define(not_schema_valid, 'not_schema_valid').
-define(validation_always_fails, 'validation_always_fails').
-define(wrong_not_schema, 'wrong_not_schema').
-define(external, 'external').

%%
Expand Down
23 changes: 12 additions & 11 deletions src/jesse_validator_draft6.erl
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ check_value(Value, [{?CONTAINS, Schema} | Attrs], State) ->
check_value(Value, [{?EXAMPLES, _Examples} | Attrs], State) ->
NewState = case jesse_lib:is_array(Value) of
true ->
%% No need to check. The schema is valid, by definition, at this point.
%% No need to check.
%% The schema is valid, by definition, at this point.
State;
false -> handle_data_invalid(?not_array, Value, State)
end,
Expand Down Expand Up @@ -419,8 +420,7 @@ check_properties(Value, Properties, State) ->
CurrentState;
Property ->
NewState = set_current_schema( CurrentState
, PropertySchema
),
, PropertySchema),
check_value( PropertyName
, Property
, PropertySchema
Expand Down Expand Up @@ -448,12 +448,13 @@ check_pattern_properties(Value, PatternProperties, State) ->

check_property_names(Value, PatternProperties, State) ->
P1P2 = [{P1, P2} || P1 <- unwrap(Value), P2 <- unwrap(PatternProperties)],
TmpState = lists:foldl( fun({Property, Pattern}, CurrentState) ->
check_match_property(Property, Pattern, CurrentState)
end
, State
, P1P2
),
TmpState = lists:foldl(
fun({Property, Pattern}, CurrentState) ->
check_match_property(Property, Pattern, CurrentState)
end
, State
, P1P2
),
set_current_schema(TmpState, get_current_schema(State)).

%% @private
Expand All @@ -470,7 +471,7 @@ check_match({PropertyName, PropertyValue}, {Pattern, Schema}, State) ->
end.

%% @private
check_match_property({PropertyName, _PropertyValue}, {_PatternKeyword, Regex}, State) ->
check_match_property({PropertyName, _}, {_, Regex}, State) ->
case re:run(PropertyName, Regex, [{capture, none}, unicode]) of
match ->
State;
Expand Down Expand Up @@ -976,7 +977,7 @@ check_format(Value, _Format = <<"ipv6">>, State) when is_binary(Value) ->
check_format(Value, _Format = <<"uri">>, State) when is_binary(Value) ->
%% not yet supported
State;
check_format(Value, _Format = <<"uri-reference">>, State) when is_binary(Value) ->
check_format(Value, <<"uri-reference">>, State) when is_binary(Value) ->
case valid_uri_string(Value) of
true -> State;
false -> handle_data_invalid(?wrong_format, Value, State)
Expand Down
98 changes: 57 additions & 41 deletions test/jesse_schema_validator_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -420,36 +420,44 @@ data_exclusive_maximum_minimum_test() ->
%% A case without errors
?assertEqual(
{ok, ValidNumber},
jesse_schema_validator:validate(Schema(<<"exclusiveMaximum">>, ?json_schema_draft6), ValidNumber, [])
jesse_schema_validator:validate(
Schema(<<"exclusiveMaximum">>, ?json_schema_draft6), ValidNumber, [])
),

?assertEqual(
{ok, ValidNumber+2},
jesse_schema_validator:validate(Schema(<<"exclusiveMinimum">>, ?json_schema_draft6), ValidNumber+2, [])
jesse_schema_validator:validate(
Schema(<<"exclusiveMinimum">>, ?json_schema_draft6), ValidNumber+2, [])
),

?assertThrow( [ { data_invalid,
{ [ { <<"$schema">> , <<"http://json-schema.org/draft-06/schema#">> }
{ [ { <<"$schema">> , ?json_schema_draft6 }
, { <<"type">> , <<"number">> }
, { <<"exclusiveMinimum">> , 43 }
]
}
, not_in_range, 42 ,[]
, not_in_range, 42 , []
}
],
jesse_schema_validator:validate(Schema(<<"exclusiveMinimum">>, ?json_schema_draft6), ValidNumber, [])
jesse_schema_validator:validate(
Schema(<<"exclusiveMinimum">>, ?json_schema_draft6),
ValidNumber, []
)
),

?assertThrow( [ { data_invalid,
{ [ { <<"$schema">> , <<"http://json-schema.org/draft-06/schema#">> }
{ [ { <<"$schema">> , ?json_schema_draft6 }
, { <<"type">> , <<"number">> }
, { <<"exclusiveMaximum">> , 43 }
]
}
, not_in_range, 44 ,[]
, not_in_range, 44 , []
}
],
jesse_schema_validator:validate(Schema(<<"exclusiveMaximum">>, ?json_schema_draft6), ValidNumber+2, [])
jesse_schema_validator:validate(
Schema(<<"exclusiveMaximum">>, ?json_schema_draft6),
ValidNumber+2, []
)
).


Expand All @@ -471,14 +479,14 @@ data_propertyNames_test() ->
[{ data_invalid
, {[{ <<"$schema">>
, <<"http://json-schema.org/draft-06/schema#">>}
, { <<"type">>,<<"object">> }
, { <<"type">>, <<"object">> }
, { <<"propertyNames">>
, {[{ <<"pattern">> , <<"^[A-Z]*$">>}]}
}
]}
, no_match
,<<"foo">>
,[]}]
, <<"foo">>
, []}]
, jesse_schema_validator:validate(Schema, IllformedObject, [])
).

Expand All @@ -492,28 +500,34 @@ data_dollarid_test() ->
Object = {[{ <<"foo">>, <<"bar">> }]},
?assertEqual(
{ok, Object},
jesse_schema_validator:validate(SchemaWithId(?json_schema_draft4, <<"id">>), Object, [])
jesse_schema_validator:validate(
SchemaWithId(?json_schema_draft4, <<"id">>),
Object, []
)
),

?assertThrow([{schema_invalid,
{[{<<"$schema">>,
<<"http://json-schema.org/draft-04/schema#">>},
{<<"type">>,<<"object">>},
{<<"$id">>,<<"foo">>}]},
{<<"type">>, <<"object">>},
{<<"$id">>, <<"foo">>}]},
wrong_draft4_id_tag}],
jesse_schema_validator:validate(SchemaWithId(?json_schema_draft4, <<"$id">>), Object, [])),
jesse_schema_validator:validate(
SchemaWithId(?json_schema_draft4, <<"$id">>), Object, [])),

?assertThrow([{ schema_invalid,
{[{<<"$schema">>,
<<"http://json-schema.org/draft-06/schema#">>},
{<<"type">>,<<"object">>},
{<<"id">>,<<"foo">>}]},
{<<"type">>, <<"object">>},
{<<"id">>, <<"foo">>}]},
wrong_draft6_id_tag}],
jesse_schema_validator:validate(SchemaWithId(?json_schema_draft6, <<"id">>), Object, [])),
jesse_schema_validator:validate(
SchemaWithId(?json_schema_draft6, <<"id">>), Object, [])),

?assertEqual(
{ok, Object},
jesse_schema_validator:validate(SchemaWithId(?json_schema_draft6, <<"$id">>), Object, [])
jesse_schema_validator:validate(
SchemaWithId(?json_schema_draft6, <<"$id">>), Object, [])
).

data_contains_test() ->
Expand All @@ -531,14 +545,14 @@ data_contains_test() ->
),

?assertThrow([{data_invalid
, {[{ <<"$schema">>, <<"http://json-schema.org/draft-06/schema#">>}
, {[{ <<"$schema">>, ?json_schema_draft6}
, { <<"type">> , <<"array">> }
, { <<"contains">>
, {[{ <<"type">> , <<"number">> }]}
}
]}
, data_invalid
, [<<"foo">>,<<"bar">>]
, [<<"foo">>, <<"bar">>]
, []}],
jesse_schema_validator:validate(Schema, ArrayOfString, [])
).
Expand All @@ -554,13 +568,13 @@ data_const_test() ->
jesse_schema_validator:validate(Schema, <<"foo">>, [])
),
?assertThrow([{ data_invalid
, {[{<<"$schema">> , <<"http://json-schema.org/draft-06/schema#">> }
, {<<"type">>,<<"string">>}
, {[{<<"$schema">> , ?json_schema_draft6 }
, {<<"type">>, <<"string">>}
, {<<"const">>
, [ <<"foo">>,<<"bar">> ]
, [ <<"foo">>, <<"bar">> ]
}
]}
, not_in_enum,<<"qux">>,[]}],
, not_in_enum, <<"qux">>, []}],
jesse_schema_validator:validate(Schema, <<"qux">>, [])
).

Expand Down Expand Up @@ -603,9 +617,9 @@ object_property_with_boolean_value_test() ->
]},

?assertThrow([{data_invalid,
{[{<<"$schema">>,?json_schema_draft6},
{<<"type">>,<<"object">>},
{<<"properties">>,false}]},
{[{<<"$schema">>, ?json_schema_draft6},
{<<"type">>, <<"object">>},
{<<"properties">>, false}]},
?validation_always_fails,
{[]},
[]
Expand All @@ -630,9 +644,9 @@ pattern_property_with_boolean_value_test() ->
]},

?assertThrow([{data_invalid,
{[{<<"$schema">>,?json_schema_draft6},
{<<"type">>,<<"object">>},
{<<"propertyNames">>,false}]},
{[{<<"$schema">>, ?json_schema_draft6},
{<<"type">>, <<"object">>},
{<<"propertyNames">>, false}]},
?validation_always_fails,
{[]},
[]
Expand All @@ -659,18 +673,18 @@ array_items_with_boolean_value_test() ->
?assertThrow([{schema_invalid,
{[{<<"$schema">>,
<<"http://json-schema.org/draft-06/schema#">>},
{<<"type">>,<<"array">>},
{<<"items">>,false}]},
{wrong_type_items,{<<"not">>,{[]}}}}]
{<<"type">>, <<"array">>},
{<<"items">>, false}]},
{wrong_type_items, {<<"not">>, {[]}}}}]
, jesse_schema_validator:validate(InvalidSchema, [], [])
),

?assertThrow([{schema_invalid,
{[{<<"$schema">>,
<<"http://json-schema.org/draft-06/schema#">>},
{<<"type">>,<<"array">>},
{<<"items">>,false}]},
{wrong_type_items,{<<"not">>,{[]}}}}]
{<<"type">>, <<"array">>},
{<<"items">>, false}]},
{wrong_type_items, {<<"not">>, {[]}}}}]
, jesse_schema_validator:validate(InvalidSchema, [1], [])
).

Expand All @@ -689,10 +703,12 @@ contains_with_boolean_value_test() ->
, {<<"type">>, <<"array">>}
, {<<"contains">>, false}
]},
?assertThrow([{data_invalid,{[{<<"$schema">>,<<"http://json-schema.org/draft-06/schema#">>},
{<<"type">>,<<"array">>},
{<<"contains">>,false}]},
validation_always_fails,[],[]}]
?assertThrow([{data_invalid,
{[{<<"$schema">>,
<<"http://json-schema.org/draft-06/schema#">>},
{<<"type">>, <<"array">>},
{<<"contains">>, false}]},
validation_always_fails, [], []}]
, jesse_schema_validator:validate(InvalidSchema, [], [])
).

Expand Down

0 comments on commit ee24d77

Please sign in to comment.