Skip to content

Commit

Permalink
JSON validation (#394)
Browse files Browse the repository at this point in the history
* Set up JSON testing

* Add schema validation

* Use parseJson

* Finish types

* Disambiguate ext/json-schema from node dependency with the same name

* Add support for specifying the jsconfig file

* Don't expose types

* Update types

* Use dictionary map type

* Fix types

* Fix AJV warnings

* Move types

* Move anb rename file

* Move common mocks

* Simplify types
  • Loading branch information
toasted-nutbread authored Dec 20, 2023
1 parent b13fbd4 commit 8b943cc
Show file tree
Hide file tree
Showing 30 changed files with 842 additions and 235 deletions.
1 change: 0 additions & 1 deletion dev/data/manifest-variants.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,6 @@
},
{
"name": "safari",
"fileName": null,
"modifications": [
{
"action": "remove",
Expand Down
2 changes: 1 addition & 1 deletion dev/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"error": ["../types/ext/error"],
"event-listener-collection": ["../types/ext/event-listener-collection"],
"japanese-util": ["../types/ext/japanese-util"],
"json-schema": ["../types/ext/json-schema"],
"ext/json-schema": ["../types/ext/json-schema"],
"log": ["../types/ext/log"],
"settings": ["../types/ext/settings"],
"structured-content": ["../types/ext/structured-content"],
Expand Down
2 changes: 1 addition & 1 deletion dev/manifest-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class ManifestUtil {

/**
* @param {import('dev/manifest').Manifest} manifest
* @param {import('dev/manifest').Modification[]} modifications
* @param {import('dev/manifest').Modification[]|undefined} modifications
* @returns {import('dev/manifest').Manifest}
*/
_applyModifications(manifest, modifications) {
Expand Down
2 changes: 1 addition & 1 deletion dev/schema-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ class JsonSchemaAjv {
export function createJsonSchema(mode, schema) {
switch (mode) {
case 'ajv': return new JsonSchemaAjv(schema);
default: return new JsonSchema(/** @type {import('json-schema').Schema} */ (schema));
default: return new JsonSchema(/** @type {import('ext/json-schema').Schema} */ (schema));
}
}
8 changes: 5 additions & 3 deletions dev/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function getArgs(args, argMap) {

/**
* @param {string} baseDirectory
* @param {?(fileName: string) => boolean} predicate
* @param {?(fileName: string, isDirectory: boolean) => boolean} predicate
* @returns {string[]}
*/
export function getAllFiles(baseDirectory, predicate = null) {
Expand All @@ -86,11 +86,13 @@ export function getAllFiles(baseDirectory, predicate = null) {
const relativeFileName = path.relative(baseDirectory, fullFileName);
const stats = fs.lstatSync(fullFileName);
if (stats.isFile()) {
if (typeof predicate !== 'function' || predicate(relativeFileName)) {
if (typeof predicate !== 'function' || predicate(relativeFileName, false)) {
results.push(relativeFileName);
}
} else if (stats.isDirectory()) {
directories.push(fullFileName);
if (typeof predicate !== 'function' || predicate(relativeFileName, true)) {
directories.push(fullFileName);
}
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions ext/js/background/profile-conditions-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelEqual(value) {
const number = this._stringToNumber(value);
Expand All @@ -172,7 +172,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelNotEqual(value) {
return {
Expand All @@ -184,7 +184,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelLessThan(value) {
const number = this._stringToNumber(value);
Expand All @@ -198,7 +198,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelGreaterThan(value) {
const number = this._stringToNumber(value);
Expand All @@ -212,7 +212,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelLessThanOrEqual(value) {
const number = this._stringToNumber(value);
Expand All @@ -226,7 +226,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaPopupLevelGreaterThanOrEqual(value) {
const number = this._stringToNumber(value);
Expand All @@ -242,7 +242,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaUrlMatchDomain(value) {
const oneOf = [];
Expand All @@ -261,7 +261,7 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaUrlMatchRegExp(value) {
return {
Expand All @@ -276,15 +276,15 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaModifierKeysAre(value) {
return this._createSchemaArrayCheck('modifierKeys', value, true, false);
}

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaModifierKeysAreNot(value) {
return {
Expand All @@ -296,15 +296,15 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaModifierKeysInclude(value) {
return this._createSchemaArrayCheck('modifierKeys', value, false, false);
}

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaModifierKeysNotInclude(value) {
return this._createSchemaArrayCheck('modifierKeys', value, false, true);
Expand All @@ -314,15 +314,15 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaFlagsAre(value) {
return this._createSchemaArrayCheck('flags', value, true, false);
}

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaFlagsAreNot(value) {
return {
Expand All @@ -334,15 +334,15 @@ export class ProfileConditionsUtil {

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaFlagsInclude(value) {
return this._createSchemaArrayCheck('flags', value, false, false);
}

/**
* @param {string} value
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaFlagsNotInclude(value) {
return this._createSchemaArrayCheck('flags', value, false, true);
Expand All @@ -355,10 +355,10 @@ export class ProfileConditionsUtil {
* @param {string} value
* @param {boolean} exact
* @param {boolean} none
* @returns {import('json-schema').Schema}
* @returns {import('ext/json-schema').Schema}
*/
_createSchemaArrayCheck(key, value, exact, none) {
/** @type {import('json-schema').Schema[]} */
/** @type {import('ext/json-schema').Schema[]} */
const containsList = [];
for (const item of this._split(value)) {
if (item.length === 0) { continue; }
Expand All @@ -369,7 +369,7 @@ export class ProfileConditionsUtil {
});
}
const containsListCount = containsList.length;
/** @type {import('json-schema').Schema} */
/** @type {import('ext/json-schema').Schema} */
const schema = {
type: 'array'
};
Expand Down
Loading

0 comments on commit 8b943cc

Please sign in to comment.