2018-05-22 21:50:24 +02:00
|
|
|
zrequire('schema');
|
|
|
|
|
|
|
|
run_test('basics', () => {
|
|
|
|
assert.equal(schema.check_string('x', 'fred'), undefined);
|
2018-12-07 22:14:28 +01:00
|
|
|
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-02 01:41:40 +02:00
|
|
|
const check_rec = (val) => schema.check_record('my_rec', val, fields);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_rec({foo: 'apple', bar: 'banana'}),
|
2020-07-02 02:16:03 +02:00
|
|
|
undefined,
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_rec('bogus'),
|
2020-07-02 02:16:03 +02:00
|
|
|
'my_rec is not a record',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_rec({foo: 'apple'}),
|
2020-07-02 02:16:03 +02:00
|
|
|
'in my_rec bar is missing',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_rec({}),
|
2020-07-02 02:16:03 +02:00
|
|
|
'in my_rec bar is missing, foo is missing',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_rec({foo: 'apple', bar: 42}),
|
2020-07-02 02:16:03 +02:00
|
|
|
'in my_rec bar is not a string',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
const check_array = (val) => schema.check_array('lst', val, schema.check_string);
|
2018-05-22 21:50:24 +02:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_array(['foo', 'bar']),
|
2020-07-02 02:16:03 +02:00
|
|
|
undefined,
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_array('foo'),
|
2020-07-02 02:16:03 +02:00
|
|
|
'lst is not an array',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
check_array(['foo', 3]),
|
2020-07-02 02:16:03 +02:00
|
|
|
'in lst we found an item where item is not a string',
|
2018-05-22 21:50:24 +02:00
|
|
|
);
|
|
|
|
});
|