Skip to content

Commit

Permalink
it's now possible to nest a sub-schema direct within the parent as `s…
Browse files Browse the repository at this point in the history
…chema: { ... } | [ ... ]`; Optimized tests for 100% codeverage
  • Loading branch information
M. Peter committed Feb 8, 2017
1 parent 0d5a3b0 commit a43c28a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ module.exports = (grunt) ->

# build the project
grunt.registerTask "build", [ "clear", "coffee:base", "includereplace" ]
grunt.registerTask "release", [ "clear", "coffee:base", "test" ]
grunt.registerTask "release", [ "build", "test" ]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ var uservalidator = new Schema( {
|Version|Date|Description|
|:--:|:--:|:--|
|1.5.0|2017-02-08|it's now possible to nest a sub-schema direct within the parent as `schema: { ... } | [ ... ]`; Optimized tests for 100% codeverage|
|1.4.0|2016-11-14|validate the basic input object for a object/array|
|1.3.0|2016-11-11|added option `customerror` to be able to create your own error objects|
|1.2.3|2016-10-07|Optimized sub-schema data type validation to check for object/array; use coveralls directly with coffee|
Expand Down
38 changes: 30 additions & 8 deletions _src/lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ _isObject = require( "lodash/isObject" )
_isString = require( "lodash/isString" )
_isObject = require( "lodash/isObject" )
_isRegExp = require( "lodash/isRegExp" )
_assignIn = require( "lodash/assignIn" )
_template = require( "lodash/template" )

moment = require( "moment-timezone" )
Expand All @@ -35,16 +36,38 @@ module.exports = class ObjSchema
defaults: ->
name: "data"

constructor: ( @schema, options )->
constructor: ( schema, options={} )->

@isArray = _isArray( @schema )
@isArray = _isArray( schema )
@config = @defaults()
@config.name = options.name if options.name?
if options.name?
@config.name = options.name
else
options.name = @config.name

@schema = @prepareSchema( schema, options )


@config.customerror = options.customerror if options.customerror? and _isFunction( options.customerror )

@_initMsgs()
return


prepareSchema: ( schema, options )=>
if @isArray
for def, idx in schema
switch def.type
when "schema"
if def.schema not instanceof ObjSchema
schema[ idx ].schema = new ObjSchema( def.schema, _assignIn( {}, options, { name: options.name + "-" + idx } ) )
else
for _k, def of schema
switch def.type
when "schema"
if def.schema not instanceof ObjSchema
schema[ _k ].schema = new ObjSchema( def.schema, _assignIn( {}, options, { name: options.name + "-" + _k } ) )
return schema

keys: =>
return Object.keys( @schema )

Expand All @@ -53,8 +76,9 @@ module.exports = class ObjSchema
validateCb: ( [data, options]..., cb )=>
_err = @validate( data )
if not _err?
cb( null )
return

if not cb?
throw _err

Expand All @@ -75,7 +99,7 @@ module.exports = class ObjSchema
[ err, _val ] = @_validateKey( idx, data[ idx ], def, data, options )
errors.push( err ) if err
else
if not _isObject( data )
if not _isObject( data ) or _isArray( data )
return [ @_error( "object", null ) ]

for _k, def of @schema
Expand Down Expand Up @@ -108,8 +132,6 @@ module.exports = class ObjSchema
validateKey: ( key, val, options )=>
if not @schema[ key ]
return null
if @isArray
def.idx = key
[ err, _val ] = @_validateKey( key, val, @schema[ key ], null, options )
if err?
return err
Expand Down
Loading

0 comments on commit a43c28a

Please sign in to comment.