diff --git a/src/macrofier/types.mjs b/src/macrofier/types.mjs index 55ef7b7e..aca481c7 100644 --- a/src/macrofier/types.mjs +++ b/src/macrofier/types.mjs @@ -227,7 +227,7 @@ const insertEnumMacros = (content, schema, module, name, suffix, templateDir = " if (!value) { value = getTemplate(path.join(templateDir, 'unset' + suffix)) } - value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value)) + value ? values.push(template[i].replace(/\$\{key\}/g, getSafeEnumKeyName(value, schema.enumKeyPrefix)) .replace(/\$\{value\}/g, value)) : '' }) template[i] = values.map((value, id) => { diff --git a/src/shared/json-schema.mjs b/src/shared/json-schema.mjs index 5d1d3a27..b8c28fef 100644 --- a/src/shared/json-schema.mjs +++ b/src/shared/json-schema.mjs @@ -426,12 +426,19 @@ function mergeOneOf(schema) { return union(schema.oneOf) } -const getSafeEnumKeyName = (value) => value.split(':').pop() // use last portion of urn:style:values - .replace(/[\.\-]/g, '_') // replace dots and dashes - .replace(/\+/g, '_plus') // change + to _plus - .replace(/([a-z])([A-Z0-9])/g, '$1_$2') // camel -> snake case - .replace(/^([0-9]+(\.[0-9]+)?)/, 'v$1') // insert `v` in front of things that look like version numbers - .toUpperCase() +const getSafeEnumKeyName = function(value, keyPrefix = '') { + if (keyPrefix != '') { + value = keyPrefix + '_' + value + } + + let key = value.split(':').pop() // use last portion of urn:style:values + .replace(/\+/g, '_plus') // change + to _plus + .replace(/[\.\-\/\;]/g, '_') // replace special characters + .replace(/([a-z])([A-Z0-9])/g, '$1_$2') // camel -> snake case + .replace(/^([0-9]+\_([0-9]+)?)/, 'v$1') // insert `v` in front of things that look like version numbers + + return key.toUpperCase() +} export { getSchemaConstraints, diff --git a/src/validate/index.mjs b/src/validate/index.mjs index f9002554..94e127a2 100644 --- a/src/validate/index.mjs +++ b/src/validate/index.mjs @@ -108,7 +108,7 @@ const run = async ({ addFormats(ajv) // explicitly add our custom extensions so we can keep strict mode on (TODO: put these in a JSON config?) - ajv.addVocabulary(['x-method', 'x-this-param', 'x-additional-params', 'x-schemas', 'components', 'x-property']) + ajv.addVocabulary(['x-method', 'x-this-param', 'x-additional-params', 'x-schemas', 'components', 'x-property', 'enumKeyPrefix']) const firebolt = ajv.compile(fireboltOpenRpcSpec) const jsonschema = ajv.compile(jsonSchemaSpec)