Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
May 23, 2012
Seth Falcon Allow object_map as a top-level spec
Previously ej:valid only supported specs that had a wrapping object
with at least one expected key. This patch allows the object_map value
spec to be used as a top-level spec for validation.
3ce80f8
Seth Falcon Improve test coverage for object_map
Add test for non object when expecting object for object_map
be22e1a
4  src/ej.erl
@@ -217,7 +217,7 @@ delete(Keys, Obj) when is_tuple(Keys) ->
217 217
 -type ej_object_map() :: {object_map, {{keys, ej_json_val_spec()},
218 218
                                        {values, ej_json_val_spec()}}}.
219 219
 
220  
--type ej_json_spec() :: {[ej_json_spec_rule()]}.
  220
+-type ej_json_spec() :: {[ej_json_spec_rule()]} | ej_object_map().
221 221
 -type ej_json_spec_rule() :: {ej_json_key_spec(), ej_json_val_spec()}.
222 222
 -type ej_json_key_spec() :: binary() | {opt, binary()}.
223 223
 -type ej_json_val_spec() :: binary()             |
@@ -237,6 +237,8 @@ delete(Keys, Obj) when is_tuple(Keys) ->
237 237
 %% first failure is encountered (validation specs are processed in
238 238
 %% order, depth first).  NOTE: this function is experimental and the
239 239
 %% API and definition of specs is subject to change.
  240
+valid({object_map, _}=Spec, Obj={OL}) when is_list(OL) ->
  241
+    check_value_spec(<<"no_key">>, Spec, Obj, #spec_ctx{});
240 242
 valid({L}, Obj={OL}) when is_list(L) andalso is_list(OL) ->
241 243
     valid(L, Obj, #spec_ctx{});
242 244
 valid({L}, Obj) when is_list(L) ->
26  test/ej_valid_test.erl
@@ -264,6 +264,8 @@ object_map_test_() ->
264 264
 
265 265
     BadMissing = {[]},
266 266
 
  267
+    BadNotObject = {[{<<"object">>, <<"notobject">>}]},
  268
+
267 269
     [
268 270
      ?_assertEqual(ok, ej:valid(Spec, Good)),
269 271
      ?_assertEqual(ok, ej:valid(Spec, GoodEmpty)),
@@ -284,7 +286,13 @@ object_map_test_() ->
284 286
 
285 287
      ?_assertEqual(#ej_invalid{type = missing, key = <<"object">>,
286 288
                                expected_type = object},
287  
-                   ej:valid(Spec, BadMissing))
  289
+                   ej:valid(Spec, BadMissing)),
  290
+
  291
+     ?_assertEqual(#ej_invalid{type = json_type, key = <<"object">>,
  292
+                               expected_type = object,
  293
+                               found_type = string,
  294
+                               found = <<"notobject">>},
  295
+                   ej:valid(Spec, BadNotObject))
288 296
     ].
289 297
 
290 298
 nested_specs_test_() ->
@@ -353,6 +361,22 @@ nested_specs_test_() ->
353 361
             ],
354 362
     [ ?_assertEqual(Expect, ej:valid(Spec, In)) || {In, Expect} <- Tests ].
355 363
 
  364
+value_spec_as_spec_test_() ->
  365
+    Spec = {object_map, {{keys, string},
  366
+                         {values, {array_map, string}}}},
  367
+    Good = {[{<<"a">>, [<<"b">>]}]},
  368
+    Empty = {[]},
  369
+    EmptyArray = {[{<<"a">>, []}]},
  370
+    NotArray = {[{<<"a">>, <<"bad">>}]},
  371
+    WrongArrayType = {[{<<"a">>, [1, 2]}]},
  372
+    [
  373
+     ?_assertEqual(ok, ej:valid(Spec, Good)),
  374
+     ?_assertEqual(ok, ej:valid(Spec, Empty)),
  375
+     ?_assertEqual(ok, ej:valid(Spec, EmptyArray)),
  376
+     ?_assertMatch(#ej_invalid{}, ej:valid(Spec, NotArray)),
  377
+     ?_assertMatch(#ej_invalid{}, ej:valid(Spec, WrongArrayType))
  378
+    ].
  379
+
356 380
 basic(Name) ->
357 381
     {[{<<"name">>, Name}]}.
358 382
 

No commit comments for this range

Something went wrong with that request. Please try again.