-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #5
- Loading branch information
Showing
5 changed files
with
143 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,10 @@ | ||
import validator, { IntegerValidationError } from "./integer"; | ||
import { NumberValidationError } from "./number"; | ||
|
||
describe("validator", () => { | ||
it("can validate and invalidate a integer", () => { | ||
expect(validator({ type: "integer" }, "im a integer")).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, 123)).toBe(true); | ||
expect(validator({ type: "integer" }, {})).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, true)).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, false)).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, null)).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, 123.123)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("multipleOf", () => { | ||
expect(validator({ type: "integer", multipleOf: 10 }, 100)).toBe(true); | ||
expect(validator({ type: "integer", multipleOf: 100 }, 10)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("maximum", () => { | ||
expect(validator({ type: "integer", maximum: 10 }, 10)).toBe(true); | ||
expect(validator({ type: "integer", maximum: 10 }, 11)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("exclusiveMaximum", () => { | ||
expect(validator({ type: "integer", exclusiveMaximum: 10 }, 9)).toBe(true); | ||
expect(validator({ type: "integer", exclusiveMaximum: 10 }, 10)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("minimum", () => { | ||
expect(validator({ type: "integer", minimum: 10 }, 10)).toBe(true); | ||
expect(validator({ type: "integer", minimum: 10 }, 9)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("exclusiveMinimum", () => { | ||
expect(validator({ type: "integer", exclusiveMinimum: 10 }, 11)).toBe(true); | ||
expect(validator({ type: "integer", exclusiveMinimum: 10 }, 10)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("const", () => { | ||
expect(validator({ type: "integer", const: 123 }, 123)).toBe(true); | ||
expect(validator({ type: "integer", const: 456 }, 123)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
|
||
it("enum", () => { | ||
expect(validator({ type: "integer", enum: [123] }, 123)).toBe(true); | ||
expect(validator({ type: "integer", enum: [123] }, 456)).toBeInstanceOf(IntegerValidationError); | ||
|
||
expect(validator({ type: "integer", enum: [123, 456] }, 123)).toBe(true); | ||
expect(validator({ type: "integer", enum: [123, 456] }, 1)).toBeInstanceOf(IntegerValidationError); | ||
|
||
expect(validator({ type: "integer", enum: ["foo", "123", "bar"] }, 123)).toBeInstanceOf(IntegerValidationError); | ||
expect(validator({ type: "integer" }, "this is an integer")).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "integer" }, 123.10)).toBeInstanceOf(IntegerValidationError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import validator, { NumberValidationError } from "./number"; | ||
|
||
describe("validator", () => { | ||
it("can validate and invalidate a number", () => { | ||
expect(validator({ type: "number" }, "im a number")).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "number" }, 123)).toBe(true); | ||
expect(validator({ type: "number" }, {})).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "number" }, true)).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "number" }, false)).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "number" }, null)).toBeInstanceOf(NumberValidationError); | ||
expect(validator({ type: "number" }, 123.123)).toBe(true); | ||
}); | ||
|
||
it("multipleOf", () => { | ||
expect(validator({ type: "number", multipleOf: 10 }, 100)).toBe(true); | ||
expect(validator({ type: "number", multipleOf: 100 }, 10)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("maximum", () => { | ||
expect(validator({ type: "number", maximum: 10 }, 10)).toBe(true); | ||
expect(validator({ type: "number", maximum: 10 }, 11)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("exclusiveMaximum", () => { | ||
expect(validator({ type: "number", exclusiveMaximum: 10 }, 9)).toBe(true); | ||
expect(validator({ type: "number", exclusiveMaximum: 10 }, 10)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("minimum", () => { | ||
expect(validator({ type: "number", minimum: 10 }, 10)).toBe(true); | ||
expect(validator({ type: "number", minimum: 10 }, 9)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("exclusiveMinimum", () => { | ||
expect(validator({ type: "number", exclusiveMinimum: 10 }, 11)).toBe(true); | ||
expect(validator({ type: "number", exclusiveMinimum: 10 }, 10)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("const", () => { | ||
expect(validator({ type: "number", const: 123 }, 123)).toBe(true); | ||
expect(validator({ type: "number", const: 456 }, 123)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
|
||
it("enum", () => { | ||
expect(validator({ type: "number", enum: [123] }, 123)).toBe(true); | ||
expect(validator({ type: "number", enum: [123] }, 456)).toBeInstanceOf(NumberValidationError); | ||
|
||
expect(validator({ type: "number", enum: [123, 456] }, 123)).toBe(true); | ||
expect(validator({ type: "number", enum: [123, 456] }, 1)).toBeInstanceOf(NumberValidationError); | ||
|
||
expect(validator({ type: "number", enum: ["foo", "123", "bar"] }, 123)).toBeInstanceOf(NumberValidationError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import ValidationError from "../validation-error"; | ||
import { JSONSchemaObject } from "@json-schema-tools/meta-schema"; | ||
|
||
export class NumberValidationError implements Error { | ||
public name = "NumberValidationError"; | ||
public message: string; | ||
|
||
constructor(schema: JSONSchemaObject, data: any, reason: string) { | ||
this.message = [ | ||
"invalid data provided is not a valid integer", | ||
`reason: ${reason}`, | ||
].join("\n"); | ||
} | ||
} | ||
export default (schema: JSONSchemaObject, d: any): true | ValidationError => { | ||
if (typeof d !== "number") { | ||
return new NumberValidationError(schema, d, "Not a number type"); | ||
} | ||
|
||
if (schema.multipleOf) { | ||
if (d % schema.multipleOf !== 0) { | ||
return new NumberValidationError(schema, d, `number is not a multiple of ${schema.multipleOf}`); | ||
} | ||
} | ||
|
||
if (schema.maximum) { | ||
if (d > schema.maximum) { | ||
return new NumberValidationError(schema, d, `number exceeds maximum of ${schema.maximum}`); | ||
} | ||
} | ||
|
||
if (schema.exclusiveMaximum) { | ||
if (d >= schema.exclusiveMaximum) { | ||
return new NumberValidationError( | ||
schema, | ||
d, | ||
`number is greater than or equal to exclusive maximum of ${schema.exclusiveMaximum}` | ||
); | ||
} | ||
} | ||
|
||
if (schema.minimum) { | ||
if (d < schema.minimum) { | ||
return new NumberValidationError(schema, d, `number is less than minimum of ${schema.minimum}`); | ||
} | ||
} | ||
|
||
if (schema.exclusiveMinimum) { | ||
if (d <= schema.exclusiveMinimum) { | ||
return new NumberValidationError( | ||
schema, | ||
d, | ||
`number is less than or equal to exclusive minimum of ${schema.exclusiveMinimum}` | ||
); | ||
} | ||
} | ||
|
||
if (schema.const) { | ||
if (d !== schema.const) { | ||
return new NumberValidationError(schema, d, `must be: ${schema.const}`); | ||
} | ||
} | ||
|
||
if (schema.enum) { | ||
if (schema.enum.indexOf(d) === -1) { | ||
return new NumberValidationError(schema, d, `must be one of: ${schema.enum}`); | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters