fix(validator): add AnyTypePlaceholder for nester object

This commit is contained in:
Bowen Tan 2022-07-26 18:07:42 +08:00
parent 7f2fdd5f63
commit d21e1526f6
2 changed files with 33 additions and 13 deletions

View File

@ -93,5 +93,13 @@ describe('generateDefaultValueFromSpec function', () => {
it('can parse any type', () => { it('can parse any type', () => {
expect(generateDefaultValueFromSpec({})).toEqual(''); expect(generateDefaultValueFromSpec({})).toEqual('');
expect(generateDefaultValueFromSpec({}, true)).toEqual(AnyTypePlaceholder); expect(generateDefaultValueFromSpec({}, true)).toEqual(AnyTypePlaceholder);
expect(
(
generateDefaultValueFromSpec(
Type.Object({ foo: Type.Object({ bar: Type.Any() }) }),
true
) as any
).foo.bar
).toEqual(AnyTypePlaceholder);
}); });
}); });

View File

@ -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 => 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 = {}; const obj: JSONSchema7Object = {};
if (spec.allOf && spec.allOf.length > 0) { 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 = Object.assign(prev, cur); (prev, cur) => {
return prev; prev = Object.assign(prev, cur);
}, obj); return prev;
},
obj
);
} }
for (const key in spec.properties) { for (const key in spec.properties) {
@ -44,7 +55,7 @@ function getObject(spec: JSONSchema7): JSONSchema7Object {
if (typeof subSpec === 'boolean') { if (typeof subSpec === 'boolean') {
obj[key] = null; obj[key] = null;
} else if (subSpec) { } else if (subSpec) {
obj[key] = generateDefaultValueFromSpec(subSpec); obj[key] = generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
} }
} }
return obj; return obj;
@ -58,10 +69,11 @@ export function generateDefaultValueFromSpec(
if ((spec.anyOf && spec.anyOf!.length > 0) || (spec.oneOf && spec.oneOf.length > 0)) { if ((spec.anyOf && spec.anyOf!.length > 0) || (spec.oneOf && spec.oneOf.length > 0)) {
const subSpec = (spec.anyOf! || spec.oneOf)[0]; const subSpec = (spec.anyOf! || spec.oneOf)[0];
if (typeof subSpec === 'boolean') return null; if (typeof subSpec === 'boolean') return null;
return generateDefaultValueFromSpec(subSpec); return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
} }
// It is any type // It is any type
console.log('any', returnPlaceholderForAny);
if (returnPlaceholderForAny) { if (returnPlaceholderForAny) {
return AnyTypePlaceholder; return AnyTypePlaceholder;
} }
@ -77,7 +89,7 @@ export function generateDefaultValueFromSpec(
const subSpec = { const subSpec = {
type: spec.type[0], type: spec.type[0],
} as JSONSchema7; } as JSONSchema7;
return generateDefaultValueFromSpec(subSpec); return generateDefaultValueFromSpec(subSpec, returnPlaceholderForAny);
} }
case spec.type === 'string': case spec.type === 'string':
if (spec.enum && spec.enum.length > 0) { if (spec.enum && spec.enum.length > 0) {
@ -90,16 +102,16 @@ export function generateDefaultValueFromSpec(
case spec.type === 'array': case spec.type === 'array':
return spec.items return spec.items
? Array.isArray(spec.items) ? Array.isArray(spec.items)
? getArray(spec.items) ? getArray(spec.items, returnPlaceholderForAny)
: isJSONSchema(spec.items) : isJSONSchema(spec.items)
? [generateDefaultValueFromSpec(spec.items)] ? [generateDefaultValueFromSpec(spec.items, returnPlaceholderForAny)]
: null : null
: []; : [];
case spec.type === 'number': case spec.type === 'number':
case spec.type === 'integer': case spec.type === 'integer':
return 0; return 0;
case spec.type === 'object': case spec.type === 'object':
return getObject(spec); return getObject(spec, returnPlaceholderForAny);
case spec.type === 'null': case spec.type === 'null':
return null; return null;
default: default: