Skip to content

Commit

Permalink
Fixes JSON field type view assuming the empty string is equivalent to…
Browse files Browse the repository at this point in the history
… `null` (#7982)

Co-authored-by: Daniel Cousens <dcousens@users.noreply.github.com>
  • Loading branch information
dcousens and dcousens committed Oct 4, 2022
1 parent 8bec257 commit 138a015
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fix-json-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Fixes JSON field type view assuming the empty string is equivalent to `null`
15 changes: 7 additions & 8 deletions packages/core/src/fields/types/json/views/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,17 @@ export const controller = (config: Config): FieldController<string, string> => {
},
deserialize: data => {
const value = data[config.path];
if (!value) return '';
// null is equivalent to Prisma.DbNull, and we show that as an empty input
if (value === null) return '';
return JSON.stringify(value, null, 2);
},
serialize: value => {
let parsedValue;
if (!value) {
return { [config.path]: null };
}
if (!value) return { [config.path]: null };
try {
parsedValue = JSON.parse(value);
} catch (e) {}
return { [config.path]: parsedValue };
return { [config.path]: JSON.parse(value) };
} catch (e) {
return { [config.path]: undefined };
}
},
};
};

1 comment on commit 138a015

@vercel
Copy link

@vercel vercel bot commented on 138a015 Oct 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.