diff options
author | Olivier Chafik <ochafik@users.noreply.github.com> | 2024-03-22 13:07:44 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 15:07:44 +0200 |
commit | 72114edf068a9b2f54d3b09e9a198f611be397e8 (patch) | |
tree | 0614f917e7ecd6a9c86dbcebd43ce4161dea8997 /examples/server/public/json-schema-to-grammar.mjs | |
parent | 2f0e81e053b41ca28e73a841e7bdbf9820baaa57 (diff) |
json-schema-to-grammar : fix order of props + non-str const/enum (#6232)
* json: ordered json in server/schema converter to respect orig order
* json: ws nits
* json: support non-string const / enums
Diffstat (limited to 'examples/server/public/json-schema-to-grammar.mjs')
-rw-r--r-- | examples/server/public/json-schema-to-grammar.mjs | 12 |
1 files changed, 3 insertions, 9 deletions
diff --git a/examples/server/public/json-schema-to-grammar.mjs b/examples/server/public/json-schema-to-grammar.mjs index baa6761c..17bd60fd 100644 --- a/examples/server/public/json-schema-to-grammar.mjs +++ b/examples/server/public/json-schema-to-grammar.mjs @@ -48,7 +48,7 @@ export class SchemaConverter { } _formatLiteral(literal) { - const escaped = JSON.stringify(literal).replace( + const escaped = literal.replace( GRAMMAR_LITERAL_ESCAPE_RE, m => GRAMMAR_LITERAL_ESCAPES[m] ); @@ -327,10 +327,7 @@ export class SchemaConverter { } _generateConstantRule(value) { - if (typeof value !== 'string') { - throw new Error('Only string constants are supported, got ' + JSON.stringify(value)); - } - return this._formatLiteral(value); + return this._formatLiteral(JSON.stringify(value)); } visit(schema, name) { @@ -346,9 +343,6 @@ export class SchemaConverter { } else if (Array.isArray(schemaType)) { return this._addRule(ruleName, this._generateUnionRule(name, schemaType.map(t => ({ type: t })))); } else if ('const' in schema) { - if (typeof schema.const !== 'string') { - throw new Error('Only string constants are supported, got ' + JSON.stringify(schema.const)); - } return this._addRule(ruleName, this._generateConstantRule(schema.const)); } else if ('enum' in schema) { const rule = schema.enum.map(v => this._generateConstantRule(v)).join(' | '); @@ -457,7 +451,7 @@ export class SchemaConverter { const propRuleName = this.visit(propSchema, `${name ?? ''}${name ? '-' : ''}${propName}`); propKvRuleNames[propName] = this._addRule( `${name ?? ''}${name ? '-' : ''}${propName}-kv`, - `${this._formatLiteral(propName)} space ":" space ${propRuleName}` + `${this._formatLiteral(JSON.stringify(propName))} space ":" space ${propRuleName}` ); } const requiredProps = sortedProps.filter(k => required.has(k)); |