<commit_before>-module(json_validator).
-export([validate/2, error_msg/1]).

-type prim() :: null | integer | boolean | float | string.
-type spec() :: prim()
              | {array, [spec()]}
              | {hom_array, spec()}
              | {object, [{binary(), spec()}]}
              | {value, term()}
              | {opt, [spec()]}.
-type not_error() :: {not_null | not_integer | not_boolean | not_float
                      | not_string | not_list | not_object}.
-type error() :: {not_error(), term()}
               | {incorrect_length, term(), non_neg_integer()}
               | {unexpected_value, term(), term()}
               | {all_options_failed, spec(), term()}
               | {missing_key, binary(), term()}.
-export_type([spec/0]).

-spec validate(spec(), term()) -> ok | {error, error()}.
% primitives
validate(null, null) -> ok;
validate(integer, Payload) when is_integer(Payload) -> ok;
validate(boolean, Payload) when is_boolean(Payload)-> ok;
validate(float, Payload) when is_float(Payload) -> ok;
validate(string, Payload) when is_binary(Payload) -> ok;
validate(Type, Payload) when is_atom(Type) ->
    {error, {list_to_atom("not_" ++ atom_to_list(Type)), Payload}};

% heterogenous lists
validate({array, _Types}, Payload) when not is_list(Payload) ->
    {error, {not_list, Payload}};<commit_msg>Make sure the json validator does not create new atoms.<commit_after>-module(json_validator).
-export([validate/2, error_msg/1]).

-type prim() :: null | integer | boolean | float | string.
-type spec() :: prim()
              | {array, [spec()]}
              | {hom_array, spec()}
              | {object, [{binary(), spec()}]}
              | {value, term()}
              | {opt, [spec()]}.
-type not_error() :: {not_null | not_integer | not_boolean | not_float
                      | not_string | not_list | not_object}.
-type error() :: {not_error(), term()}
               | {incorrect_length, term(), non_neg_integer()}
               | {unexpected_value, term(), term()}
               | {all_options_failed, spec(), term()}
               | {missing_key, binary(), term()}.
-export_type([spec/0]).

-spec validate(spec(), term()) -> ok | {error, error()}.
% primitives
validate(null, null) -> ok;
validate(integer, Payload) when is_integer(Payload) -> ok;
validate(boolean, Payload) when is_boolean(Payload)-> ok;
validate(float, Payload) when is_float(Payload) -> ok;
validate(string, Payload) when is_binary(Payload) -> ok;
validate(Type, Payload) when is_atom(Type) ->
    {error, {list_to_existing_atom("not_" ++ atom_to_list(Type)), Payload}};

% heterogenous lists
validate({array, _Types}, Payload) when not is_list(Payload) ->
    {error, {not_list, Payload}};
{'fraction_non_alphanumeric': 0.17842630217953453, 'fraction_numerical': 0.002216475803472479, 'mean_word_length': 3.134351145038168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4713115', 'n_tokens_mistral': 843, 'n_tokens_neox': 849, 'n_words': 206}