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

default #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

default #52

wants to merge 2 commits into from

Conversation

andreineculau
Copy link
Member

@andreineculau andreineculau commented May 20, 2017

subset of @lazedo 's #42


This change is Reviewable

?not_found ->
case get_value(?DEFAULT, PropertySchema) of
?not_found -> CurrentState;
Default -> check_default(PropertyName, PropertySchema, Default, CurrentState)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 382 is too long: Default -> check_default(PropertyName, PropertySchema, Default, CurrentState).

is_valid_default(Types, Default, State)
when is_list(Types) ->
check_default_for_type(Default, State)
andalso lists:any(fun(Type) -> is_type_valid(Default, Type, State) end, Types);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 1078 is too long: andalso lists:any(fun(Type) -> is_type_valid(Default, Type, State) end, Types);.

],

[ ?assertEqual({ok, Value}
,jesse_schema_validator:validate(Schema, Value, [])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space right "," on line 46


%% @doc check if json object is_empty.
-spec is_json_object_empty(Value :: any()) -> boolean().
is_json_object_empty({struct, Value}) when is_list(Value) andalso Value =:= [] -> true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 74 is too long: is_json_object_empty({struct, Value}) when is_list(Value) andalso Value =:= [] -> true;.

check_default_for_type(Default, State) ->
jesse_state:validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 1059 is too long: orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false).

is_json_object_empty({Value}) when is_list(Value) andalso Value =:= [] -> true;
%% handle `jsx' empty objects
is_json_object_empty([{}]) -> true;
?IF_MAPS(is_json_object_empty(Map) when erlang:is_map(Map) -> maps:size(Map) =:= 0;)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 78 is too long: ?IF_MAPS(is_json_object_empty(Map) when erlang:is_map(Map) -> maps:size(Map) =:= 0;).

, Value
, CurrentState
);
Default -> check_default(PropertyName, PropertySchema, Default, CurrentState)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 357 is too long: Default -> check_default(PropertyName, PropertySchema, Default, CurrentState).

check_default_for_type(Default, State) ->
jesse_state:validator_option('use_defaults', State, false)
andalso (not jesse_lib:is_json_object(Default)
orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 1381 is too long: orelse jesse_state:validator_option('apply_defaults_to_empty_objects', State, false).

,jesse_schema_validator:validate(Schema, Value, [])
)
, ?assertEqual({ok, Default}
,jesse_schema_validator:validate(Schema, Value, Options)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space right "," on line 49

validator_option(Option, #state{validator_options=Options}) ->
proplists:get_value(Option, Options).

-spec validator_option(Option :: atom(), State :: state(), Default :: any()) -> any().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 409 is too long: -spec validator_option(Option :: atom(), State :: state(), Default :: any()) -> any()..

@@ -1362,3 +1369,42 @@ valid_datetime(DateTimeBin) ->
_ ->
false
end.

%% @private
set_value(PropertyName, Value, State) ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

jesse_validator_draft4:set_value/3 calls undefined function jesse_state:set_value/3

@@ -1031,3 +1047,62 @@ add_to_path(State, Property) ->
%% @private
remove_last_from_path(State) ->
jesse_state:remove_last_from_path(State).

%% @private
set_value(PropertyName, Value, State) ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

jesse_validator_draft3:set_value/3 calls undefined function jesse_state:set_value/3

jesse_state:validator_option(Option, State, Default).

%% @private
set_value(PropertyName, Value, State) ->

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

jesse_validator_draft3:set_value/3 calls undefined function jesse_state:set_value/3

@andreineculau andreineculau force-pushed the pr-42-default branch 2 times, most recently from 3a0f529 to d29dd1c Compare May 20, 2017 21:29
]).

%% Includes
-include("jesse_schema_validator.hrl").

-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 62 is too long: -type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined..

]).

%% Includes
-include("jesse_schema_validator.hrl").

-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined.
-type setter_fun() :: fun((jesse:json_path(), jesse:json_term(), jesse:json_term()) -> jesse:json_term()) | undefined.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Line 63 is too long: -type setter_fun() :: fun((jesse:json_path(), jesse:json_term(), jesse:json_term()) -> jesse:json_term()) | undefined..

set_value(#state{setter_fun=undefined}=State, _Path, _Value) -> State;
set_value(#state{current_value=undefined}=State, _Path, _Value) -> State;
set_value(#state{setter_fun=Setter
,current_value=Value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Elvis:

Missing space right "," on line 443

@andreineculau andreineculau changed the base branch from master to pr-42-validator May 20, 2017 21:47
@andreineculau andreineculau mentioned this pull request May 20, 2017

-type error_list() :: list().

-type external_validator() :: fun((jesse:json_term(), state()) -> state())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

type state() undefined

}
).

-opaque options() :: #options{}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

opaque type options() is not exported

@gadgetci
Copy link

xref failed with exit status: 1

}
).

-opaque options() :: #options{}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

opaque type options() is not exported

@gadgetci
Copy link

compiler failed with exit status: 1


-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state

, Path
, NewValue
) ->
State#state{current_value = Setter(Path, NewValue, Value)}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

field current_value undefined in record state

set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;
set_value( #state{setter_fun = Setter
, current_value = Value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state

@@ -164,6 +175,9 @@ new(JsonSchema, Options) ->
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
, external_validator = ExternalValidator
, setter_fun = SetterFun
, current_value = Value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state


-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

field current_value undefined in record state

SetterFun = proplists:get_value( setter_fun
, Options
),
Value = proplists:get_value( with_value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

variable 'Value' is unused

, current_value = Value
} = State
, Path
, NewValue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

variable 'NewValue' is unused

@andreineculau
Copy link
Member Author

FWIW it's late and this PR needs more care and attention, but I have pushed anyway in case someone else has the time and will to move this forward

, Path
, NewValue
) ->
State#state{current_value = Setter(Path, NewValue, Value)}.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

field current_value undefined in record state

, current_value = Value
} = State
, Path
, NewValue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

variable 'NewValue' is unused


%% @doc Getter for `current_value'.
-spec get_current_value(State :: state()) -> jesse:json_term().
get_current_value(#state{current_value = Value}) -> Value.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

variable 'Value' is unbound

@@ -164,6 +172,9 @@ new(JsonSchema, Options) ->
, default_schema_ver = DefaultSchemaVer
, schema_loader_fun = LoaderFun
, external_validator = ExternalValidator
, setter_fun = SetterFun
, current_value = Value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state

set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;
set_value( #state{setter_fun = Setter
, current_value = Value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state

SetterFun = proplists:get_value( setter_fun
, Options
),
Value = proplists:get_value( with_value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

variable 'Value' is unused


-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Xref:

field current_value undefined in record state


-spec set_value(State :: state(), jesse:path(), jesse:json_term()) -> state().
set_value(#state{setter_fun = undefined} = State, _Path, _Value) -> State;
set_value(#state{current_value = undefined} = State, _Path, _Value) -> State;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Compiler:

field current_value undefined in record state

@andreineculau andreineculau changed the base branch from pr-42-validator to master May 21, 2017 23:07
lazedo added 2 commits May 22, 2017 10:31
allows to set values during validation
persistence depends on setter_fun option
@lazedo
Copy link
Contributor

lazedo commented May 30, 2017

@andreineculau what was wrong with the original pull request and why is it failing this part?

@velimir
Copy link

velimir commented Apr 20, 2018

hey @andreineculau could you please let us know what the current status of this PR is?

@andreineculau
Copy link
Member Author

Status is that i have just started parental leave today so "if the gods allow" i will start closing down issues/PR soon one way or the other 🎉

@velimir
Copy link

velimir commented Apr 20, 2018

@andreineculau congrats! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants