Skip to content

Commit

Permalink
Merge 45fd3b1 into 2607b93
Browse files Browse the repository at this point in the history
  • Loading branch information
dvisztempacct committed Mar 22, 2018
2 parents 2607b93 + 45fd3b1 commit 28303a4
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions __tests__/AjvBasicFiltering.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ describe("data filtering (removeAdditional option)", () => {
Ajv_options.allErrors(options, Js.true_);
Ajv_options.jsonPointers(options, Js.true_);
Ajv_options.removeAdditional(options, Js.true_);
let validate = (schema, document) => {
let validate_ajv = switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
| `Sync(fn) => fn
| `Async(fn) => failwith("unexpected_async_mode")
};
validate_ajv(document)
};
let schema =
Json.Encode.(
object_([
Expand All @@ -29,24 +36,15 @@ describe("data filtering (removeAdditional option)", () => {
("bar", object_([("baz", string("abc")), ("additional2", int(2))]))
])
);
let validate =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
| `Sync(fn) => fn
| `Async(_) => failwith("unexpected_async_mode")
};

let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
validate(validData) |> handler |> Expect.expect |> Expect.toBe(Js.true_);
validate(schema, validData) |> handler |> Expect.expect |> Expect.toBe(Js.true_);
});
test("errors should be returned as a json array", () => {
let invalidData = Json.Encode.(object_([("foo", string("bar"))]));
let validate =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
| `Sync(fn) => fn
| `Async(_) => failwith("unexpected_async_mode")
};
let handler =
fun
| `Valid(_) => Js.false_
Expand All @@ -57,23 +55,22 @@ describe("data filtering (removeAdditional option)", () => {
Js.log2("INVALID: ", x);
Js.false_;
};
validate(invalidData) |> handler |> Expect.expect |> Expect.toBe(Js.true_);
validate(schema, invalidData) |> handler |> Expect.expect |> Expect.toBe(Js.true_);
});
test("required errors should all be returned", () => {
let invalidData = Json.Encode.(object_([]));
let validate =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
| `Sync(fn) => fn
| `Async(_) => failwith("unexpected_async_mode")
};
let handler =
fun
| `Valid(_) => [||]
| `Invalid(err) => {
let x = Ajv.Error.toDict(err);
[|Belt_MapString.has(x, "foo"), Belt_MapString.has(x, "bar")|];
[|
Belt_MapString.has(x, "foo")
, Belt_MapString.has(x, "bar")
,
|];
};
validate(invalidData)
validate(schema, invalidData)
|> handler
|> Expect.expect
|> Expect.toEqual([|true, true|]);
Expand Down

0 comments on commit 28303a4

Please sign in to comment.