mirror of
https://github.com/smartxworks/sunmao-ui.git
synced 2024-12-09 09:20:18 +08:00
fix(validator): add AnyTypePlaceholder for nester object
This commit is contained in:
parent
7f2fdd5f63
commit
d21e1526f6
@ -93,5 +93,13 @@ describe('generateDefaultValueFromSpec function', () => {
|
||||
it('can parse any type', () => {
|
||||
expect(generateDefaultValueFromSpec({})).toEqual('');
|
||||
expect(generateDefaultValueFromSpec({}, true)).toEqual(AnyTypePlaceholder);
|
||||
expect(
|
||||
(
|
||||
generateDefaultValueFromSpec(
|
||||
Type.Object({ foo: Type.Object({ bar: Type.Any() }) }),
|
||||
true
|
||||
) as any
|
||||
).foo.bar
|
||||
).toEqual(AnyTypePlaceholder);
|
||||
});
|
||||
});
|
||||
|
@ -23,20 +23,31 @@ export function StringUnion<T extends string[]>(values: [...T], options?: any) {
|
||||
);
|
||||
}
|
||||
|
||||
function getArray(items: JSONSchema7Definition[]): JSONSchema7Type[] {
|
||||
function getArray(
|
||||
items: JSONSchema7Definition[],
|
||||
returnPlaceholderForAny = false
|
||||
): JSONSchema7Type[] {
|
||||
return items.map(item =>
|
||||
isJSONSchema(item) ? generateDefaultValueFromSpec(item) : null
|
||||
isJSONSchema(item)
|
||||
? generateDefaultValueFromSpec(item, returnPlaceholderForAny)
|
||||
: null
|
||||
);
|
||||
}
|
||||
|
||||
function getObject(spec: JSONSchema7): JSONSchema7Object {
|
||||
function getObject(
|
||||
spec: JSONSchema7,
|
||||
returnPlaceholderForAny = false
|
||||
): JSONSchema7Object {
|
||||
const obj: JSONSchema7Object = {};
|
||||
|
||||
if (spec.allOf && spec.allOf.length > 0) {
|
||||
return (getArray(spec.allOf) as JSONSchema7Object[]).reduce((prev, cur) => {
|
||||
return (getArray(spec.allOf, returnPlaceholderForAny) as JSONSchema7Object[]).reduce(
|
||||
(prev, cur) => {
|
||||
prev = Object.assign(prev, cur);
|
||||
return prev;
|
||||
}, obj);
|
||||
},
|
||||
obj
|
||||
);
|
||||
}
|
||||
|
||||
for (const key in spec.properties) {
|
||||
@ -44,7 +55,7 @@ function getObject(spec: JSONSchema7): JSONSchema7Object {
|
||||
if (typeof subSpec === 'boolean') {
|
||||
obj[key] = null;
|
||||
} else if (subSpec) {
|
||||
obj[key] = generateDefaultValueFromSpec(subSpec);
|
||||
obj[key] = generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
@ -58,10 +69,11 @@ export function generateDefaultValueFromSpec(
|
||||
if ((spec.anyOf && spec.anyOf!.length > 0) || (spec.oneOf && spec.oneOf.length > 0)) {
|
||||
const subSpec = (spec.anyOf! || spec.oneOf)[0];
|
||||
if (typeof subSpec === 'boolean') return null;
|
||||
return generateDefaultValueFromSpec(subSpec);
|
||||
return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
|
||||
}
|
||||
|
||||
// It is any type
|
||||
console.log('any', returnPlaceholderForAny);
|
||||
if (returnPlaceholderForAny) {
|
||||
return AnyTypePlaceholder;
|
||||
}
|
||||
@ -77,7 +89,7 @@ export function generateDefaultValueFromSpec(
|
||||
const subSpec = {
|
||||
type: spec.type[0],
|
||||
} as JSONSchema7;
|
||||
return generateDefaultValueFromSpec(subSpec);
|
||||
return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
|
||||
}
|
||||
case spec.type === 'string':
|
||||
if (spec.enum && spec.enum.length > 0) {
|
||||
@ -90,16 +102,16 @@ export function generateDefaultValueFromSpec(
|
||||
case spec.type === 'array':
|
||||
return spec.items
|
||||
? Array.isArray(spec.items)
|
||||
? getArray(spec.items)
|
||||
? getArray(spec.items, returnPlaceholderForAny)
|
||||
: isJSONSchema(spec.items)
|
||||
? [generateDefaultValueFromSpec(spec.items)]
|
||||
? [generateDefaultValueFromSpec(spec.items, returnPlaceholderForAny)]
|
||||
: null
|
||||
: [];
|
||||
case spec.type === 'number':
|
||||
case spec.type === 'integer':
|
||||
return 0;
|
||||
case spec.type === 'object':
|
||||
return getObject(spec);
|
||||
return getObject(spec, returnPlaceholderForAny);
|
||||
case spec.type === 'null':
|
||||
return null;
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user