bcherny/json-schema-to-typescript

how to handle this error "This interface was referenced by ..."

tresorama opened this issue · 1 comments

I am trying to compile the following schema to a TS type, and I get an error that prevent the TS type from being created.

I am thinking the error can be dependent on the "schema" itself and not on this library.
This is because the same script run again this example json scheme is converted without error.

I am asking here some insight about the error, so I can later open a correct issue on the schema provider repo.

What i don't know is:

  • Why it happens ?
  • What part of the schema cause this?
  • Is caused by the schema ?

Input Schema

schema

Script Used

const path = require('path');
const fs = require('fs');
const { compile, compileFromFile } = require('json-schema-to-typescript');

// Utility - Disk
const getFilePathFromHere = (relativePath) => path.resolve(__dirname, relativePath);
const writeToDisk = (filePath, text) => { fs.writeFileSync(filePath, text); };

// run
(async () => {
  try {
    console.log('Initializing ...!');

    // fetch the schema and save to a local file
    const jsonSchema = await fetch("https://schemas.wp.org/trunk/theme.json").then(res => res.text());
    const filePath = getFilePathFromHere('./schema.json');
    writeToDisk(filePath, jsonSchema);

    // compile to TS type
    const tsTypes = await compileFromFile(filePath);
    writeToDisk(getFilePathFromHere('./schema.ts'), tsTypes);

    console.log('Success!');
    process.exit(0);
  } catch (error) {
    console.log('Error!');
    console.error(error);
    process.exit(1);
  }
})();

Error

Initializing ...!
Error!
SyntaxError: Expression expected. (11:61)
   9 | /**
  10 |  * This interface was referenced by `SettingsBlocksPropertiesComplete`'s JSON-Schema definition
> 11 |  * via the `patternProperty` "^[a-z][a-z0-9-]*/[a-z][a-z0-9-]*$".
     |                                                             ^
  12 |  */
  13 | export type SettingsPropertiesComplete = (SettingsProperties & {
  14 | appearanceTools?: unknown
    at v (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/parser-typescript.js:1:14679)
    at _H (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/parser-typescript.js:49:10722)
    at Object.cH [as parse] (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/parser-typescript.js:49:11028)
    at Object.parse (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/index.js:7515:23)
    at coreFormat (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/index.js:8829:18)
    at formatWithCursor2 (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/index.js:9021:18)
    at /Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/index.js:38183:12
    at format (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/prettier/index.js:38197:12)
    at format (/Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/json-schema-to-typescript/dist/src/formatter.js:20:34)
    at /Users/jacopo/Local Sites/2/demo-andomya-preventivo/app/public/wp-content/themes/andomya-theme--child-of-tt3/node_modules/json-schema-to-typescript/dist/src/index.js:163:56 {
  loc: { start: { line: 11, column: 61 } },
  codeFrame: '\x1B[0m \x1B[90m  9 |\x1B[39m \x1B[90m/**\x1B[39m\x1B[0m\n' +
    "\x1B[0m \x1B[90m 10 |\x1B[39m \x1B[90m * This interface was referenced by `SettingsBlocksPropertiesComplete`'s JSON-Schema definition\x1B[39m\x1B[0m\n" +
    '\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 11 |\x1B[39m \x1B[90m * via the `patternProperty` "^[a-z][a-z0-9-]*/\x1B[39m[a\x1B[33m-\x1B[39mz][a\x1B[33m-\x1B[39mz0\x1B[33m-\x1B[39m\x1B[35m9\x1B[39m\x1B[33m-\x1B[39m]\x1B[33m*\x1B[39m$\x1B[32m".\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m    |\x1B[39m                                                             \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 12 |\x1B[39m  \x1B[33m*\x1B[39m\x1B[33m/\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 13 |\x1B[39m \x1B[36mexport\x1B[39m type \x1B[33mSettingsPropertiesComplete\x1B[39m \x1B[33m=\x1B[39m (\x1B[33mSettingsProperties\x1B[39m \x1B[33m&\x1B[39m {\x1B[0m\n' +
    '\x1B[0m \x1B[90m 14 |\x1B[39m appearanceTools\x1B[33m?\x1B[39m\x1B[33m:\x1B[39m unknown\x1B[0m'
}

Fixed in v13.1.2.