Skip to content

Commit a24814c

Browse files
committed
Handle empty RichText documents edge case
1 parent 906f47d commit a24814c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/common/rich-text.scalar.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { GraphQLJSONObject } from 'graphql-scalars';
99
import { isEqual } from 'lodash';
1010
import { JsonObject } from 'type-fest';
1111
import { SecuredProperty } from '~/common/secured-property';
12+
import { InputException } from './exceptions/input.exception';
1213
import { OptionalField, OptionalFieldOptions } from './optional-field';
1314

1415
function hashId(name: string) {
@@ -81,9 +82,26 @@ export const RichTextField = (options?: OptionalFieldOptions) =>
8182
OptionalField(() => RichTextScalar, {
8283
optional: false,
8384
...options,
84-
transform: RichTextDocument.fromMaybe,
85+
transform: (value) => {
86+
const doc = RichTextDocument.fromMaybe(value);
87+
if (doc) {
88+
return doc;
89+
}
90+
if (options?.nullable) {
91+
return null;
92+
}
93+
if (options?.optional) {
94+
return undefined;
95+
}
96+
// Should never _really_ get here.
97+
// UI should understand & send null instead of an empty document.
98+
// Would prefer this to be done with validators.
99+
// But I believe this needs to `null`s to be validated.
100+
// skipMissingProperties -> skipUndefinedProperties
101+
throw new InputException('RichText must be given');
102+
},
85103
}),
86-
IsObject(), // TODO validate empty blocks becomes null becomes validation error
104+
IsObject(),
87105
);
88106

89107
/** @internal */

0 commit comments

Comments
 (0)