Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
do not validate files and ignores twice
Browse files Browse the repository at this point in the history
  • Loading branch information
fasttime committed Aug 5, 2023
1 parent 2bac413 commit c61908e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 67 deletions.
74 changes: 9 additions & 65 deletions src/base-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,13 @@
// Helpers
//------------------------------------------------------------------------------

/**
* Assets that a given value is an array.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array.
*/
function assertIsArray(value) {
if (!Array.isArray(value)) {
throw new TypeError('Expected value to be an array.');
}
}

/**
* Assets that a given value is an array containing only strings and functions.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array of strings and functions.
*/
function assertIsArrayOfStringsAndFunctions(value, name) {
assertIsArray(value, name);

if (value.some(item => typeof item !== 'string' && typeof item !== 'function')) {
throw new TypeError('Expected array to only contain strings and functions.');
}
}

/**
* Assets that a given value is a non-empty array.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array or an empty array.
*/
function assertIsNonEmptyArray(value) {
if (!Array.isArray(value) || value.length === 0) {
throw new TypeError('Expected value to be a non-empty array.');
}
}
const NOOP_STRATEGY = {
required: false,
merge() {
return undefined;
},
validate() { }
};

//------------------------------------------------------------------------------
// Exports
Expand All @@ -65,32 +35,6 @@ export const baseSchema = Object.freeze({
}
}
},
files: {
required: false,
merge() {
return undefined;
},
validate(value) {

// first check if it's an array
assertIsNonEmptyArray(value);

// then check each member
value.forEach(item => {
if (Array.isArray(item)) {
assertIsArrayOfStringsAndFunctions(item);
} else if (typeof item !== 'string' && typeof item !== 'function') {
throw new TypeError('Items must be a string, a function, or an array of strings and functions.');
}
});

}
},
ignores: {
required: false,
merge() {
return undefined;
},
validate: assertIsArrayOfStringsAndFunctions
}
files: NOOP_STRATEGY,
ignores: NOOP_STRATEGY
});
5 changes: 3 additions & 2 deletions src/config-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import createDebug from 'debug';

import { ObjectSchema } from '@humanwhocodes/object-schema';
import { baseSchema } from './base-schema.js';
import { filesAndIgnoresSchema } from './files-and-ignores-schema.js';

//------------------------------------------------------------------------------
// Helpers
Expand All @@ -30,7 +31,7 @@ const MINIMATCH_OPTIONS = {

const CONFIG_TYPES = new Set(['array', 'function']);

const BASE_SCHEMA = new ObjectSchema(baseSchema);
const FILES_AND_IGNORES_SCHEMA = new ObjectSchema(filesAndIgnoresSchema);

/**
* Shorthand for checking if a value is a string.
Expand All @@ -55,7 +56,7 @@ function assertValidFilesAndIgnores({ files, ignores }) {
if (ignores !== undefined) {
validateConfig.ignores = ignores;
}
BASE_SCHEMA.validate(validateConfig);
FILES_AND_IGNORES_SCHEMA.validate(validateConfig);
}

/**
Expand Down
85 changes: 85 additions & 0 deletions src/files-and-ignores-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @fileoverview ConfigSchema
* @author Nicholas C. Zakas
*/

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
* Asserts that a given value is an array.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array.
*/
function assertIsArray(value) {
if (!Array.isArray(value)) {
throw new TypeError('Expected value to be an array.');
}
}

/**
* Asserts that a given value is an array containing only strings and functions.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array of strings and functions.
*/
function assertIsArrayOfStringsAndFunctions(value, name) {
assertIsArray(value, name);

if (value.some(item => typeof item !== 'string' && typeof item !== 'function')) {
throw new TypeError('Expected array to only contain strings and functions.');
}
}

/**
* Asserts that a given value is a non-empty array.
* @param {*} value The value to check.
* @returns {void}
* @throws {TypeError} When the value is not an array or an empty array.
*/
function assertIsNonEmptyArray(value) {
if (!Array.isArray(value) || value.length === 0) {
throw new TypeError('Expected value to be a non-empty array.');
}
}

//------------------------------------------------------------------------------
// Exports
//------------------------------------------------------------------------------

/**
* The schema for `files` and `ignores` that every ConfigArray uses.
* @type Object
*/
export const filesAndIgnoresSchema = Object.freeze({
files: {
required: false,
merge() {
return undefined;
},
validate(value) {

// first check if it's an array
assertIsNonEmptyArray(value);

// then check each member
value.forEach(item => {
if (Array.isArray(item)) {
assertIsArrayOfStringsAndFunctions(item);
} else if (typeof item !== 'string' && typeof item !== 'function') {
throw new TypeError('Items must be a string, a function, or an array of strings and functions.');
}
});

}
},
ignores: {
required: false,
merge() {
return undefined;
},
validate: assertIsArrayOfStringsAndFunctions
}
});
17 changes: 17 additions & 0 deletions tests/config-array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,23 @@ describe('ConfigArray', () => {
.throw('Key "ignores": Expected array to only contain strings and functions.');

});

it('should throw an error when name is not a string', async () => {
configs = new ConfigArray([
{
files: ['**'],
name: true
}
], { basePath });
await configs.normalize();

expect(() => {
configs.getConfig(path.resolve(basePath, 'foo.js'));
})
.to
.throw('Key "name": Property must be a string.');

});
});

describe('ConfigArray members', () => {
Expand Down

0 comments on commit c61908e

Please sign in to comment.