mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-09-11 12:17:45 +02:00
[eric] imports: extract schema getDefault into shared
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
export interface SchemaNode {
|
||||
type?: string;
|
||||
properties?: Record<string, SchemaNode>;
|
||||
items?: SchemaNode;
|
||||
required?: string[];
|
||||
enum?: string[];
|
||||
description?: string;
|
||||
default?: any;
|
||||
}
|
||||
|
||||
export function getDefault(schema: SchemaNode): any {
|
||||
if (schema.default !== undefined) return schema.default;
|
||||
switch (schema.type) {
|
||||
case 'string': return '';
|
||||
case 'number': return 0;
|
||||
case 'boolean': return false;
|
||||
case 'array': return [];
|
||||
case 'object': {
|
||||
const obj: Record<string, any> = {};
|
||||
if (schema.properties) {
|
||||
for (const [k, v] of Object.entries(schema.properties)) {
|
||||
obj[k] = getDefault(v);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
default: return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user