https://www.prisma.io/ logo
#orm-help
Title
# orm-help
a

Amgelo563

09/28/2022, 12:40 AM
I'm getting this when trying to call JSON.parse() on a fetched Json property
Copy code
TS2531: Object is possibly 'null'.
TS2345: Argument of type 'JsonValue' is not assignable to parameter of type 'string'.   Type 'null' is not assignable to type 'string'.
What confuses me is that • Wasn't JsonValue made to always support JSON.parse()? • Why would JsonValue be null if my schema says it's required? I tried to use
Copy code
JSON.parse(fetchedValue ?? '{}');
but I get
Copy code
TS2345: Argument of type 'string | number | boolean | JsonObject | JsonArray' is not assignable to parameter of type 'string'.   Type 'number' is not assignable to type 'string'.
which is related to the first mentioned point. Could someone explain to me what is going on? I probably missed something on the wiki or I did something wrong
šŸ‘€ 1
n

Nurul

09/30/2022, 2:22 PM
Hi @Amgelo563 šŸ‘‹ Valid JSON values include
null,
so I assume that even if your JsonValue field is required in the schema file, null would be considered valid.
Copy code
export declare type JsonValue =
  | string
  | number
  | boolean
  | null
  | JsonObject
  | JsonArray
a

Amgelo563

10/01/2022, 12:59 AM
Is there a way I could "enforce" Prisma to return an actual object by parsing it or something? I'm not really saving numbers, booleans or anything like that in that field
Maybe a constraint or something like that?
n

Nurul

10/14/2022, 2:20 PM
@Amgelo563 šŸ‘‹ Apologies in delay in getting back to you. In the Middleware you can parse the response of query and validate it with
typeof
? If it is not of type object then you can throw a custom error.
a

Amgelo563

10/15/2022, 4:11 AM
Thanks! Would I be able to change the type or I have to cast it to object?
n

Nurul

10/19/2022, 12:45 PM
I think you would need to cast it to object
6 Views