v1.0.10
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
deepMerge,
|
||||
buildCreateDefaults,
|
||||
buildEmbeddedDefaults,
|
||||
buildNewObjectValue,
|
||||
} from './schemaResolver';
|
||||
import { getDisplayProperty } from './schemaResolver';
|
||||
|
||||
@@ -865,3 +866,115 @@ describe('getDisplayProperty', () => {
|
||||
expect(getDisplayProperty(schema, 'x:NoLabel')).toBe('title');
|
||||
});
|
||||
});
|
||||
|
||||
const structSchema: Schema = {
|
||||
objects: {},
|
||||
schemas: {
|
||||
'x:Service': { type: 'single', schemaName: 'x:Service' },
|
||||
'x:Listener': { type: 'single', schemaName: 'x:Listener' },
|
||||
'x:Tls': { type: 'single', schemaName: 'x:Tls' },
|
||||
'x:Store': {
|
||||
type: 'multiple',
|
||||
variants: [
|
||||
{ name: 'S3', label: 'S3', schemaName: 'x:S3Store' },
|
||||
{ name: 'Manual', label: 'Manual' },
|
||||
],
|
||||
},
|
||||
},
|
||||
fields: {
|
||||
'x:Service': {
|
||||
properties: {
|
||||
hostname: {
|
||||
description: '',
|
||||
type: { type: 'string', format: 'string', nullable: true },
|
||||
update: 'mutable',
|
||||
},
|
||||
cleartext: { description: '', type: { type: 'boolean' }, update: 'mutable' },
|
||||
},
|
||||
},
|
||||
'x:Listener': {
|
||||
properties: {
|
||||
enabled: { description: '', type: { type: 'boolean' }, update: 'mutable' },
|
||||
proxied: { description: '', type: { type: 'boolean' }, update: 'mutable' },
|
||||
readOnly: { description: '', type: { type: 'boolean' }, update: 'serverSet' },
|
||||
tls: { description: '', type: { type: 'object', objectName: 'x:Tls' }, update: 'mutable' },
|
||||
fallback: {
|
||||
description: '',
|
||||
type: { type: 'object', objectName: 'x:Tls', nullable: true },
|
||||
update: 'mutable',
|
||||
},
|
||||
},
|
||||
defaults: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
'x:Tls': {
|
||||
properties: {
|
||||
implicit: { description: '', type: { type: 'boolean' }, update: 'mutable' },
|
||||
certificateId: {
|
||||
description: '',
|
||||
type: { type: 'string', format: 'string', nullable: true },
|
||||
update: 'mutable',
|
||||
},
|
||||
},
|
||||
},
|
||||
'x:S3Store': {
|
||||
properties: {
|
||||
bucket: { description: '', type: { type: 'string', format: 'string' }, update: 'mutable' },
|
||||
allowInvalidCerts: { description: '', type: { type: 'boolean' }, update: 'mutable' },
|
||||
},
|
||||
defaults: {
|
||||
bucket: 'stalwart',
|
||||
},
|
||||
},
|
||||
},
|
||||
forms: {},
|
||||
lists: {},
|
||||
enums: {},
|
||||
dashboards: [],
|
||||
layouts: [],
|
||||
};
|
||||
|
||||
describe('buildNewObjectValue', () => {
|
||||
it('seeds non-nullable booleans a struct has no defaults for', () => {
|
||||
expect(buildNewObjectValue(structSchema, 'x:Service')).toEqual({ cleartext: false });
|
||||
});
|
||||
|
||||
it('keeps schema defaults and only fills the missing booleans', () => {
|
||||
const result = buildNewObjectValue(structSchema, 'x:Listener');
|
||||
expect(result.enabled).toBe(true);
|
||||
expect(result.proxied).toBe(false);
|
||||
});
|
||||
|
||||
it('skips serverSet properties', () => {
|
||||
expect(buildNewObjectValue(structSchema, 'x:Listener')).not.toHaveProperty('readOnly');
|
||||
});
|
||||
|
||||
it('recurses into non-nullable embedded objects and skips nullable ones', () => {
|
||||
const result = buildNewObjectValue(structSchema, 'x:Listener');
|
||||
expect(result.tls).toEqual({ implicit: false });
|
||||
expect(result).not.toHaveProperty('fallback');
|
||||
});
|
||||
|
||||
it('seeds the first variant with its @type, defaults and booleans', () => {
|
||||
expect(buildNewObjectValue(structSchema, 'x:Store')).toEqual({
|
||||
'@type': 'S3',
|
||||
bucket: 'stalwart',
|
||||
allowInvalidCerts: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('honours an explicit variant name', () => {
|
||||
expect(buildNewObjectValue(structSchema, 'x:Store', 'Manual')).toEqual({ '@type': 'Manual' });
|
||||
});
|
||||
|
||||
it('returns an empty object for an unknown object name', () => {
|
||||
expect(buildNewObjectValue(structSchema, 'x:Unknown')).toEqual({});
|
||||
});
|
||||
|
||||
it('still merges parent defaults into embedded children', () => {
|
||||
const result = buildNewObjectValue(embeddedSchema, 'x:Model', 'FtrlCcfh');
|
||||
expect(result.featureL2Normalize).toBe(true);
|
||||
expect((result.parameters as Record<string, unknown>).numFeatures).toBe('20');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -260,6 +260,62 @@ export function buildEmbeddedDefaults(
|
||||
return result;
|
||||
}
|
||||
|
||||
export function buildNewObjectValue(schema: Schema, objectName: string, variantName?: string): Record<string, unknown> {
|
||||
const result = buildEmbeddedDefaults(schema, objectName, {}, variantName);
|
||||
return completeStructDefaults(schema, objectName, (result['@type'] as string | undefined) ?? variantName, result);
|
||||
}
|
||||
|
||||
function completeStructDefaults(
|
||||
schema: Schema,
|
||||
objectName: string,
|
||||
variantName: string | undefined,
|
||||
target: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const resolved = resolveSchema(schema, objectName);
|
||||
if (!resolved) return target;
|
||||
|
||||
const fields =
|
||||
resolved.type === 'single'
|
||||
? resolved.fields
|
||||
: ((variantName ? resolved.variants.find((v) => v.name === variantName) : resolved.variants[0])?.fields ?? null);
|
||||
if (!fields) return target;
|
||||
|
||||
for (const [propName, propDef] of Object.entries(fields.properties)) {
|
||||
if (propDef.update === 'serverSet') continue;
|
||||
const t = propDef.type;
|
||||
|
||||
if (t.type === 'boolean') {
|
||||
if (!(propName in target)) {
|
||||
target[propName] = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (t.type !== 'object' || t.nullable) continue;
|
||||
|
||||
const current = target[propName];
|
||||
if (current !== undefined && !isPlainRecord(current)) continue;
|
||||
|
||||
const overrides = isPlainRecord(current) ? current : {};
|
||||
const nestedEntry = schema.schemas[t.objectName];
|
||||
const nestedVariant =
|
||||
nestedEntry?.type === 'multiple'
|
||||
? ((overrides['@type'] as string | undefined) ?? nestedEntry.variants[0]?.name)
|
||||
: undefined;
|
||||
|
||||
const nested = completeStructDefaults(schema, t.objectName, nestedVariant, { ...overrides });
|
||||
if (Object.keys(nested).length > 0) {
|
||||
target[propName] = nested;
|
||||
}
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function getDisplayProperty(schema: Schema, objectName: string): string {
|
||||
const list = schema.lists[objectName];
|
||||
if (list?.labelProperty) return list.labelProperty;
|
||||
|
||||
Reference in New Issue
Block a user