Use ARRAY
passing it no arguments:
import s from "./speco";
const arraySpec = s.ARRAY();
s.isValid(arraySpec, []);
// => true
s.isValid(arraySpec, ["a", 1, {}]);
// => true
s.isValid(arraySpec, [1, 2]);
// => true
s.isValid(arraySpec, 1);
// => false
s.isValid(arraySpec, "a");
// => false
s.isValid(arraySpec, {});
// => false
function SomeObject() {
this.a = 1;
}
s.isValid(arraySpec, new SomeObject());
// => false
s.isValid(arraySpec, () => {});
// => false
Use ARRAY_OF
passing it the spec that all its elements follow:
import s from "./speco";
const arraySpec = s.ARRAY_OF(s.STRING);
s.isValid(arraySpec, []);
// => true
s.isValid(arraySpec, ["a", "b"]);
// => true
s.isValid(arraySpec, ["a", 1, {}]);
// => false
s.isValid(arraySpec, [1, "l"]);
// => false
Use ARRAY_OF_DISTINCT
passing it the spec that all its elements follow:
import s from "./speco";
const arraySpec = s.ARRAY_OF_DISTINCT(s.STRING);
s.isValid(arraySpec, ["a"]);
// => true
s.isValid(arraySpec, ["a", "b"]);
// => true
s.isValid(arraySpec, ["a", "a"]);
// => false
s.isValid(arraySpec, ["a", 1, {}]);
// => false
Use ARRAY
passing it the specs for each of the elements of the array:
import s from "./speco";
const arraySpec = s.ARRAY(s.STRING, s.NUM);
s.isValid(arraySpec, ["a", 1]):
// => true
s.isValid(arraySpec, ["a", 1, {}]);
// => false
s.isValid(arraySpec, [3, 1]);
// => false
s.isValid(arraySpec, [1, 2, []]);
// => false