Skip to content

Commit

Permalink
Merge 3621d2c into 38009a4
Browse files Browse the repository at this point in the history
  • Loading branch information
dvisztempacct committed Mar 26, 2018
2 parents 38009a4 + 3621d2c commit cdd270c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 16 deletions.
37 changes: 37 additions & 0 deletions __tests__/AjvNumericTests.re
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,41 @@ describe("numeric tests", () => {
|> Expect.expect
|> Expect.toBe(Js.false_);
});
test(
"disrespected minimum and exclusiveMaximum should report correct fields",
() => {
let invalidData =
Json.Encode.(object_([("foo", int(-1)), ("bar", int(0))]));
let handler = v => {
let handlerResult =
switch (v) {
| `Valid(_) => [|true, true, true|]
| `Invalid(err) =>
let x = Ajv.Error.toDict(err);
[|Belt_MapString.has(x, "foo"), Belt_MapString.has(x, "bar")|];
};
handlerResult;
};
validate(schema, invalidData)
|> handler
|> Expect.expect
|> Expect.toEqual([|true, true|]);
});
test(
"disrespected exclusiveMinimum and maximum should report correct fields",
() => {
let invalidData =
Json.Encode.(object_([("foo", int(9001)), ("bar", int(-9000))]));
let handler =
fun
| `Valid(_) => [||]
| `Invalid(err) => {
let x = Ajv.Error.toDict(err);
[|Belt_MapString.has(x, "foo"), Belt_MapString.has(x, "bar")|];
};
validate(schema, invalidData)
|> handler
|> Expect.expect
|> Expect.toEqual([|true, true|]);
});
});
60 changes: 44 additions & 16 deletions src/Ajv_error.re
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
type t = {
key: string,
message: string
message: string,
};

module RawValidationError = {
module RawParams = {
type t =
| MissingProperty(string)
| Type(string);
let fromJson = (keyword, json) => {
switch keyword {
| LimitError(string, int)
| TypeError(string);
let fromJson = (keyword, json) =>
switch (keyword) {
| "required" =>
MissingProperty(Json.Decode.(field("missingProperty", string, json)))
| "type" => Type(Json.Decode.(field("type", string, json)))
| "type" => TypeError(Json.Decode.(field("type", string, json)))
| "minimum" =>
LimitError("foo", Json.Decode.(field("limit", int, json)))
| "maximum" =>
LimitError("foo", Json.Decode.(field("limit", int, json)))
| "exclusiveMaximum" =>
LimitError("foo", Json.Decode.(field("limit", int, json)))
| "exclusiveMinimum" =>
LimitError("foo", Json.Decode.(field("limit", int, json)))
| _ => failwith({j|Unknown keyword: $keyword|j})
};
};
};
type t = {
keyword: string,
Expand All @@ -24,7 +32,7 @@ module RawValidationError = {
params: RawParams.t,
message: string,
schema: option(string),
parentSchema: option(string)
parentSchema: option(string),
};
let fromJson = json => {
let keyword = Json.Decode.field("keyword", Json.Decode.string, json);
Expand All @@ -35,19 +43,39 @@ module RawValidationError = {
params: json |> field("params", RawParams.fromJson(keyword)),
message: json |> field("message", string),
schema: json |> optional(field("schema", string)),
parentSchema: json |> optional(field("parentSchema", string))
parentSchema: json |> optional(field("parentSchema", string)),
};
};
let toError = ({dataPath, message, params, _}) =>
switch params {
| RawParams.MissingProperty(key) => {key, message}
| RawParams.Type(_) => {key: dataPath, message}
let toError = ({keyword, dataPath, message, params, _}) =>
switch (keyword, params) {
| ("required", MissingProperty(key)) => {key, message}
/* TODO dataPath != key */
| ("type", TypeError(_))
| ("minimum", LimitError(_))
| ("maximum", LimitError(_))
| ("exclusiveMinimum", LimitError(_))
| ("exclusiveMaximum", LimitError(_)) => {
key: String.sub(dataPath, 1, String.length(dataPath) - 1),
message,
}
| _ => failwith({j|Unknown keyword: $keyword|j})
};
};

let toDict = (json) => {
let toDict = json =>
Json.Decode.list(RawValidationError.fromJson, json)
|> Belt_List.map(_, RawValidationError.toError)
|> Belt_List.reduce(_, Belt_MapString.empty, (m, e) => Belt_MapString.set(m, e.key, e.message))
;
};
|> Belt_List.reduce(_, Belt_MapString.empty, (m, e) =>
Belt_MapString.set(m, e.key, e.message)
);

let logDict = dict =>
Belt_MapString.forEach(
dict,
(k, v) => {
Js.log("kv");
Js.log(k);
Js.log(v);
();
},
);

0 comments on commit cdd270c

Please sign in to comment.