Skip to content

Commit ca45f82

Browse files
committed
improved/fixed display of schema constraints
1 parent 82e437e commit ca45f82

File tree

3 files changed

+53
-43
lines changed

3 files changed

+53
-43
lines changed

src/components/schema-table.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,19 @@ export default class SchemaTable extends LitElement {
116116
}
117117

118118
generateTree(data, dataType = 'object', key = '', description = '', level = 0) {
119-
const leftPadding = 16 * level; // 2 space indentation at each level
119+
/*
120+
if ((data['::type'] || '').includes('xxx-of-option') || key.startsWith('::OPTION')) {
121+
level -= 1;
122+
}
123+
*/
124+
if (key.startsWith('::OPTION')) {
125+
level -= 1;
126+
}
127+
let leftPadding = 16 * level; // 2 space indentation at each level
128+
if (key.startsWith('::OPTION')) {
129+
leftPadding -= 6;
130+
}
131+
120132
if (!data) {
121133
return html`<div class="null" style="display:inline;">null</div>`;
122134
}
@@ -158,7 +170,7 @@ export default class SchemaTable extends LitElement {
158170
? html`
159171
<div class='tr ${level < this.schemaExpandLevel ? 'expanded' : 'collapsed'} ${data['::type']}' data-obj='${keyLabel}'>
160172
<div class="td key ${data['::deprecated'] ? 'deprecated' : ''}" style='padding-left:${leftPadding}px'>
161-
${keyLabel || keyDescr
173+
${(keyLabel || keyDescr) && (data['::type'] || '').includes('xxx-of-option') === false
162174
? html`
163175
<span
164176
class='obj-toggle ${level < this.schemaExpandLevel ? 'expanded' : 'collapsed'}'
@@ -225,7 +237,7 @@ export default class SchemaTable extends LitElement {
225237
if (readorWriteOnly === '🆆' && this.schemaHideWriteOnly === 'true') {
226238
return;
227239
}
228-
const dataTypeCss = type.replace('{', '').substring(0, 4).toLowerCase();
240+
const dataTypeCss = type.replace(/\{|\[/g, '').substring(0, 4).toLowerCase();
229241
return html`
230242
<div class = "tr primitive">
231243
<div class="td key ${deprecated}" style='padding-left:${leftPadding}px' >
@@ -241,10 +253,10 @@ export default class SchemaTable extends LitElement {
241253
<span style="font-family: var(--font-mono);">${readorWriteOnly} </span> </div>
242254
<div class='td key-descr'>
243255
${dataType === 'array' ? description : ''}
244-
${constraint ? html`<div style='color: var(--fg2); padding-bottom:3px;'>${allowedValues}</div>` : ''}
245-
${defaultValue ? html`<div style='color: var(--fg2); padding-bottom:3px;' ><span class='bold-text'>Default:</span> ${defaultValue}</div>` : ''}
246-
${allowedValues ? html`<div style='color: var(--fg2); padding-bottom:3px;'><span class='bold-text'>Allowed: </span> &nbsp; ${allowedValues}</div>` : ''}
247-
${pattern ? html`<div style='color: var(--fg2); padding-bottom:3px;'><span class='bold-text'>Pattern:</span> &nbsp; ${pattern}</div>` : ''}
256+
${constraint ? html`<div style='color: var(--fg2); padding-bottom:3px;'> <span class='bold-text'>Constraints:</span> ${constraint}</div>` : ''}
257+
${defaultValue ? html`<div style='color: var(--fg2); padding-bottom:3px;'> <span class='bold-text'>Default:</span> ${defaultValue}</div>` : ''}
258+
${allowedValues ? html`<div style='color: var(--fg2); padding-bottom:3px;'> <span class='bold-text'>Allowed: </span> &nbsp; ${allowedValues}</div>` : ''}
259+
${pattern ? html`<div style='color: var(--fg2); padding-bottom:3px;'> <span class='bold-text'>Pattern:</span> &nbsp; ${pattern}</div>` : ''}
248260
${schemaDescription ? html`<span class="m-markdown-small">${unsafeHTML(marked(schemaDescription))}</span>` : ''}
249261
</div>
250262
</div>

src/components/schema-tree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export default class SchemaTree extends LitElement {
230230
if (readorWriteOnly === '🆆' && this.schemaHideWriteOnly === 'true') {
231231
return;
232232
}
233-
const dataTypeCss = type.replace(/^\{|^\[/g, '').substring(0, 4).toLowerCase();
233+
const dataTypeCss = type.replace(/\{|\[/g, '').substring(0, 4).toLowerCase();
234234
return html`
235235
<div class = "tr primitive">
236236
<div class="td key ${deprecated}" style='min-width:${minFieldColWidth}px' >
@@ -242,14 +242,14 @@ export default class SchemaTree extends LitElement {
242242
? html`<span class="key-label">${keyLabel}:</span>`
243243
: ''
244244
}
245-
<span class=${dataTypeCss} style="font-family:var(--font-regular)">
245+
<span class="${dataTypeCss}" >
246246
${dataType === 'array' ? `[${type}]` : `${type}`}
247247
${readorWriteOnly}
248248
</span>
249249
</div>
250250
<div class='td key-descr'>
251251
${dataType === 'array' ? description : ''}
252-
${constraint ? html`<div style='color: var(--fg2)'>${constraint}</div>` : ''}
252+
${constraint ? html`<div style='color: var(--fg2)'><span class='bold-text'>Constraints:</span> &nbsp;${constraint}</div>` : ''}
253253
${defaultValue ? html`<div style='color: var(--fg2)'><span class='bold-text'>Default:</span> ${defaultValue}</div>` : ''}
254254
${allowedValues ? html`<div style='color: var(--fg2)'><span class='bold-text'>Allowed:</span> &nbsp; ${allowedValues}</div>` : ''}
255255
${pattern ? html`<div style='color: var(--fg2)'><span class='bold-text'>Pattern:</span> ${pattern}</div>` : ''}

src/utils/schema-utils.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ export function getTypeInfo(schema) {
33
if (!schema) {
44
return;
55
}
6-
76
let dataType = '';
7+
let constrain = '';
8+
89
if (schema.$ref) {
910
const n = schema.$ref.lastIndexOf('/');
1011
const schemaNode = schema.$ref.substring(n + 1);
@@ -43,52 +44,44 @@ export function getTypeInfo(schema) {
4344
arrayType: '',
4445
html: '',
4546
};
47+
4648
if (info.type === '{recursive}') {
4749
info.description = schema.$ref.substring(schema.$ref.lastIndexOf('/') + 1);
4850
} else if (info.type === '{missing-type-info}') {
4951
info.description = info.description || '';
5052
}
5153

5254
// Set Allowed Values
53-
if (schema.enum) {
54-
let opt = '';
55-
schema.enum.map((v) => {
56-
opt += `${v}, `;
57-
});
58-
info.allowedValues = opt.slice(0, -2);
59-
}
55+
info.allowedValues = Array.isArray(schema.enum) ? schema.enum.join('┃') : '';
56+
if (dataType === 'array' && schema.items) {
57+
const arrayItemType = schema.items?.type;
58+
const arrayItemDefault = schema.items?.default !== undefined ? schema.items.default : '';
6059

61-
if (schema.type === 'array' && schema.items) {
62-
const arraySchema = schema.items;
63-
info.arrayType = `${schema.type} of ${arraySchema.type}`;
64-
info.default = arraySchema.default === 0 ? '0 ' : (arraySchema.default ? arraySchema.default : '');
65-
if (arraySchema.enum) {
66-
let opt = '';
67-
arraySchema.enum.map((v) => {
68-
opt += `${v}, `;
69-
});
70-
info.allowedValues = opt.slice(0, -2);
60+
info.arrayType = `${schema.type} of ${Array.isArray(arrayItemType) ? arrayItemType.join('') : arrayItemType}`;
61+
info.default = arrayItemDefault;
62+
info.allowedValues = Array.isArray(schema.items?.enum) ? schema.items.enum.join('┃') : '';
63+
}
64+
if (dataType.match(/integer|number/g)) {
65+
if (schema.minimum !== undefined || schema.exclusiveMinimum !== undefined) {
66+
constrain += schema.minimum !== undefined ? `>= ${schema.minimum}` : `> ${schema.exclusiveMinimum}`;
7167
}
72-
} else if (schema.type === 'integer' || schema.type === 'number') {
73-
if (schema.minimum !== undefined && schema.maximum !== undefined) {
74-
info.constrain = `${schema.exclusiveMinimum ? '>' : '>='}${schema.minimum} and ${schema.exclusiveMaximum ? '<' : '<='} ${schema.maximum}`;
75-
} else if (schema.minimum !== undefined && schema.maximum === undefined) {
76-
info.constrain = `${schema.exclusiveMinimum ? '>' : '>='}${schema.minimum}`;
77-
} else if (schema.minimum === undefined && schema.maximum !== undefined) {
78-
info.constrain = `${schema.exclusiveMaximum ? '<' : '<='}${schema.maximum}`;
68+
if (schema.maximum !== undefined || schema.exclusiveMaximum !== undefined) {
69+
constrain += schema.maximum !== undefined ? `${constrain ? '┃' : ''}<= ${schema.maximum}` : `${constrain ? '┃' : ''}< ${schema.exclusiveMaximum}`;
7970
}
8071
if (schema.multipleOf !== undefined) {
81-
info.constrain = `(multiple of ${schema.multipleOf})`;
72+
constrain += `${constrain ? '┃' : ''} multiple of ${schema.multipleOf}`;
8273
}
83-
} else if (schema.type === 'string') {
74+
}
75+
if (dataType.match(/string/g)) {
8476
if (schema.minLength !== undefined && schema.maxLength !== undefined) {
85-
info.constrain = `(${schema.minLength} to ${schema.maxLength} chars)`;
86-
} else if (schema.minLength !== undefined && schema.maxLength === undefined) {
87-
info.constrain = `min ${schema.minLength} chars`;
88-
} else if (schema.minLength === undefined && schema.maxLength !== undefined) {
89-
info.constrain = `max ${schema.maxLength} chars`;
77+
constrain += `${constrain ? '┃' : ''}${schema.minLength} to ${schema.maxLength} chars`;
78+
} else if (schema.minLength !== undefined) {
79+
constrain += `${constrain ? '┃' : ''}min ${schema.minLength} chars`;
80+
} else if (schema.maxLength !== undefined) {
81+
constrain += `max ${constrain ? '┃' : ''}${schema.maxLength} chars`;
9082
}
9183
}
84+
info.constrain = constrain;
9285
info.html = `${info.type}~|~${info.readOrWriteOnly}~|~${info.constrain}~|~${info.default}~|~${info.allowedValues}~|~${info.pattern}~|~${info.description}~|~${schema.title || ''}~|~${info.deprecated ? 'deprecated' : ''}`;
9386
return info;
9487
}
@@ -478,9 +471,14 @@ export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
478471
subSchema.type.forEach((v) => {
479472
if (v.match(/integer|number|string|null|boolean/g)) {
480473
primitiveType.push(v);
481-
} else if (v === 'array' && subSchema.items?.type.match(/integer|number|string|null|boolean/g)) {
474+
} else if (v === 'array' && typeof subSchema.items?.type === 'string' && subSchema.items?.type.match(/integer|number|string|null|boolean/g)) {
482475
// Array with primitive types should also be treated as primitive type
476+
477+
// if (subSchema.items.type === 'string' && subSchema.items.format) {
478+
// primitiveType.push(`[${subSchema.items.format}]`);
479+
// } else {
483480
primitiveType.push(`[${subSchema.items.type}]`);
481+
// }
484482
} else {
485483
complexTypes.push(v);
486484
}

0 commit comments

Comments
 (0)