diff --git a/lib/model.js b/lib/model.js index 00fba5a8..db03453d 100644 --- a/lib/model.js +++ b/lib/model.js @@ -254,6 +254,21 @@ Model.prototype.getTableName = function() { return this._getModel()._name; } +/* + * Return the schema for this Model + */ +Model.prototype.getSchema = function() { + return this._schema; +} + +/* + * Return the exported representation of the schema for this model + */ +Model.prototype.exportSchema = function() { + return this.getSchema().export(); +} + + Model.prototype.fetchIndexes = function() { var self = this; @@ -263,7 +278,7 @@ Model.prototype.fetchIndexes = function() { var r = self._thinky.r; var query = new Query(self); r.table(self.getTableName()).indexList().do(function(indexes) { - return r.table(self.getTableName()).indexWait(r.args(indexes)) + return r.table(self.getTableName()).indexWait(r.args(indexes)) }).run().then(function(indexes) { for(var i=0; i array.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must have at least "+this._min+" elements.") + throw new Errors.ValidationError("Value for "+prefix+" must have at least "+this._min+" elements.") } if ((this._max !== -1) && (this._max < array.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must have at most "+this._max+" elements.") + throw new Errors.ValidationError("Value for "+prefix+" must have at most "+this._max+" elements.") } if ((this._length !== -1) && (this._length !== array.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must be an array with "+this._length+" elements.") + throw new Errors.ValidationError("Value for "+prefix+" must be an array with "+this._length+" elements.") } for(var i=0; i jsDate)){ - throw new Errors.ValidationError("Value for "+prefix+" must be after "+this._min+".") + throw new Errors.ValidationError("Value for "+prefix+" must be after "+this._min+".") } if ((this._max instanceof Date) && (this._max < jsDate)){ - throw new Errors.ValidationError("Value for "+prefix+" must be before "+this._max+".") + throw new Errors.ValidationError("Value for "+prefix+" must be before "+this._max+".") } } } @@ -155,4 +155,7 @@ TypeDate.prototype._getDefaultFields = function(prefix, defaultFields, virtualFi } +TypeDate.prototype.export = 'date'; + + module.exports = TypeDate; diff --git a/lib/type/number.js b/lib/type/number.js index 9e308b03..d6158a58 100644 --- a/lib/type/number.js +++ b/lib/type/number.js @@ -104,7 +104,7 @@ TypeNumber.prototype.validate = function(number, prefix, options) { options = util.mergeOptions(this._options, options); if (util.validateIfUndefined(number, prefix, "number", options)) return; - + if ((typeof this._validator === "function") && (this._validator(number) === false)) { throw new Errors.ValidationError("Validator for the field "+prefix+" returned `false`."); } @@ -122,13 +122,13 @@ TypeNumber.prototype.validate = function(number, prefix, options) { } else { if ((this._min !== -1) && (this._min > number)){ - throw new Errors.ValidationError("Value for "+prefix+" must be greater than "+this._min+".") + throw new Errors.ValidationError("Value for "+prefix+" must be greater than "+this._min+".") } if ((this._max !== -1) && (this._max < number)){ - throw new Errors.ValidationError("Value for "+prefix+" must be less than "+this._max+".") + throw new Errors.ValidationError("Value for "+prefix+" must be less than "+this._max+".") } if ((this._integer === true) && (number%1 !== 0)){ - throw new Errors.ValidationError("Value for "+prefix+" must be an integer.") + throw new Errors.ValidationError("Value for "+prefix+" must be an integer.") } } } @@ -144,4 +144,7 @@ TypeNumber.prototype._getDefaultFields = function(prefix, defaultFields, virtual } +TypeNumber.prototype.export = 'number'; + + module.exports = TypeNumber; diff --git a/lib/type/object.js b/lib/type/object.js index b55beb39..6b918414 100644 --- a/lib/type/object.js +++ b/lib/type/object.js @@ -177,5 +177,20 @@ TypeObject.prototype._getDefaultFields = function(prefix, defaultFields, virtual } } +TypeObject.prototype.export = function() { + if (Object.keys(this._schema).length > 0) { + var exported = {}; + util.loopKeys(this._schema, function(_schema, key) { + var type = _schema[key]; + if (type.export != null) { + exported[key] = util.result(type.export, type); + } + }); + return exported; + } else { + return 'object'; + } +} + module.exports = TypeObject; diff --git a/lib/type/point.js b/lib/type/point.js index aabab3ed..b3253545 100644 --- a/lib/type/point.js +++ b/lib/type/point.js @@ -136,4 +136,7 @@ TypePoint.prototype._getDefaultFields = function(prefix, defaultFields, virtualF } } + +TypePoint.prototype.export = 'point'; + module.exports = TypePoint; diff --git a/lib/type/string.js b/lib/type/string.js index b4616add..e3e38a8e 100644 --- a/lib/type/string.js +++ b/lib/type/string.js @@ -255,7 +255,7 @@ TypeString.prototype.default = function(fnOrValue) { /** * Set a custom validator that will be called with the string. The validator * should return a boolean whether the field is valid or not. - * @param {function} fn + * @param {function} fn * @return {TypeString} */ TypeString.prototype.validator = function(fn) { @@ -269,7 +269,7 @@ TypeString.prototype.validator = function(fn) { /** * Set the valid values for this field. The arguments must be strings * or an array of strings. - * @param {...string|Array.} fn + * @param {...string|Array.} fn * @return {TypeString} */ TypeString.prototype.enum = function() { @@ -320,28 +320,28 @@ TypeString.prototype.validate = function(str, prefix, options) { } else { if ((this._min !== -1) && (this._min > str.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must be longer than "+this._min+".") + throw new Errors.ValidationError("Value for "+prefix+" must be longer than "+this._min+".") } if ((this._max !== -1) && (this._max < str.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must be shorter than "+this._max+".") + throw new Errors.ValidationError("Value for "+prefix+" must be shorter than "+this._max+".") } if ((this._length !== -1) && (this._length !== str.length)){ - throw new Errors.ValidationError("Value for "+prefix+" must be a string with "+this._length+" characters.") + throw new Errors.ValidationError("Value for "+prefix+" must be a string with "+this._length+" characters.") } if ((this._regex instanceof RegExp) && (this._regex.test(str) === false)) { - throw new Errors.ValidationError("Value for "+prefix+" must match the regex.") + throw new Errors.ValidationError("Value for "+prefix+" must match the regex.") } if ((this._alphanum === true) && (validator.isAlphanumeric(str) === false)) { - throw new Errors.ValidationError("Value for "+prefix+" must be an alphanumeric string.") + throw new Errors.ValidationError("Value for "+prefix+" must be an alphanumeric string.") } if ((this._email === true) && (validator.isEmail(str) === false)) { - throw new Errors.ValidationError("Value for "+prefix+" must be a valid email.") + throw new Errors.ValidationError("Value for "+prefix+" must be a valid email.") } if ((this._lowercase === true) && (validator.isLowercase(str) === false)) { - throw new Errors.ValidationError("Value for "+prefix+" must be a lowercase string.") + throw new Errors.ValidationError("Value for "+prefix+" must be a lowercase string.") } if ((this._uppercase === true) && (validator.isUppercase(str) === false)) { - throw new Errors.ValidationError("Value for "+prefix+" must be a uppercase string.") + throw new Errors.ValidationError("Value for "+prefix+" must be a uppercase string.") } if ((this._enum !== undefined) && (this._enum[str] !== true)) { var validValues = Object.keys(this._enum); @@ -386,5 +386,7 @@ TypeString.prototype._getDefaultFields = function(prefix, defaultFields, virtual return this; } +TypeString.prototype.export = 'string'; + module.exports = TypeString; diff --git a/lib/util.js b/lib/util.js index 6c64e69e..846e5bd9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -76,7 +76,7 @@ util.tryCatch = tryCatch; * - doc {Document} the document that triggered the hooks * - fn {Function} the main function * - fnArgs {Array} arguments for `fn` - * @return {Promise=} + * @return {Promise=} */ function hook(options) { var preHooks = options.preHooks; @@ -331,4 +331,13 @@ function toArray(args) { } util.toArray = toArray; +function result(obj, receiver) { + if (typeof obj === 'function') { + return obj.call(receiver); + } else { + return obj; + } +} +util.result = result; + module.exports = util; diff --git a/test/schema.js b/test/schema.js index 84cacf57..0f35e67a 100644 --- a/test/schema.js +++ b/test/schema.js @@ -163,7 +163,7 @@ describe('Chainable types', function(){ // These tests are mostly for new validators like `min`/`max`/`alphanum`/etc. it('General - chainable types in nested schemas', function(){ var name = util.s8(); - var Model = thinky.createModel(name, + var Model = thinky.createModel(name, {id: type.string(), objectArray: [{ myAttribute: thinky.type.object() }]}) var doc = new Model({ id: util.s8(), objectArray : {} }) @@ -268,7 +268,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'a'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be longer than 2."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be longer than 2."); }); }); it('String - min - good', function(){ @@ -288,7 +288,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'abcdefgh'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be shorter than 5."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be shorter than 5."); }); }); it('String - max - good', function(){ @@ -308,7 +308,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'abcdef'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a string with 5 characters."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a string with 5 characters."); }); }); it('String - length - too short', function(){ @@ -320,7 +320,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'abc'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a string with 5 characters."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a string with 5 characters."); }); }); it('String - length - good', function(){ @@ -340,7 +340,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'bar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must match the regex."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must match the regex."); }); }); it('String - regex - good', function(){ @@ -360,7 +360,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'bar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must match the regex."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must match the regex."); }); }); it('String - regex with flags - good', function(){ @@ -382,7 +382,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'élégant'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be an alphanumeric string."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be an alphanumeric string."); }); }); it('String - alphanum - match', function(){ @@ -402,7 +402,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'fooATbar.com'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a valid email."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a valid email."); }); }); it('String - email - match', function(){ @@ -422,7 +422,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'fooBar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a lowercase string."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a lowercase string."); }); }); it('String - lowercase - match', function(){ @@ -442,7 +442,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'fooBar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a uppercase string."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be a uppercase string."); }); }); it('String - uppercase - match', function(){ @@ -462,7 +462,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'fooBar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); + return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); }); }); it('String - validator - return true', function(){ @@ -482,7 +482,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'fooBar'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Not good"); + return (error instanceof Errors.ValidationError) && (error.message === "Not good"); }); }); it('String - enum - unknown value - 1', function(){ @@ -494,7 +494,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'buzz'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo, bar."); + return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo, bar."); }); }); it('String - enum - unknown value - 2', function(){ @@ -506,7 +506,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'buzz'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo, bar."); + return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo, bar."); }); }); it('String - enum - unknown value - 3', function(){ @@ -518,7 +518,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'buzz'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo."); + return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo."); }); }); it('String - enum - unknown value - 4', function(){ @@ -530,7 +530,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 'buzz'}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo."); + return (error instanceof Errors.ValidationError) && (error.message === "The field [id] must be one of these values: foo."); }); }); @@ -639,7 +639,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 1}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be greater than 2."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be greater than 2."); }); }); it('Number - min - good', function(){ @@ -659,7 +659,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 8}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be less than 5."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be less than 5."); }); }); it('Number - max - good', function(){ @@ -679,7 +679,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 3.14}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be an integer."); + return (error instanceof Errors.ValidationError) && (error.message === "Value for [id] must be an integer."); }); }); it('Number - integer - good', function(){ @@ -699,7 +699,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 2}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); + return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); }); }); it('Number - validator - return true', function(){ @@ -719,7 +719,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: 4}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Not good"); + return (error instanceof Errors.ValidationError) && (error.message === "Not good"); }); }); @@ -751,7 +751,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: true}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); + return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); }); }); it('Boolean - validator - return true', function(){ @@ -771,7 +771,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: true}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Not good"); + return (error instanceof Errors.ValidationError) && (error.message === "Not good"); }); }); @@ -817,7 +817,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: valueDate}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && ((error.message.match("Value for .id. must be after")) !== null); + return (error instanceof Errors.ValidationError) && ((error.message.match("Value for .id. must be after")) !== null); }); }); it('Date - min - good', function(){ @@ -847,7 +847,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: valueDate}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && ((error.message.match("Value for .id. must be before")) !== null); + return (error instanceof Errors.ValidationError) && ((error.message.match("Value for .id. must be before")) !== null); }); }); it('Date - max - good', function(){ @@ -872,7 +872,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: new Date()}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); + return (error instanceof Errors.ValidationError) && (error.message === "Validator for the field [id] returned `false`."); }); }); it('Date - validator - return true', function(){ @@ -892,7 +892,7 @@ describe('Chainable types', function(){ var doc = new Model({ id: new Date()}); doc.validate(); }, function(error) { - return (error instanceof Errors.ValidationError) && (error.message === "Not good"); + return (error instanceof Errors.ValidationError) && (error.message === "Not good"); }); }); @@ -1377,7 +1377,7 @@ describe('generateDefault', function(){ schema: { field: {_type: Number, default: defaultValue} }, - default: defaultArray + default: defaultArray } }, {init: false}) @@ -1400,11 +1400,11 @@ describe('generateDefault', function(){ _type: Array, schema: { field: { - _type: Object, + _type: Object, schema: {value: {_type: Number, default: defaultValue} } } }, - default: defaultArray + default: defaultArray } }, {init: false}) @@ -1432,7 +1432,7 @@ describe('generateDefault', function(){ field: {_type: Number, default: defaultValue} } }, - default: defaultArray + default: defaultArray } }, {init: false}) @@ -1771,7 +1771,7 @@ describe('validate', function(){ doc = new Model({ id: str, - field: null + field: null }) assert.throws(function() { @@ -1791,7 +1791,7 @@ describe('validate', function(){ doc = new Model({ id: str, - field: null + field: null }) doc.validate(); @@ -1838,7 +1838,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Number + field: Number }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -1895,7 +1895,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Boolean + field: Boolean }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -1932,7 +1932,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -1948,7 +1948,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -1988,7 +1988,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2020,7 +2020,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2036,7 +2036,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2055,7 +2055,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2074,7 +2074,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false}) doc = new Model({ @@ -2089,7 +2089,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_missing: true}) doc = new Model({ @@ -2107,7 +2107,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: Date + field: Date }, {init: false, enforce_missing: false}) doc = new Model({ @@ -2276,7 +2276,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_missing: true}) doc = new Model({ @@ -2295,7 +2295,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_missing: true}) doc = new Model({ @@ -2314,7 +2314,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_missing: false}) doc = new Model({ @@ -2329,7 +2329,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2349,7 +2349,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2369,7 +2369,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'none'}) doc = new Model({ @@ -2384,7 +2384,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2404,7 +2404,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2424,7 +2424,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'none'}) doc = new Model({ @@ -2440,7 +2440,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2460,7 +2460,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'strict'}) doc = new Model({ @@ -2480,7 +2480,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2502,7 +2502,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, - field: [Number] + field: [Number] }, {init: false, enforce_type: 'loose'}) doc = new Model({ @@ -2880,7 +2880,7 @@ describe('validate', function(){ bar: "keep" }) doc.validate(); - + assert.equal(false, doc.foo.hasOwnProperty('buzz')); assert.deepEqual(doc, { id: str, @@ -2895,7 +2895,7 @@ describe('validate', function(){ var Model = thinky.createModel(name, { id: String, foo: type.object().schema({ - fizz: type.string() + fizz: type.string() }).removeExtra() }, {init: false}) @@ -2938,7 +2938,7 @@ describe('validate', function(){ doc.validateAll(); assert.deepEqual(doc, {id: value.id, otherDoc: {id: otherDoc.id, otherId: otherDoc.otherId}}); - + }); it('Test option validate="oncreate"', function(){ @@ -3681,3 +3681,32 @@ describe('_validator', function(){ doc.validate(); }); }); + +describe('export', function() { + it('should export the schema', function() { + var Model = thinky.createModel(util.s8(), { + any: thinky.type.any(), + array: [ thinky.type.string() ], + boolean: thinky.type.boolean(), + buffer: thinky.type.buffer(), + date: thinky.type.date(), + number: thinky.type.number(), + object: thinky.type.object(), + point: thinky.type.point(), + string: thinky.type.string(), + virtual: thinky.type.virtual() + }, {init: false}); + var exported = Model.getSchema().export(); + assert.deepEqual(exported, { + any: 'any', + array: '[string]', + boolean: 'boolean', + buffer: 'buffer', + date: 'date', + number: 'number', + object: 'object', + point: 'point', + string: 'string' + }); + }); +})