Skip to content

Commit 95ace46

Browse files
committed
Fix #88: Don't crash when data is null for object or array
1 parent 713cc8d commit 95ace46

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/data.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ export function getBlankData(schema, getRef) {
198198

199199

200200
function getSyncedArray(data, schema, getRef) {
201+
if (data === null)
202+
data = [];
203+
201204
if (actualType(data) !== 'array')
202205
throw new Error("Schema expected an 'array' but the data type was '" + actualType(data) + "'");
203206

@@ -260,6 +263,9 @@ function getSyncedArray(data, schema, getRef) {
260263

261264

262265
function getSyncedObject(data, schema, getRef) {
266+
if (data === null)
267+
data = {};
268+
263269
if (actualType(data) !== 'object')
264270
throw new Error("Schema expected an 'object' but the data type was '" + actualType(data) + "'");
265271

src/editorState.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class EditorState {
2525
if (typeof data === 'string' && data !== '')
2626
data = JSON.parse(data);
2727

28-
if (!data) {
28+
if (!data && data !== null) {
2929
// create empty data from schema
3030
data = getBlankData(schema, (ref) => EditorState.getRef(ref, schema));
3131
} else {

src/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export function getArrayFormRow(args) {
220220

221221
let removable = true;
222222
let min_items = getKeyword(schema, 'min_items', 'minItems') || 0;
223+
223224
if (data.length <= min_items || isReadonly)
224225
removable = false;
225226

@@ -399,7 +400,6 @@ export function getObjectFormRow(args) {
399400

400401
let keys = [...Object.keys(schema_keys)];
401402

402-
403403
if (schema.additionalProperties)
404404
keys = [...keys, ...Object.keys(data).filter((k) => keys.indexOf(k) === -1)];
405405

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export function normalizeKeyword(kw) {
163163
/* Converts custom supported keywords to standard JSON schema keywords */
164164

165165
if (Array.isArray(kw))
166-
kw = kw[0];
166+
kw = kw.find((k) => k !== 'null') || 'null';
167167

168168
switch (kw) {
169169
case 'list': return 'array';

0 commit comments

Comments
 (0)