46
46
47
47
-export_type ([ state / 0
48
48
, allowed_errors / 0
49
+ , options / 0
49
50
]).
50
51
51
52
% % Includes
52
53
-include (" jesse_schema_validator.hrl" ).
53
54
54
55
% % Internal datastructures
55
56
-record ( state
56
- , { root_schema :: jesse :json_term ()
57
- , current_schema :: jesse :json_term ()
58
- , current_path :: [binary () | non_neg_integer ()]
59
- % % current path in reversed order
57
+ , { root_schema :: schema ()
58
+ , current_schema :: schema ()
59
+ , current_path :: current_path ()
60
60
, allowed_errors :: allowed_errors ()
61
- , error_list :: list ()
62
- , error_handler :: fun (( jesse_error :error_reason ()
63
- , [jesse_error :error_reason ()]
64
- , non_neg_integer ()
65
- ) -> list ()
66
- | no_return ()
67
- )
68
- , default_schema_ver :: binary ()
69
- , schema_loader_fun :: fun ((string ()) -> {ok , jesse :json_term ()}
70
- | jesse :json_term ()
71
- | ? not_found
72
- )
61
+ , error_list :: error_list ()
62
+ , error_handler :: error_handler ()
63
+ , default_schema_ver :: schema_ver ()
64
+ , schema_loader_fun :: schema_loader_fun ()
73
65
, external_validator :: external_validator ()
74
- , id :: http_uri : uri () | undefined
66
+ , id :: schema_id ()
75
67
}
76
68
).
77
69
78
- -type allowed_errors () :: non_neg_integer ()
79
- | ? infinity .
80
-
81
- -type external_validator () :: fun ((jesse :json_term (), state ()) -> state ())
82
- | undefined .
83
-
70
+ -opaque options () :: # options {}.
84
71
-opaque state () :: # state {}.
85
72
86
73
% %% API
87
74
% % @doc Adds `Property' to the `current_path' in `State'.
88
- -spec add_to_path (State :: state (),
89
- Property :: binary () | non_neg_integer ()) -> state ().
90
- add_to_path (State , Property ) ->
75
+ -spec add_to_path ( State :: state ()
76
+ , Item :: current_path_item ()
77
+ ) -> state ().
78
+ add_to_path (State , Item ) ->
91
79
CurrentPath = State # state .current_path ,
92
- State # state {current_path = [Property | CurrentPath ]}.
80
+ State # state {current_path = [Item | CurrentPath ]}.
93
81
94
82
% % @doc Getter for `allowed_errors'.
95
83
-spec get_allowed_errors (State :: state ()) -> allowed_errors ().
96
84
get_allowed_errors (# state {allowed_errors = AllowedErrors }) ->
97
85
AllowedErrors .
98
86
99
87
% % @doc Getter for `current_path'.
100
- -spec get_current_path (State :: state ()) -> [ binary () | non_neg_integer ()] .
88
+ -spec get_current_path (State :: state ()) -> current_path () .
101
89
get_current_path (# state {current_path = CurrentPath }) ->
102
90
CurrentPath .
103
91
104
92
% % @doc Getter for `current_schema'.
105
- -spec get_current_schema (State :: state ()) -> jesse : json_term ().
93
+ -spec get_current_schema (State :: state ()) -> schema ().
106
94
get_current_schema (# state {current_schema = CurrentSchema }) ->
107
95
CurrentSchema .
108
96
109
97
% % @doc Getter for `current_schema_id'.
110
- -spec get_current_schema_id (State :: state ()) -> binary () | undefined .
98
+ -spec get_current_schema_id (State :: state ()) -> schema_id () .
111
99
get_current_schema_id (# state { current_schema = CurrentSchema
112
100
, root_schema = RootSchema
113
101
}) ->
114
102
Default = jesse_json_path :value (? ID , RootSchema , ? not_found ),
115
103
jesse_json_path :value (? ID , CurrentSchema , Default ).
116
104
117
105
% % @doc Getter for `default_schema_ver'.
118
- -spec get_default_schema_ver (State :: state ()) -> binary ().
106
+ -spec get_default_schema_ver (State :: state ()) -> schema_ver ().
119
107
get_default_schema_ver (# state {default_schema_ver = SchemaVer }) ->
120
108
SchemaVer .
121
109
122
110
% % @doc Getter for `error_handler'.
123
- -spec get_error_handler (State :: state ()) -> fun (( jesse_error :error_reason ()
124
- , [jesse_error :error_reason ()]
125
- , non_neg_integer ()
126
- ) -> list () | no_return ()).
111
+ -spec get_error_handler (State :: state ()) -> error_handler ().
127
112
get_error_handler (# state {error_handler = ErrorHandler }) ->
128
113
ErrorHandler .
129
114
130
115
% % @doc Getter for `error_list'.
131
- -spec get_error_list (State :: state ()) -> list ().
116
+ -spec get_error_list (State :: state ()) -> error_list ().
132
117
get_error_list (# state {error_list = ErrorList }) ->
133
118
ErrorList .
134
119
135
120
% % @doc Returns newly created state.
136
- -spec new ( JsonSchema :: jesse : json_term ()
137
- , Options :: [{ Key :: atom (), Data :: any ()}]
121
+ -spec new ( JsonSchema :: schema ()
122
+ , Options :: options ()
138
123
) -> state ().
139
124
new (JsonSchema , Options ) ->
140
- ErrorHandler = proplists :get_value ( error_handler
141
- , Options
142
- , ? default_error_handler_fun
143
- ),
144
- AllowedErrors = proplists :get_value ( allowed_errors
145
- , Options
146
- , 0
147
- ),
125
+ AllowedErrors = proplists :get_value ( allowed_errors
126
+ , Options
127
+ , 0
128
+ ),
148
129
MetaSchemaVer = jesse_json_path :value ( ? SCHEMA
149
130
, JsonSchema
150
131
, ? default_schema_ver
@@ -153,13 +134,17 @@ new(JsonSchema, Options) ->
153
134
, Options
154
135
, MetaSchemaVer
155
136
),
137
+ ErrorHandler = proplists :get_value ( error_handler
138
+ , Options
139
+ , ? default_error_handler_fun
140
+ ),
141
+ ExternalValidator = proplists :get_value ( external_validator
142
+ , Options
143
+ ),
156
144
LoaderFun = proplists :get_value ( schema_loader_fun
157
145
, Options
158
146
, ? default_schema_loader_fun
159
147
),
160
- ExternalValidator = proplists :get_value ( external_validator
161
- , Options
162
- ),
163
148
NewState = # state { root_schema = JsonSchema
164
149
, current_path = []
165
150
, allowed_errors = AllowedErrors
@@ -185,19 +170,19 @@ set_allowed_errors(#state{} = State, AllowedErrors) ->
185
170
186
171
% % @doc Setter for `current_schema'.
187
172
-spec set_current_schema ( State :: state ()
188
- , NewSchema :: jesse : json_term ()
173
+ , NewSchema :: schema ()
189
174
) -> state ().
190
175
set_current_schema (# state {id = Id } = State , NewSchema ) ->
191
176
NewId = combine_id (Id , jesse_json_path :value (? ID , NewSchema , undefined )),
192
177
State # state {current_schema = NewSchema , id = NewId }.
193
178
194
179
% % @doc Setter for `error_list'.
195
- -spec set_error_list (State :: state (), ErrorList :: list ()) -> state ().
180
+ -spec set_error_list (State :: state (), ErrorList :: error_list ()) -> state ().
196
181
set_error_list (State , ErrorList ) ->
197
182
State # state {error_list = ErrorList }.
198
183
199
184
% % @doc Resolve a reference.
200
- -spec resolve_ref (State :: state (), Reference :: binary ()) -> state ().
185
+ -spec resolve_ref (State :: state (), Reference :: schema_ref ()) -> state ().
201
186
resolve_ref (State , Reference ) ->
202
187
Id = State # state .id ,
203
188
CanonicalReference = combine_id (Id , Reference ),
@@ -253,7 +238,7 @@ undo_resolve_ref(RefState, OriginalState) ->
253
238
254
239
% % @doc Retrieve a specific part of a schema
255
240
% % @private
256
- -spec load_local_schema ( Schema :: not_found | jesse : json_term ()
241
+ -spec load_local_schema ( Schema :: ? not_found | schema ()
257
242
, Path :: [binary ()]
258
243
) -> not_found | jesse :json_term ().
259
244
load_local_schema (? not_found , _Path ) ->
@@ -378,8 +363,10 @@ raw_canonical_path2([H|T], Acc) ->
378
363
end .
379
364
380
365
% % @doc Load a schema based on URI
381
- -spec load_schema (State :: state (), SchemaURI :: string () | binary ()) ->
382
- jesse :json_term () | ? not_found .
366
+ -spec load_schema ( State :: state ()
367
+ , SchemaURI :: string () | binary ()
368
+ ) -> schema ()
369
+ | ? not_found .
383
370
load_schema (State , SchemaURI ) when is_binary (SchemaURI ) ->
384
371
load_schema (State , unicode :characters_to_list (SchemaURI ));
385
372
load_schema (# state {schema_loader_fun = LoaderFun }, SchemaURI ) ->
0 commit comments