I'm getting this when trying to call JSON.parse() ...
# orm-help
a
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
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
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
@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
Thanks! Would I be able to change the type or I have to cast it to object?
n
I think you would need to cast it to object