From 798731f9b1d92b39612a9dab2a4ade3b90ea0044 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Fri, 15 Mar 2024 01:33:15 +0100 Subject: [PATCH] Restructure --- src/nodeUtils.js | 27 +-------------------------- src/utils.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/nodeUtils.js b/src/nodeUtils.js index 1c5c5f7..af88f8a 100644 --- a/src/nodeUtils.js +++ b/src/nodeUtils.js @@ -2,7 +2,7 @@ const klaw = require('klaw'); const fs = require('fs-extra'); const path = require('path'); -const { isUrl, isObject } = require('./utils'); +const { isUrl, makeAjvErrorMessage } = require('./utils'); const SCHEMA_CHOICE = ['anyOf', 'oneOf']; @@ -173,31 +173,6 @@ function isSchemaChoice(schemaPath) { return typeof schemaPath === 'string' && schemaPath.match(/\/(one|any)Of\/\d+\//); } -function makeAjvErrorMessage(error) { - let message = error.message; - if (isObject(error.params) && Object.keys(error.params).length > 0) { - let params = Object.entries(error.params) - .map(([key, value]) => { - let label = key.replace(/([^A-Z]+)([A-Z])/g, "$1 $2").toLowerCase(); - return `${label}: ${value}`; - }) - .join(', ') - message += ` (${params})`; - } - if (error.instancePath) { - return `${error.instancePath} ${message}`; - } - else if (error.schemaPath) { - return `${message}, for schema ${error.schemaPath}`; - } - else if (message) { - return message; - } - else { - return String(error); - } -} - async function resolveFiles(files, depth = -1) { const resolved = []; const extensions = [".geojson", ".json"]; diff --git a/src/utils.js b/src/utils.js index f79c230..a74a11c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -82,11 +82,37 @@ function getSummary(result, config) { return summary; } +function makeAjvErrorMessage(error) { + let message = error.message; + if (isObject(error.params) && Object.keys(error.params).length > 0) { + let params = Object.entries(error.params) + .map(([key, value]) => { + let label = key.replace(/([^A-Z]+)([A-Z])/g, "$1 $2").toLowerCase(); + return `${label}: ${value}`; + }) + .join(', ') + message += ` (${params})`; + } + if (error.instancePath) { + return `${error.instancePath} ${message}`; + } + else if (error.schemaPath) { + return `${message}, for schema ${error.schemaPath}`; + } + else if (message) { + return message; + } + else { + return String(error); + } +} + module.exports = { createAjv, getSummary, isObject, isUrl, loadSchemaFromUri, + makeAjvErrorMessage, normalizePath };