欲しかった機能が実装できたのでタグを切りました。
hagino3000/Struct.js
https://github.com/hagino3000/Struct.js
https://github.com/hagino3000/Struct.js
主な機能追加
- ネストした構造のサポート
- nullableオプションの追加
- カスタムバリデーション関数のサポート
- 設定で全チェックを無効化する機能を追加
- 初期作成時の値チェックを追加
リリース用の設定
Proxyオブジェクトを生成しないので、通常のプロパティアクセスのコストだけになる。Struct.configure({
// 全てのチェック無効化
'disable any check': true
});
バリデーション関数のサポート
Struct.define('Size', {
width: {type: 'number', cond: 'v >= 0'},
height: {type: 'number', cond: function(v){return v >= 0} /*same as ↑*/}
});
var size = Struct.create('Size', {
width: 0,
height: 0
});
ネスト構造のサポート
Struct.create にネストしたObjectをそのまま食わせても大丈夫。Struct.define('Position', {
x: {type: 'number', nullable: false},
y: {type: 'number', nullable: false}
});
Struct.define('Size', {
width: {type: 'number', nullable: false, cond: 'v >= 0'},
height: {type: 'number', nullable: false, cond: 'v >= 0'}
});
Struct.define('Rect', {
pos: {type: 'struct:Position', nullable: false},
size: {type: 'struct:Size', nullable: false},
createdAt: {type: 'date', writable: false}
});
var rect = Struct.create('Rect', {
pos: {x: 0, y: 0},
size: {width: 100, height: 100},
createdAt: new Date()
});
rect.pos.x = "200"; // => type mismatch error
rect.size.height = -1; // => condition formula validation error