Skip to content

Commit 4ce1a12

Browse files
committed
Fixes for object no properties, array no items
1 parent 2e7b9c2 commit 4ce1a12

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/components/schema-form-array.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function SchemaFormArray({
1818
}: ISchemaContainerProps): React.ReactElement {
1919
const dispatch = useContext(ValueDispatch);
2020
const [ collapsed, setCollapsed ] = useState(false);
21-
const itemSchema = schema['items'];
21+
const itemSchema = schema['items'] || {};
2222
const valueArray = Array.isArray(value) ? value as any[] : [];
2323
const pathEl = path.length ? _.last(path) : '';
2424
const arrayClass = path.length === 0 ? "" : "sf-array sf-" + pathEl;

src/components/schema-form-object.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ export function SchemaFormObject({
6060
return <></>;
6161
}
6262

63-
let topOrder: NestedListArray = schema['order'] || Object.keys(schema['properties']);
64-
let properties = Object.entries(schema['properties']);
63+
let topOrder: NestedListArray = schema['order'] || Object.keys(schema['properties'] || {});
64+
let properties = Object.entries(schema['properties'] || {});
6565
let requireds = schema['required'];
6666
if (schema['order'] && _.flatten(schema['order']).length < properties.length) {
6767
console.log('fewer items in order than properties at ' + path.join('.'));

src/editors/multi-select-buttons-editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function MultiSelectButtonsEditor(props: ISchemaContainerProps): React.Re
3939
const classes = `sf-control sf-check-buttons ${isError && 'sf-has-error'}`;
4040
const readOnly = schema['readOnly'] || false;
4141
const baseProps = { name, readOnly, onFocus: handleFocus, onBlur: handleBlur };
42-
const enums = schema['items']['enum'] as string[];
42+
const enums = schema?.['items']['enum'] as string[];
4343
if (!enums || schema['type'] !== 'array') {
4444
throw(`In schema ${JSON.stringify(schema)}, editor: checkButtons must be an array type with items an enum property`);
4545
}

src/schema/schema.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ function nullOptionalsAllowedApply(schema: object) {
7070
if (schema['$ref']) return;
7171
switch (schema['type']) {
7272
case 'object':
73-
for (let prop in schema['properties']) {
73+
const properties = schema['properties'] || {};
74+
for (let prop in properties) {
7475
if (req.indexOf(prop) < 0) {
7576
nullOptionalsAllowedApply(schema['properties'][prop]);
7677
}
7778
}
7879
break;
7980
case 'array':
80-
const items = schema['items'];
81+
const items = schema['items'] || {};
8182
nullOptionalsAllowedApply(items);
8283
if (items['oneOf'] && !(items['oneOf'] as any[]).some(subschema => subschema["type"] == "null")) {
8384
items['oneOf'].push({ type: 'null' });
@@ -226,7 +227,7 @@ export function disjoin(schema0: object | null, schema1: object | null ): object
226227
case 'properties':
227228
let deleteProps = [];
228229
for (let p in schema['properties']) {
229-
let otherProp = schema1['properties'][p] || null;
230+
let otherProp = schema1?.['properties']?.[p] || null;
230231
if (otherProp === null) {
231232
deleteProps.push(p);
232233
} else {
@@ -321,7 +322,7 @@ export function fieldUnion(baseSchema: object, schema: object | null): object |
321322
};
322323
switch (schema['type']) {
323324
case "object":
324-
let props = schema['properties'];
325+
let props = schema['properties'] || {};
325326
for (let field in props) {
326327
union[field] = fieldUnion(baseSchema, props[field]);
327328
}
@@ -489,7 +490,7 @@ export const deleteSubschemaProperties = (value: any, schema: object): any => {
489490
}
490491
return value === {} ? null : value;
491492
} else if (schemaType === 'array' && Array.isArray(value)) {
492-
return (value as any[]).map(item => deleteSubschemaProperties(item, schema['items'])).filter((item: any) => item !== null);
493+
return (value as any[]).map(item => deleteSubschemaProperties(item, schema['items'] || {})).filter((item: any) => item !== null);
493494
} else {
494495
return null;
495496
}

0 commit comments

Comments
 (0)