Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allowing schemas to be passed uninitialized schemas #12

Merged
merged 1 commit into from

2 participants

@tomgco

This allows:

var blogSchema = schemata({
    comments:
      { type: schemata.Array(createCommentSchema)
    }
  })
  return blogSchema

Instead of:

  var blogSchema = schemata({
    comments:
      { type: schemata.Array(createCommentSchema())
    }
  })
  return blogSchema
@serby
Owner

Is this backwards compatible?

@serby
Owner

Looks like it is, so it will be released as v0.1.0

@tomgco

Yes it is

@serby serby merged commit 2cebca0 into serby:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 13, 2013
  1. @tomgco
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 1 deletion.
  1. +1 −1  lib/schemata.js
  2. +41 −0 test/schemata.test.js
View
2  lib/schemata.js
@@ -11,7 +11,7 @@ var async = require('async')
*/
function ArrayType(schema) {
return {
- arraySchema: schema
+ arraySchema: typeof schema === 'function' ? schema() : schema
};
}
View
41 test/schemata.test.js
@@ -54,6 +54,26 @@ function createBlogSchema() {
return blogSchema
}
+function createBlogSchemaWithSubSchemaNotInitialised() {
+
+ var blogSchema = schemata({
+ title: {
+ tag: ['auto']
+ },
+ body: {
+ tag: ['auto']
+ },
+ author: {
+ type: createContactSchema()
+ },
+ comments:
+ { type: schemata.Array(createCommentSchema)
+ , tag: ['auto']
+ }
+ })
+ return blogSchema
+}
+
function createArraySchema() {
var schema = schemata({
images: {
@@ -586,6 +606,27 @@ describe('schemata', function() {
})
})
+
+ it('should cause an error if a subSchema is passed un-invoked', function (done) {
+ var schema = createBlogSchemaWithSubSchemaNotInitialised()
+ , model = schema.makeBlank()
+ , testValues = [undefined, null, '', 0, []]
+ , subSchema = schema.schema.comments.type.arraySchema.schema
+
+ subSchema.email.validators = {
+ all: [validity.required]
+ }
+
+ async.forEach(testValues, function (value, next) {
+ model.comments = value
+
+ schema.validate(model, function (error, errors) {
+ errors.should.eql({})
+ next()
+ })
+ }, done)
+ })
+
it('does not try and validate array sub-schemas that are falsy or []', function (done) {
var schema = createBlogSchema()
, model = schema.makeBlank()
Something went wrong with that request. Please try again.