Skip to content

Commit

Permalink
Tweak comments, async, hide node deprecations for new-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Jul 13, 2024
1 parent dde6290 commit fff76c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
77 changes: 41 additions & 36 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ import * as jsoncParser from 'jsonc-parser'
import chalk from 'chalk'
import minimist from 'minimist'

const AjvDraft06SchemaJson = JSON.parse(
fs.readFileSync('node_modules/ajv/dist/refs/json-schema-draft-06.json'),
const AjvDraft06SchemaJson = readJsonFile(
'node_modules/ajv/dist/refs/json-schema-draft-06.json',
)
const temporaryCoverageDir = './temp'
const schemaDir = './src/schemas/json'
const testPositiveDir = './src/test'
const testNegativeDir = './src/negative_test'
const urlSchemaStore = 'https://json.schemastore.org/'
const catalog = JSON.parse(fs.readFileSync('./src/api/json/catalog.json'))
const catalog = readJsonFile('./src/api/json/catalog.json')
const schemaValidation = jsoncParser.parse(
fs.readFileSync('./src/schema-validation.json', 'utf-8'),
await fs.promises.readFile('./src/schema-validation.json', 'utf-8'),
)
const schemasToBeTested = fs.readdirSync(schemaDir)
const foldersPositiveTest = fs.readdirSync(testPositiveDir)
const foldersNegativeTest = fs.readdirSync(testNegativeDir)
const [schemasToBeTested, foldersPositiveTest, foldersNegativeTest] =
await Promise.all([
fs.promises.readdir(schemaDir),
fs.promises.readdir(testPositiveDir),
fs.promises.readdir(testNegativeDir),
])

// prettier-ignore
const SCHEMA_DIALECTS = [
{ schemaName: '2020-12', url: 'https://json-schema.org/draft/2020-12/schema', isActive: true, isTooHigh: true },
Expand All @@ -56,14 +60,14 @@ const log = {
},
}

const argv = minimist(process.argv.slice(2), {
boolean: ['help', 'lint'],
})

function readJsonFile(/** @type {string} */ filename) {
return JSON.parse(fs.readFileSync(filename, 'utf-8'))
}

const argv = minimist(process.argv.slice(2), {
boolean: ['help', 'lint'],
})

function skipThisFileName(/** @type {string} */ name) {
// This macOS file must always be ignored.
return name === '.DS_Store'
Expand All @@ -80,9 +84,8 @@ function getUrlFromCatalog(catalogUrl) {
}

/**
* @summary Create an exception with error text
* Make sure that the user see this error message.
* And not only the error message generated by npm after this message.
* @summary Calling this will terminate the process and show the text
* of each error message, in addition to npm's error message.
* @param {string[]} errorText
*/
function throwWithErrorText(errorText) {
Expand All @@ -102,9 +105,10 @@ function throwWithErrorText(errorText) {
async function remoteSchemaFile(schemaOnlyScan, showLog = true) {
for (const { url } of catalog.schemas) {
if (url.startsWith(urlSchemaStore)) {
// Skip local schema
// Skip local schemas
continue
}

try {
const res = await fetch(url)
const resText = await res.text()
Expand Down Expand Up @@ -407,7 +411,6 @@ async function localSchemaFileAndTestFile(
* @param {Schema} schema
*/
function testSchemaFileForBOM(schema) {
// JSON schema file must not have any BOM type
const buffer = schema.rawFile
const bomTypes = [
{ name: 'UTF-8', signature: [0xef, 0xbb, 0xbf] },
Expand Down Expand Up @@ -441,8 +444,8 @@ function testSchemaFileForBOM(schema) {
*/

/**
* There are multiple AJV version for each $schema version.
* return the correct AJV instance
* @summary There are multiple AJV versions for each $schema version. This returns
* the correct AJV instance
* @param {FactoryAJVParameter} schemaName
* @returns {Object}
*/
Expand Down Expand Up @@ -523,7 +526,7 @@ function factoryAJV({
*/

/**
* Get the option items for this specific jsonName
* @summary Gets the option items for a particular `jsonName`
* @param {string} jsonName
* @returns {getOptionReturn}
*/
Expand All @@ -544,7 +547,6 @@ function getOption(jsonName) {
},
)

// return all the collected values
return {
unknownFormatsList,
unknownKeywordsList,
Expand Down Expand Up @@ -704,8 +706,9 @@ function showSchemaVersions() {
try {
obj = getObj_(schema.jsonObj)
} catch {
// suppress possible JSON.parse exception. It will be processed as obj = undefined
// Suppress `JSON.parse` exceptions, leaving obj with value of `undefined`
}

if (obj) {
schemaDialectCounts.set(obj.url, schemaDialectCounts.get(obj.url) + 1)
} else {
Expand Down Expand Up @@ -788,7 +791,7 @@ function taskLint() {
lintSchemaNoSmartQuotes()
}

function taskCheck() {
async function taskCheck() {
// Check filesystem
assertDirectoryStructureIsValid()
assertFilenamesHaveCorrectExtensions()
Expand All @@ -801,7 +804,7 @@ function taskCheck() {
assertSchemaValidationJsonHasValidSkipTest()

// Check catalog.json
assertCatalogJsonPassesJsonLint()
await assertCatalogJsonPassesJsonLint()
assertCatalogJsonValidatesAgainstJsonSchema()
assertCatalogJsonHasNoDuplicateNames()
assertCatalogJsonHasNoPoorlyWordedFields()
Expand Down Expand Up @@ -1039,19 +1042,22 @@ async function remoteAssertSchemaHasNoBom() {
await remoteSchemaFile(testSchemaFileForBOM, false)
}

function assertCatalogJsonPassesJsonLint() {
jsonlint.parse(fs.readFileSync('./src/api/json/catalog.json', 'utf-8'), {
ignoreBOM: false,
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: false,
})
async function assertCatalogJsonPassesJsonLint() {
jsonlint.parse(
await fs.promises.readFile('./src/api/json/catalog.json', 'utf-8'),
{
ignoreBOM: false,
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: false,
},
)
}

function assertCatalogJsonValidatesAgainstJsonSchema() {
const catalogSchema = readJsonFile(
path.resolve('.', schemaDir, 'schema-catalog.json'),
path.join(schemaDir, 'schema-catalog.json'),
)
const ajvInstance = factoryAJV({ schemaName: 'draft-04' })
if (ajvInstance.validate(catalogSchema, catalog)) {
Expand Down Expand Up @@ -1142,7 +1148,7 @@ function assertCatalogJsonLocalUrlsMustRefFile() {
return
}
countScan++
// Check if local URL have .json extension
// Check if local URLs have .json extension
const filenameMustBeAtThisUrlDepthPosition = 3
const filename = catalogUrl.split('/')[filenameMustBeAtThisUrlDepthPosition]
if (!filename?.endsWith('.json')) {
Expand All @@ -1168,7 +1174,7 @@ function assertCatalogJsonIncludesAllSchemas() {

// Read all the JSON file name from catalog and add it to allCatalogLocalJsonFiles[]
getUrlFromCatalog((catalogUrl) => {
// No need to validate the local URL correctness. It is al ready done in "local_assert_catalog.json_local_url_must_ref_file"
// No need to validate the local URL correctness. It is already done in "local_assert_catalog.json_local_url_must_ref_file"
// Only scan for local schema.
if (catalogUrl.startsWith(urlSchemaStore)) {
const filename = catalogUrl.split('/').pop()
Expand All @@ -1190,7 +1196,7 @@ function assertCatalogJsonIncludesAllSchemas() {
}
}
}
// Get all the json file for AJV
// Get all the JSON files for AJV
localSchemaFileAndTestFile(
{ schemaOnlyScan: schemaFileCompare },
{ fullScanAllFiles: true },
Expand Down Expand Up @@ -1929,7 +1935,6 @@ async function taskCoverage() {
}

function printStrictAndNotStrictAjvValidatedSchemas() {
// this is only for AJV schemas
const schemaVersion = showSchemaVersions()
const schemaInFullStrictMode = []
const schemaInNotStrictMode = []
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"prettier:fix": "prettier --config .prettierrc.cjs --ignore-path .gitignore --write .",
"eslint": "eslint ./cli.js",
"eslint:fix": "eslint --fix ./cli.js",
"new-schema": "node ./cli.js new-schema",
"new-schema": "node --no-deprecation ./cli.js new-schema",
"check": "node ./cli.js check",
"check-remote": "node ./cli.js check-remote",
"maintenance": "node ./cli.js maintenance",
Expand Down

0 comments on commit fff76c6

Please sign in to comment.