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

2600hz changes #42

Closed
wants to merge 16 commits into from
Closed

2600hz changes #42

wants to merge 16 commits into from

Conversation

lazedo
Copy link
Contributor

@lazedo lazedo commented Feb 24, 2017

some changes that fix/enhance jesse

  • fixes mixing draft3/draft4 schemas
  • export current_schema_id
  • allows extra validator
  • explicit error for enum
  • allow a setter_fun in options
  • set defaults

the fixes the mix between draft3/draft4 schemas
wraps the logic of validating referenced schemas
this will extra validations for schema elements
example : lookup some value in a database an provide an error
allows to set values during validation
@lazedo lazedo closed this Feb 24, 2017
@lazedo lazedo reopened this Feb 24, 2017
persistence depends on setter_fun option
@@ -192,7 +192,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);
Copy link
Member

Choose a reason for hiding this comment

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

isn't the $ref already part of the error?
I've cherry-picked your change, and instead output the canonical reference (the output from combine_id). That is slightly more valuable i.e. see how the $ref was expanding, and see what jesse tried to find and load.

@@ -112,6 +112,7 @@
-define(not_one_schema_valid, 'not_one_schema_valid').
-define(not_schema_valid, 'not_schema_valid').
-define(wrong_not_schema, 'wrong_not_schema').
-define(external_error, 'external_error').
Copy link
Member

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) ?

@@ -64,7 +64,7 @@
) -> [error_reason()] | no_return().
default_error_handler(Error, ErrorList, AllowedErrors) ->
case AllowedErrors > length(ErrorList) orelse AllowedErrors =:= 'infinity' of
true -> [Error | ErrorList];
true -> ErrorList ++ [Error];
false -> throw([Error | ErrorList])
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this fix also apply to the false branch ?

@andreineculau
Copy link
Member

andreineculau commented Mar 12, 2017

would you care to give some detailed rationale for

  • allow a setter_fun in schema validator
  • defaults

thanks

@lazedo
Copy link
Contributor Author

lazedo commented Mar 13, 2017

@andreineculau
for defaults
if you give an empty object {} for a schema with defaults in its properties, it will fill up the object with the defaults and continue validation.

for setter_fun
when applying default, setter_fun will allow to actually set the value and return the value as part of validation.

example below for getting defaults for a schema

Options = [{'schema_loader_fun', fun kz_json_schema:fload/1}
                 ,{'allowed_errors', 'infinity'}
                 ,{'extra_validator', fun kz_json_schema_extensions:extra_validator/2}
                 ,{'setter_fun', fun kz_json:set_value/3}
                 ],
{ok, JObj} = kz_json_schema:validate(<<"system_config.conferences">>, kz_json:new(), Options),
io:format("~s~n", [kz_json:encode(JObj, ['pretty'])]).

{
  "entry_tone" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,659);v=-7;>=3;+=.1;%(800,0,659,783)",
  "exit_tone" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,440);v=-7;>=3;+=.1;%(800,0,349,440)",
  "moderator_entry_tone" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,659);v=-7;>=3;+=.1;%(800,0,659,783)",
  "moderator_exit_tone" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,440);v=-7;>=3;+=.1;%(800,0,349,440)",
  "number_timeout" : 5000,
  "pin_timeout" : 5000,
  "profiles" : {
    "default" : {
      "alone-sound" : "prompt://system_media/conf-alone/en-us",
      "caller-controls" : "default",
      "comfort-noise" : 1000,
      "deaf-sound" : "prompt://system_media/conf-deaf/en-us",
      "energy-level" : 20,
      "enter-sound" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,659);v=-7;>=3;+=.1;%(800,0,659,783)",
      "exit-sound" : "tone_stream://v=-7;>=2;+=.1;%(300,0,523,440);v=-7;>=3;+=.1;%(800,0,349,440)",
      "interval" : 20,
      "locked-sound" : "prompt://system_media/conf-max_participants/en-us",
      "max-member" : 200,
      "max-members-sound" : "prompt://system_media/conf-max_participants/en-us",
      "member-enter-sound" : "prompt://system_media/conf-joining_conference/en-us",
      "moh-sound" : "$${hold_music}",
      "muted-sound" : "prompt://system_media/conf-muted/en-us",
      "rate" : 8000,
      "undeaf-sound" : "prompt://system_media/conf-undeaf/en-us",
      "unmuted-sound" : "prompt://system_media/conf-unmuted/en-us"
    },
    "page" : {
      "caller-controls" : "default",
      "comfort-noise" : 1000,
      "energy-level" : 20,
      "enter-sound" : "",
      "interval" : 20,
      "moh-sound" : "",
      "rate" : 8000
    }
  },
  "review_name" : false,
  "route_win_timeout" : 3000,
  "support_name_announcement" : true
}

@andreineculau
Copy link
Member

@lazedo thanks for the clear explanation!

I can only think of two requests:

  • can you add an option for this behaviour e.g. use_default, which would be on by default
  • setter_fun maybe renamed to default_setter_fun ?!

Wdyt?

@lazedo
Copy link
Contributor Author

lazedo commented Mar 13, 2017

@andreineculau i'm ok with adding the use_default and we can default it to false to preserve current behaviour. thoughts?
as for the the setter_fun, why the rename ? if its not provided, there's no default to set the values, and since the library can act on different json implementations (jsx and other) there's really no easy way to set a default_setter_fun

@lazedo
Copy link
Contributor Author

lazedo commented Mar 13, 2017

@andreineculau i ended up adding a 'validator_options' to control the defaults behaviour.
use_defaults -> to control applying defaults
'apply_defaults_to_empty_objects' -> to control if we should iterate object shcema properties and apply defaults.

this last is to bypass a recursive default {} with {$ref, #}

let me know if you agree or if there's anything else needed to wok on.
Thanks

@lazedo
Copy link
Contributor Author

lazedo commented Mar 27, 2017

@andreineculau hi, anything else required here ?

needed when allowed_errors is `infinity`
@andreineculau
Copy link
Member

andreineculau commented Mar 29, 2017 via email

@lazedo
Copy link
Contributor Author

lazedo commented May 10, 2017

@andreineculau ping. we would love to create a 1.5 release after this merge

when is_binary(Type) ->
check_default_for_type(Default, State)
andalso is_type_valid(Default, Type, State);
is_valid_default(Types, Default, State)
Copy link
Member

Choose a reason for hiding this comment

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

Types - typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

expressing that in this clause we're dealing with a list used in lists:any

This was referenced May 20, 2017
@andreineculau
Copy link
Member

andreineculau commented May 20, 2017

closing in favour of #51 and #52 (other commits already merged onto master). it was impossible for me to review properly as a mash. thanks!

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

Successfully merging this pull request may close these issues.

2 participants