| 1 |
import _AjvJTD from "../ajv_jtd" |
| 2 |
import * as assert from "assert" |
| 3 |
|
| 4 |
describe("JTD int32range option", function () { |
| 5 |
|
| 6 |
describe("validation", () => { |
| 7 |
it("should limit range for int32 and uint32 types by default", () => { |
| 8 |
const ajv = new _AjvJTD() |
| 9 |
const validateInt32 = ajv.compile({type: "int32"}) |
| 10 |
assert.strictEqual(validateInt32(-2147483648), true) |
| 11 |
assert.strictEqual(validateInt32(-2147483649), false) |
| 12 |
assert.strictEqual(validateInt32(2147483647), true) |
| 13 |
assert.strictEqual(validateInt32(2147483648), false) |
| 14 |
const validateUint32 = ajv.compile({type: "uint32"}) |
| 15 |
assert.strictEqual(validateUint32(0), true) |
| 16 |
assert.strictEqual(validateUint32(-1), false) |
| 17 |
assert.strictEqual(validateUint32(4294967295), true) |
| 18 |
assert.strictEqual(validateUint32(4294967296), false) |
| 19 |
}) |
| 20 |
|
| 21 |
it("should NOT limit range for int32 and uint32 types with int32range: false", () => { |
| 22 |
const ajv = new _AjvJTD({int32range: false}) |
| 23 |
const validateInt32 = ajv.compile({type: "int32"}) |
| 24 |
assert.strictEqual(validateInt32(-2147483648), true) |
| 25 |
assert.strictEqual(validateInt32(-2147483649), true) |
| 26 |
assert.strictEqual(validateInt32(Number.MIN_SAFE_INTEGER), true) |
| 27 |
assert.strictEqual(validateInt32(2147483647), true) |
| 28 |
assert.strictEqual(validateInt32(2147483648), true) |
| 29 |
assert.strictEqual(validateInt32(Number.MAX_SAFE_INTEGER), true) |
| 30 |
const validateUint32 = ajv.compile({type: "uint32"}) |
| 31 |
assert.strictEqual(validateUint32(0), true) |
| 32 |
assert.strictEqual(validateUint32(-1), false) |
| 33 |
assert.strictEqual(validateUint32(4294967295), true) |
| 34 |
assert.strictEqual(validateUint32(4294967296), true) |
| 35 |
assert.strictEqual(validateUint32(Number.MAX_SAFE_INTEGER), true) |
| 36 |
}) |
| 37 |
}) |
| 38 |
|
| 39 |
describe("parsing", () => { |
| 40 |
it("should limit range for int32 and uint32 types by default", () => { |
| 41 |
const ajv = new _AjvJTD() |
| 42 |
const parseInt32 = ajv.compileParser({type: "int32"}) |
| 43 |
assert.strictEqual(parseInt32("-2147483648"), -2147483648) |
| 44 |
assert.strictEqual(parseInt32("-2147483649"), undefined) |
| 45 |
assert.strictEqual(parseInt32("2147483647"), 2147483647) |
| 46 |
assert.strictEqual(parseInt32("2147483648"), undefined) |
| 47 |
const parseUint32 = ajv.compileParser({type: "uint32"}) |
| 48 |
assert.strictEqual(parseUint32("0"), 0) |
| 49 |
assert.strictEqual(parseUint32("-1"), undefined) |
| 50 |
assert.strictEqual(parseUint32("4294967295"), 4294967295) |
| 51 |
assert.strictEqual(parseUint32("4294967296"), undefined) |
| 52 |
}) |
| 53 |
|
| 54 |
it("should NOT limit range for int32 and uint32 types with int32range: false", () => { |
| 55 |
const ajv = new _AjvJTD({int32range: false}) |
| 56 |
const parseInt32 = ajv.compileParser({type: "int32"}) |
| 57 |
assert.strictEqual(parseInt32("-2147483648"), -2147483648) |
| 58 |
assert.strictEqual(parseInt32("-2147483649"), -2147483649) |
| 59 |
assert.strictEqual(parseInt32("" + Number.MIN_SAFE_INTEGER), Number.MIN_SAFE_INTEGER) |
| 60 |
assert.strictEqual(parseInt32("2147483647"), 2147483647) |
| 61 |
assert.strictEqual(parseInt32("2147483648"), 2147483648) |
| 62 |
assert.strictEqual(parseInt32("" + Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER) |
| 63 |
const parseUint32 = ajv.compileParser({type: "uint32"}) |
| 64 |
assert.strictEqual(parseUint32("0"), 0) |
| 65 |
assert.strictEqual(parseUint32("-1"), undefined) |
| 66 |
assert.strictEqual(parseUint32("4294967295"), 4294967295) |
| 67 |
assert.strictEqual(parseUint32("4294967296"), 4294967296) |
| 68 |
assert.strictEqual(parseUint32("" + Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER) |
| 69 |
}) |
| 70 |
}) |
| 71 |
}) |