2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-10 04:53:22 +01:00
|
|
|
const schema = zrequire("schema");
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
|
|
|
assert.equal(schema.check_string("x", "fred"), undefined);
|
|
|
|
assert.equal(schema.check_string("x", [1, 2]), "x is not a string");
|
2018-05-22 21:50:24 +02:00
|
|
|
|
|
|
|
const fields = {
|
|
|
|
foo: schema.check_string,
|
|
|
|
bar: schema.check_string,
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const check_rec = (val) => schema.check_record("my_rec", val, fields);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(check_rec({foo: "apple", bar: "banana"}), undefined);
|
|
|
|
|
|
|
|
assert.equal(check_rec("bogus"), "my_rec is not a record");
|
|
|
|
|
|
|
|
assert.equal(check_rec({foo: "apple"}), "in my_rec bar is missing");
|
|
|
|
|
|
|
|
assert.equal(check_rec({}), "in my_rec bar is missing, foo is missing");
|
|
|
|
|
|
|
|
assert.equal(check_rec({foo: "apple", bar: 42}), "in my_rec bar is not a string");
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const check_array = (val) => schema.check_array("lst", val, schema.check_string);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(check_array(["foo", "bar"]), undefined);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(check_array("foo"), "lst is not an array");
|
2018-05-22 21:50:24 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(check_array(["foo", 3]), "in lst we found an item where item is not a string");
|
2018-05-22 21:50:24 +02:00
|
|
|
});
|