Skip to content

Commit

Permalink
fix(scripts): support for object in schema's type section
Browse files Browse the repository at this point in the history
  • Loading branch information
yaegassy authored and d8vjork committed Jun 28, 2023
1 parent c706140 commit c74f2e6
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions scripts/generate-rules-schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ function relativePath(fromPath) {
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), fromPath);
}

function mapTypeToJsonSchema(type) {
function mapTypeToJsonSchema(type, defaultValueType) {
if (type === 'bool') {
return 'boolean';
}

if (type === "array" && defaultValueType === "object") {
return "object";
}

return type;
}

Expand All @@ -48,14 +52,28 @@ function ruleIntoJsonSchemaProperty(rule) {

jsonSchemaProperty.properties[configItem.name].description = configItem.description;

let defaultValueType = undefined;
if ('defaultValue' in configItem) {
jsonSchemaProperty.properties[configItem.name].default = configItem.defaultValue;

if (configItem.defaultValue === null) {
defaultValueType = 'null';
} else if (typeof configItem.defaultValue === 'object') {
if (!Array.isArray(configItem.defaultValue)) {
defaultValueType = 'object';
} else {
defaultValueType = 'array';
}
} else {
defaultValueType = typeof configItem.defaultValue;
}
}

if ('allowedTypes' in configItem) {
jsonSchemaProperty.properties[configItem.name].type = configItem.allowedTypes.length > 1
? configItem.allowedTypes.map(mapTypeToJsonSchema)
: mapTypeToJsonSchema(configItem.allowedTypes[0]);
jsonSchemaProperty.properties[configItem.name].type =
configItem.allowedTypes.length > 1
? configItem.allowedTypes.map((type) => mapTypeToJsonSchema(type, defaultValueType))
: mapTypeToJsonSchema(configItem.allowedTypes[0], defaultValueType);
}

if ('allowedValues' in configItem) {
Expand Down Expand Up @@ -98,4 +116,4 @@ writeFileSync(
schemaContentPath,
JSON.stringify(schemaContent, null, 2),
{ encoding: 'utf-8' }
);
);

0 comments on commit c74f2e6

Please sign in to comment.