1
1
import { applyDecorators } from '@nestjs/common' ;
2
- import { Field , FieldOptions , ObjectType } from '@nestjs/graphql' ;
2
+ import { ObjectType } from '@nestjs/graphql' ;
3
3
import { setToStringTag } from '@seedcompany/common' ;
4
4
import { markSkipClassTransformation } from '@seedcompany/nest' ;
5
5
import { IsObject } from 'class-validator' ;
@@ -9,7 +9,8 @@ import { GraphQLJSONObject } from 'graphql-scalars';
9
9
import { isEqual } from 'lodash' ;
10
10
import { JsonObject } from 'type-fest' ;
11
11
import { SecuredProperty } from '~/common/secured-property' ;
12
- import { Transform } from './transform.decorator' ;
12
+ import { InputException } from './exceptions/input.exception' ;
13
+ import { OptionalField , OptionalFieldOptions } from './optional-field' ;
13
14
14
15
function hashId ( name : string ) {
15
16
return createHash ( 'shake256' , { outputLength : 5 } ) . update ( name ) . digest ( 'hex' ) ;
@@ -76,11 +77,31 @@ export class RichTextDocument {
76
77
setToStringTag ( RichTextDocument , 'RichText' ) ;
77
78
markSkipClassTransformation ( RichTextDocument ) ;
78
79
79
- export const RichTextField = ( options ?: FieldOptions ) =>
80
+ export const RichTextField = ( options ?: OptionalFieldOptions ) =>
80
81
applyDecorators (
81
- Field ( ( ) => RichTextScalar , options ) ,
82
+ OptionalField ( ( ) => RichTextScalar , {
83
+ optional : false ,
84
+ ...options ,
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
+ } ,
103
+ } ) ,
82
104
IsObject ( ) ,
83
- Transform ( ( { value } ) => RichTextDocument . fromMaybe ( value ) ) ,
84
105
) ;
85
106
86
107
/** @internal */
0 commit comments