Skip to content

Commit

Permalink
Merge 1f378c2 into fe2d896
Browse files Browse the repository at this point in the history
  • Loading branch information
dvisztempacct committed May 16, 2018
2 parents fe2d896 + 1f378c2 commit f34c37d
Show file tree
Hide file tree
Showing 18 changed files with 2,946 additions and 2,027 deletions.
54 changes: 27 additions & 27 deletions __tests__/AjvArrayTests.re
Expand Up @@ -2,9 +2,9 @@ open Jest;

describe("array tests", () => {
let options = Ajv_options.make();
Ajv_options.allErrors(options, Js.true_);
Ajv_options.jsonPointers(options, Js.true_);
Ajv_options.removeAdditional(options, Js.true_);
Ajv_options.allErrors(options, true);
Ajv_options.jsonPointers(options, true);
Ajv_options.removeAdditional(options, true);
let validate = (schema, document) => {
let validate_ajv =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
Expand Down Expand Up @@ -74,12 +74,12 @@ describe("array tests", () => {
);
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("disrespected array limits should fail to validate", () => {
let validData =
Expand All @@ -90,12 +90,12 @@ describe("array tests", () => {
);
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.false_);
|> Expect.toBe(false);
});
test("disrespected array limits should fail to report invalid fields", () => {
let validData =
Expand All @@ -107,29 +107,29 @@ describe("array tests", () => {
let handler = v => {
let handlerResult =
switch (v) {
| `Valid(_) => Js.false_
| `Valid(_) => false
| `Invalid(err) =>
let x = Ajv.Error.toDict(err);
Belt_MapString.has(x, "foo") ? Js.true_ : Js.false_;
Belt_MapString.has(x, "foo") ? true : false;
};
handlerResult;
};
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("disrespected array item type should fail to validate", () => {
let validData =
Json.Encode.(object_([("foo", array(int, [|1, 2, 3, 4|]))]));
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.false_);
|> Expect.toBe(false);
});
test("disrespected array item type should indicate invalid field", () => {
let validData =
Expand All @@ -141,17 +141,17 @@ describe("array tests", () => {
let handler = v => {
let handlerResult =
switch (v) {
| `Valid(_) => Js.false_
| `Valid(_) => false
| `Invalid(err) =>
let x = Ajv.Error.toDict(err);
Belt_MapString.has(x, "foo") ? Js.true_ : Js.false_;
Belt_MapString.has(x, "foo") ? true : false;
};
handlerResult;
};
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("respected contains keyword should validate", () => {
let validData =
Expand All @@ -162,12 +162,12 @@ describe("array tests", () => {
);
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(containsSchema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("disrespected contains keyword should fail to validate", () => {
let validData =
Expand All @@ -176,12 +176,12 @@ describe("array tests", () => {
);
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(containsSchema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.false_);
|> Expect.toBe(false);
});
test("disrespected contains keyword should indicate invalid field", () => {
let validData =
Expand All @@ -191,16 +191,16 @@ describe("array tests", () => {
let handler = v => {
let handlerResult =
switch (v) {
| `Valid(_) => Js.false_
| `Valid(_) => false
| `Invalid(err) =>
let x = Ajv.Error.toDict(err);
Belt_MapString.has(x, "foo") ? Js.true_ : Js.false_;
Belt_MapString.has(x, "foo") ? true : false;
};
handlerResult;
};
validate(containsSchema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
});
82 changes: 43 additions & 39 deletions __tests__/AjvBasicDataReference.re
Expand Up @@ -2,53 +2,57 @@ open Jest;

describe("$data reference", () => {
let options = Ajv_options.make();
Ajv_options.setData(options, Js.true_);

Ajv_options.setData(options, true);
/* https://www.npmjs.com/package/ajv#data-reference */
test("smaller/larger example", () => {
let schema = Json.Encode.(object_([
("properties", object_([
("smaller", object_([
("type", string("number")),
("maximum", object_([
("$data", string("1/larger"))
]))
])),
("larger", object_([
("type", string("number"))
]))
]))
]));

let validData = Json.Encode.(object_([
("smaller", int(5)),
("larger", int(7)),
]));

let schema =
Json.Encode.(
object_([
(
"properties",
object_([
(
"smaller",
object_([
("type", string("number")),
("maximum", object_([("$data", string("1/larger"))])),
]),
),
("larger", object_([("type", string("number"))])),
]),
),
])
);
let validData =
Json.Encode.(object_([("smaller", int(5)), ("larger", int(7))]));
Ajv.ajv(options)
|> Ajv.validate(`Schema(schema), validData)
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});

test("example with self reference", () => {
let schema = Json.Encode.(object_([
("additionalProperties", object_([
("type", string("string")),
("format", object_([
("$data", string("0#"))
]))
]))
]));

let validData = Json.Encode.(object_([
("date-time", string("1963-06-19T08:30:06.283185Z")),
("email", string("joe.bloggs@example.com")),
]));

let schema =
Json.Encode.(
object_([
(
"additionalProperties",
object_([
("type", string("string")),
("format", object_([("$data", string("0#"))])),
]),
),
])
);
let validData =
Json.Encode.(
object_([
("date-time", string("1963-06-19T08:30:06.283185Z")),
("email", string("joe.bloggs@example.com")),
])
);
Ajv.ajv(options)
|> Ajv.validate(`Schema(schema), validData)
|> Expect.expect
|> Expect.toBe(Js.true_);
})
|> Expect.toBe(true);
});
});
22 changes: 11 additions & 11 deletions __tests__/AjvBasicFiltering.re
Expand Up @@ -2,14 +2,14 @@ open Jest;

describe("data filtering (removeAdditional option)", () => {
let options = Ajv_options.make();
Ajv_options.allErrors(options, Js.true_);
Ajv_options.jsonPointers(options, Js.true_);
Ajv_options.removeAdditional(options, Js.true_);
Ajv_options.allErrors(options, true);
Ajv_options.jsonPointers(options, true);
Ajv_options.removeAdditional(options, true);
let validate = (schema, document) => {
let validate_ajv =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
| `Sync(fn) => fn
| `Async(fn) => failwith("unexpected_async_mode")
| `Async(_) => failwith("unexpected_async_mode")
};
validate_ajv(document);
};
Expand Down Expand Up @@ -45,29 +45,29 @@ describe("data filtering (removeAdditional option)", () => {
);
let handler =
fun
| `Valid(_) => Js.true_
| `Invalid(_) => Js.false_;
| `Valid(_) => true
| `Invalid(_) => false;
validate(schema, validData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("errors should be returned as a json array", () => {
let invalidData = Json.Encode.(object_([("foo", string("bar"))]));
let handler =
fun
| `Valid(_) => Js.false_
| `Valid(_) => false
| `Invalid(err) =>
switch (Js.Json.classify(err)) {
| Js.Json.JSONArray(_) => Js.true_
| Js.Json.JSONArray(_) => true
| x =>
Js.log2("INVALID: ", x);
Js.false_;
false;
};
validate(schema, invalidData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
});
test("required errors should all be returned", () => {
let invalidData = Json.Encode.(object_([]));
Expand Down
12 changes: 6 additions & 6 deletions __tests__/AjvEscapedJsonPointers.re
Expand Up @@ -2,9 +2,9 @@ open Jest;

describe("json pointer escaping tests", () => {
let options = Ajv_options.make();
Ajv_options.allErrors(options, Js.true_);
Ajv_options.jsonPointers(options, Js.true_);
Ajv_options.removeAdditional(options, Js.true_);
Ajv_options.allErrors(options, true);
Ajv_options.jsonPointers(options, true);
Ajv_options.removeAdditional(options, true);
let validate = (schema, document) => {
let validate_ajv =
switch (Ajv.ajv(options) |> Ajv.compile(schema)) {
Expand Down Expand Up @@ -33,17 +33,17 @@ describe("json pointer escaping tests", () => {
let handler = v => {
let handlerResult =
switch (v) {
| `Valid(_) => Js.false_
| `Valid(_) => false
| `Invalid(err) =>
let x = Ajv.Error.toDict(err);
Belt_MapString.has(x, "foo/bar~baz") ? Js.true_ : Js.false_;
Belt_MapString.has(x, "foo/bar~baz") ? true : false;
};
handlerResult;
};
validate(schema, invalidData)
|> handler
|> Expect.expect
|> Expect.toBe(Js.true_);
|> Expect.toBe(true);
},
);
});

0 comments on commit f34c37d

Please sign in to comment.