Skip to content

Commit 7cbbfeb

Browse files
authored
refactor: consolidate error messages
Signed-off-by: Athan <[email protected]>
1 parent 7ab18e6 commit 7cbbfeb

File tree

1 file changed

+32
-43
lines changed
  • lib/node_modules/@stdlib/_tools/remark/plugins/remark-lint-expected-html-sections/lib

1 file changed

+32
-43
lines changed

lib/node_modules/@stdlib/_tools/remark/plugins/remark-lint-expected-html-sections/lib/validate.js

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,49 @@
2121
// MODULES //
2222

2323
var objectKeys = require( '@stdlib/utils/keys' );
24-
var isObject = require( '@stdlib/assert/is-plain-object' );
24+
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2525
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' );
2928
var format = require( '@stdlib/string/format' );
3029

3130

3231
// FUNCTIONS //
3332

3433
/**
35-
* Validates a section schema configuration.
34+
* Validates a schema object.
3635
*
3736
* @private
38-
* @param {Object} schema - schema section to validate
39-
* @param {string} section - section name
37+
* @param {Object} schema - schema object
4038
* @returns {(Error|null)} null or an error object
4139
*/
42-
function validateSection( schema, section ) {
40+
function validateSchema( schema ) {
41+
var section;
42+
var keys;
43+
var obj;
4344
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 ) ) );
5052
}
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 ) ) );
5456
}
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 ) ) );
5559
}
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 ) ) );
6663
}
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 ) ) );
6766
}
68-
} else {
69-
return new TypeError( format( 'invalid option. `%s` schema section must have %s `%s` property.', section, 'an', 'optional' ) );
7067
}
7168
return null;
7269
}
@@ -99,29 +96,21 @@ function validateSection( schema, section ) {
9996
* }
10097
*/
10198
function validate( opts, options ) {
102-
var section;
103-
var keys;
10499
var err;
105-
var i;
106-
107-
if ( !isObject( options ) ) {
100+
if ( !isPlainObject( options ) ) {
108101
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
109102
}
110103
if ( hasOwnProp( options, 'schema' ) ) {
111-
if ( !isObject( options.schema ) ) {
104+
if ( !isPlainObject( options.schema ) ) {
112105
return new TypeError( format( 'invalid option. `%s` option must be an object. Option: `%s`.', 'schema', options.schema ) );
113106
}
114107
if ( !hasOwnProp( options.schema, 'root' ) ) {
115108
return new TypeError( format( 'invalid option. `%s` option must have %s `%s` property.', 'schema', 'a', 'root' ) );
116109
}
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+
}
125114
opts.schema = options.schema;
126115
}
127116
return null;

0 commit comments

Comments
 (0)