How to use the objectmodel.ArrayModel function in objectmodel

To help you get started, we’ve selected a few objectmodel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sylvainpolletvillard / ObjectModel / docs / examples / array-models2.js View on Github external
});

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);
github sylvainpolletvillard / ObjectModel / docs / examples / array-models.js View on Github external
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"]

objectmodel

Strong Dynamically Typed Object Modeling for JavaScript

MIT
Latest version published 11 months ago

Package Health Score

65 / 100
Full package analysis