diff --git a/package-lock.json b/package-lock.json index f059f16d..40e5bcfd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,7 @@ "concurrently": "^8.2.0", "glob": "^8.1.0", "jsdom": "^22.0.0", + "json-schema": "^0.4.0", "prettier": "^2.8.8", "prettier-plugin-organize-imports": "^3.1.1", "ts-node": "^10.8.2", @@ -9154,6 +9155,12 @@ "resolved": "https://registry.npmjs.org/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz", "integrity": "sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w==" }, + "node_modules/json-schema": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz", + "integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==", + "dev": true + }, "node_modules/json-schema-traverse": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", diff --git a/package.json b/package.json index 6caf2dae..041e1ac5 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "concurrently": "^8.2.0", "glob": "^8.1.0", "jsdom": "^22.0.0", + "json-schema": "^0.4.0", "prettier": "^2.8.8", "prettier-plugin-organize-imports": "^3.1.1", "ts-node": "^10.8.2", diff --git a/scripts/update-policies.tsx b/scripts/update-policies.tsx index df13b80b..17d30996 100644 --- a/scripts/update-policies.tsx +++ b/scripts/update-policies.tsx @@ -1,5 +1,4 @@ import { dereference } from "@apidevtools/json-schema-ref-parser"; -import { JSONSchema } from "@apidevtools/json-schema-ref-parser/dist/lib/types"; import { render } from "@zuplo/md-tools"; import arg from "arg"; import chalk from "chalk"; @@ -7,12 +6,13 @@ import chokidar from "chokidar"; import { existsSync } from "fs"; import { copyFile, mkdir, readdir, readFile, rm, writeFile } from "fs/promises"; import glob from "glob"; +import { JSONSchema7 } from "json-schema"; import path from "path"; import prettier from "prettier"; import React from "react"; import { renderToStaticMarkup } from "react-dom/server"; -type PolicySchema = JSONSchema & { +type PolicySchema = JSONSchema7 & { isPreview?: boolean; isDeprecated?: boolean; isPaidAddOn?: boolean; @@ -21,7 +21,11 @@ type PolicySchema = JSONSchema & { type PolicyProperties = Record< string, - { description: string; properties?: PolicyProperties } + { + default?: boolean | string | number; + description: string; + properties?: PolicyProperties; + } >; // NOTE: This component is used to generate policy HTML in the policy script @@ -98,7 +102,7 @@ function stringify(obj: any) { async function processProperties(properties) { const tasks = Object.keys(properties).map(async (key) => { if (properties[key].description) { - const html = await render(properties[key].description); + const { html } = await render(properties[key].description); properties[key].description = html; } if (properties[key].properties) { @@ -252,10 +256,10 @@ async function run() { schemaJson ) ); - const handler = schema.properties.handler as any; + const handler = schema.properties.handler as JSONSchema7; meta.defaultHandler = { - export: handler.properties.export.const, - module: handler.properties.module.const, + export: (handler.properties.export as JSONSchema7).const, + module: (handler.properties.module as JSONSchema7).const, options: {}, }; } @@ -377,10 +381,10 @@ async function getExampleHtml( throw new Error("Invalid schema"); } - const description = await render(schema.description); + const { html: description } = await render(schema.description); + const properties = (schema.properties.handler as any).properties.options ?.properties; - if (properties && Object.keys(properties).length === 0) { console.warn( chalk.yellow(