Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
});
const Mother = Person.extend({
female: true,
child: Person
});
const Father = Person.extend({
female: false,
child: Person
});
const Family = ObjectModel({
father: Father,
mother: Mother,
children: ArrayModel(Person), // array of Persons
grandparents: [ArrayModel([Mother, Father])]
// optional array of Mothers or Fathers
});
let joe = new Person({ name: "Joe", female: false });
let ann = new Person({ name: "Ann", female: true });
let joanna = new Person({ name: "Joanna", female: true });
const joefamily = new Family({
father: joe,
mother: ann,
children: [joanna, "dog"]
});
// TypeError: expecting Array[1] to be { name: String, female: Boolean }, got String "dog"
console.log(joefamily);
import { ArrayModel } from "objectmodel";
const Cards = new ArrayModel([Number, "J", "Q", "K"]);
// Hand is an array of 2 Numbers, J, Q, or K
const Hand = Cards.extend().assert(
a => a.length === 2,
"should have two cards"
);
const myHand = Hand([7, "K"]);
myHand[0] = "Joker";
// TypeError: expecting Array[0] to be Number or "J" or "Q" or "K", got String "Joker"
myHand.push("K");
// TypeError: assertion "should have two cards" returned false for value[7, "Joker", "K"]