|
21 | 21 | // MODULES // |
22 | 22 |
|
23 | 23 | var objectKeys = require( '@stdlib/utils/keys' ); |
24 | | -var isObject = require( '@stdlib/assert/is-plain-object' ); |
| 24 | +var isPlainObject = require( '@stdlib/assert/is-plain-object' ); |
25 | 25 | var hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
26 | | -var isArray = require( '@stdlib/assert/is-array' ); |
27 | | -var isString = require( '@stdlib/assert/is-string' ).isPrimitive; |
28 | | -var join = require( '@stdlib/array/base/join' ); |
| 26 | +var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; |
| 27 | +var isEmptyArray = require( '@stdlib/assert/is-empty-array' ); |
29 | 28 | var format = require( '@stdlib/string/format' ); |
30 | 29 |
|
31 | 30 |
|
32 | 31 | // FUNCTIONS // |
33 | 32 |
|
34 | 33 | /** |
35 | | -* Validates a section schema configuration. |
| 34 | +* Validates a schema object. |
36 | 35 | * |
37 | 36 | * @private |
38 | | -* @param {Object} schema - schema section to validate |
39 | | -* @param {string} section - section name |
| 37 | +* @param {Object} schema - schema object |
40 | 38 | * @returns {(Error|null)} null or an error object |
41 | 39 | */ |
42 | | -function validateSection( schema, section ) { |
| 40 | +function validateSchema( schema ) { |
| 41 | + var section; |
| 42 | + var keys; |
| 43 | + var obj; |
43 | 44 | var i; |
44 | | - if ( !isObject( schema ) ) { |
45 | | - return new TypeError( format( 'invalid option. `%s` schema section must be an object. Value: `%s`.', section, schema ) ); |
46 | | - } |
47 | | - if ( hasOwnProp( schema, 'required' ) ) { |
48 | | - if ( !isArray( schema.required ) ) { |
49 | | - return new TypeError( format( 'invalid option. `%s` must be an array. Value: `%s`.', section+'.required', schema.required ) ); |
| 45 | + |
| 46 | + keys = objectKeys( schema ); |
| 47 | + for ( i = 0; i < keys.length; i++ ) { |
| 48 | + section = keys[ i ]; |
| 49 | + obj = schema[ section ]; |
| 50 | + if ( !isPlainObject( obj ) ) { |
| 51 | + return new TypeError( format( 'invalid option. `%s` option must be an object containing properties having values which are objects. Option: `%s`.', 'schema', JSON.stringify( obj ) ) ); |
50 | 52 | } |
51 | | - for ( i = 0; i < schema.required.length; i++ ) { |
52 | | - if ( !isString( schema.required[ i ] ) ) { |
53 | | - return new TypeError( format( 'invalid option. `%s` must only contain strings. Index: `%u`. Value: `%s`.', section+'.required', i, schema.required[ i ] ) ); |
| 53 | + if ( hasOwnProp( obj, 'required' ) ) { |
| 54 | + if ( !isStringArray( obj.required ) && !isEmptyArray( obj.required ) ) { |
| 55 | + return new TypeError( format( 'invalid option. `%s` option must be an object having %s `%s` property which is an array of strings. Option: `%s`.', 'schema', 'a', section+'.required', JSON.stringify( obj ) ) ); |
54 | 56 | } |
| 57 | + } else { |
| 58 | + return new TypeError( format( 'invalid option. `%s` option must be an object having %s `%s` property which is an array of strings. Option: `%s`.', 'schema', 'a', section+'.required', JSON.stringify( obj ) ) ); |
55 | 59 | } |
56 | | - } else { |
57 | | - return new TypeError( format( 'invalid option. `%s` schema section must have %s `%s` property.', section, 'a', 'required' ) ); |
58 | | - } |
59 | | - if ( hasOwnProp( schema, 'optional' ) ) { |
60 | | - if ( !isArray( schema.optional ) ) { |
61 | | - return new TypeError( format( 'invalid option. `%s` must be an array. Value: `%s`.', section+'.optional', schema.optional ) ); |
62 | | - } |
63 | | - for ( i = 0; i < schema.optional.length; i++ ) { |
64 | | - if ( !isString( schema.optional[ i ] ) ) { |
65 | | - return new TypeError( format( 'invalid option. `%s` must only contain strings. Index: `%u`. Value: `%s`.', section+'.optional', i, schema.optional[ i ] ) ); |
| 60 | + if ( hasOwnProp( obj, 'optional' ) ) { |
| 61 | + if ( !isStringArray( obj.optional ) && !isEmptyArray( obj.optional ) ) { |
| 62 | + return new TypeError( format( 'invalid option. `%s` option must be an object having %s `%s` property which is an array of strings. Option: `%s`.', 'schema', 'a', section+'.optional', JSON.stringify( obj ) ) ); |
66 | 63 | } |
| 64 | + } else { |
| 65 | + return new TypeError( format( 'invalid option. `%s` option must be an object having %s `%s` property which is an array of strings. Option: `%s`.', 'schema', 'a', section+'.optional', JSON.stringify( obj ) ) ); |
67 | 66 | } |
68 | | - } else { |
69 | | - return new TypeError( format( 'invalid option. `%s` schema section must have %s `%s` property.', section, 'an', 'optional' ) ); |
70 | 67 | } |
71 | 68 | return null; |
72 | 69 | } |
@@ -99,29 +96,21 @@ function validateSection( schema, section ) { |
99 | 96 | * } |
100 | 97 | */ |
101 | 98 | function validate( opts, options ) { |
102 | | - var section; |
103 | | - var keys; |
104 | 99 | var err; |
105 | | - var i; |
106 | | - |
107 | | - if ( !isObject( options ) ) { |
| 100 | + if ( !isPlainObject( options ) ) { |
108 | 101 | return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) ); |
109 | 102 | } |
110 | 103 | if ( hasOwnProp( options, 'schema' ) ) { |
111 | | - if ( !isObject( options.schema ) ) { |
| 104 | + if ( !isPlainObject( options.schema ) ) { |
112 | 105 | return new TypeError( format( 'invalid option. `%s` option must be an object. Option: `%s`.', 'schema', options.schema ) ); |
113 | 106 | } |
114 | 107 | if ( !hasOwnProp( options.schema, 'root' ) ) { |
115 | 108 | return new TypeError( format( 'invalid option. `%s` option must have %s `%s` property.', 'schema', 'a', 'root' ) ); |
116 | 109 | } |
117 | | - keys = objectKeys( options.schema ); |
118 | | - for ( i = 0; i < keys.length; i++ ) { |
119 | | - section = keys[ i ]; |
120 | | - err = validateSection( options.schema[ section ], section ); |
121 | | - if ( err ) { |
122 | | - return err; |
123 | | - } |
124 | | - } |
| 110 | + err = validateSchema( options.schema ); |
| 111 | + if ( err ) { |
| 112 | + return err; |
| 113 | + } |
125 | 114 | opts.schema = options.schema; |
126 | 115 | } |
127 | 116 | return null; |
|
0 commit comments