-
Notifications
You must be signed in to change notification settings - Fork 64
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
2600hz changes #42
Closed
Closed
2600hz changes #42
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fc761d9
restore default_schema_ver in undo_resolve_ref
lazedo 0f50fc3
create validate_ref
lazedo 4997f84
allow to get current schema id
lazedo 91f2525
provide schema id to error
lazedo ea502a6
allow extra validator
lazedo 58fac47
more explicit error for enums
lazedo 46f3d9a
allow a setter_fun in schema validator
lazedo a4bf90f
set default values for properties
lazedo 984635f
return errors in the order they occur
lazedo 46399e1
fix path with default
lazedo 67df6d6
fix throwing error in order
lazedo 5ba3b1c
Merge branch 'master' into 2600hz-patches-1
lazedo d17bc19
add condition to apply defaults
lazedo e8460e1
fix setter test
lazedo c651c5e
handle invalid refs
lazedo 3c455d1
fix default check when multiples types are provided
lazedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,8 +26,10 @@ | |
%% API | ||
-export([ add_to_path/2 | ||
, get_allowed_errors/1 | ||
, get_extra_validator/1 | ||
, get_current_path/1 | ||
, get_current_schema/1 | ||
, get_current_schema_id/1 | ||
, get_default_schema_ver/1 | ||
, get_error_handler/1 | ||
, get_error_list/1 | ||
|
@@ -48,6 +50,7 @@ | |
%% Includes | ||
-include("jesse_schema_validator.hrl"). | ||
|
||
-type extra_validator() :: fun((jesse:json_term(), state()) -> state()) | undefined. | ||
%% Internal datastructures | ||
-record( state | ||
, { root_schema :: jesse:json_term() | ||
|
@@ -67,6 +70,7 @@ | |
jesse:json_term() | | ||
?not_found | ||
) | ||
, extra_validator :: extra_validator() | ||
, id :: binary() | 'undefined' | ||
} | ||
). | ||
|
@@ -96,6 +100,14 @@ get_current_path(#state{current_path = CurrentPath}) -> | |
get_current_schema(#state{current_schema = CurrentSchema}) -> | ||
CurrentSchema. | ||
|
||
%% @doc Getter for `current_schema_id'. | ||
-spec get_current_schema_id(State :: state()) -> binary() | undefined. | ||
get_current_schema_id(#state{current_schema = CurrentSchema | ||
,root_schema = RootSchema | ||
}) -> | ||
Default = jesse_json_path:value(?ID, RootSchema, ?not_found), | ||
jesse_json_path:value(?ID, CurrentSchema, Default). | ||
|
||
%% @doc Getter for `default_schema_ver'. | ||
-spec get_default_schema_ver(State :: state()) -> binary(). | ||
get_default_schema_ver(#state{default_schema_ver = SchemaVer}) -> | ||
|
@@ -139,13 +151,17 @@ new(JsonSchema, Options) -> | |
, Options | ||
, ?default_schema_loader_fun | ||
), | ||
ExtraValidator = proplists:get_value( extra_validator | ||
, Options | ||
), | ||
NewState = #state{ root_schema = JsonSchema | ||
, current_path = [] | ||
, allowed_errors = AllowedErrors | ||
, error_list = [] | ||
, error_handler = ErrorHandler | ||
, default_schema_ver = DefaultSchemaVer | ||
, schema_loader_fun = LoaderFun | ||
, extra_validator = ExtraValidator | ||
}, | ||
set_current_schema(NewState, JsonSchema). | ||
|
||
|
@@ -183,7 +199,7 @@ resolve_ref(State, Reference) -> | |
Path = jesse_json_path:parse(Pointer), | ||
case load_local_schema(State#state.root_schema, Path) of | ||
?not_found -> | ||
jesse_error:handle_schema_invalid(?schema_invalid, State); | ||
jesse_error:handle_schema_invalid(Reference, State); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't the |
||
Schema -> | ||
set_current_schema(State, Schema) | ||
end; | ||
|
@@ -194,7 +210,7 @@ resolve_ref(State, Reference) -> | |
), | ||
case load_schema(State, BaseURI) of | ||
?not_found -> | ||
jesse_error:handle_schema_invalid(?schema_invalid, State); | ||
jesse_error:handle_schema_invalid(Reference, State); | ||
RemoteSchema -> | ||
SchemaVer = | ||
jesse_json_path:value(?SCHEMA, RemoteSchema, ?default_schema_ver), | ||
|
@@ -205,7 +221,7 @@ resolve_ref(State, Reference) -> | |
Path = jesse_json_path:parse(Pointer), | ||
case load_local_schema(RemoteSchema, Path) of | ||
?not_found -> | ||
jesse_error:handle_schema_invalid(?schema_invalid, State); | ||
jesse_error:handle_schema_invalid(Reference, State); | ||
Schema -> | ||
set_current_schema(NewState, Schema) | ||
end | ||
|
@@ -217,6 +233,7 @@ resolve_ref(State, Reference) -> | |
undo_resolve_ref(RefState, OriginalState) -> | ||
RefState#state{ root_schema = OriginalState#state.root_schema | ||
, current_schema = OriginalState#state.current_schema | ||
, default_schema_ver = OriginalState#state.default_schema_ver | ||
, id = OriginalState#state.id | ||
}. | ||
|
||
|
@@ -367,3 +384,5 @@ load_schema(#state{schema_loader_fun = LoaderFun}, SchemaURI) -> | |
%% io:format("load_schema: ~p\n", [{_C, _E, erlang:get_stacktrace()}]), | ||
?not_found | ||
end. | ||
|
||
get_extra_validator(#state{extra_validator=Fun}) -> Fun. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the sugar-power for this change, and I've been tempted to do similar, but explain to me why wouldn't you first validate with jesse and then validate for more (e.g. realtime semantics), rather than call a function for all nested values (while I assume you're only interested in validate a handful) ?