question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NonNullable JSON data type attribute

See original GitHub issue

Problem

Columns with data type Json can technically be nullable, even thought they are defined as not nullable in the prisma schema. However, it would be very helpful to mark Json columns as absolutely not nullable via a data type attribute.

This would prevent a lot of tedious logic in larger codebases which try to enforce a strict type-safety.

Example:

To please the typescript compiler that prisma queries really don’t return a null value (because we enforce that on data write/update operations) for json columns, we have a lot places which look like this:

const entity = await prisma.entity.findMany(...)

return {
      ...entity,
      entries: entity.entries.map((entry) => ({
        ...entry,
        someSubEntity: {
          ...entry.someSubEntity,
          attributes: entry.someSubEntity
            .attributes as Attributes, // we need to tell tsc that there are really no null values stored in the json column
        },
      })),
    }

Suggested solution

Introduction of a @jsonNotNullable data type attribute which ensures that both for write / update operations on the model no null value is written to the column and that the generated JsonValue type does not contain ... | null. E.g. the generated type would be something like JsonValueNotNullable = string | number | boolean | JsonObject | JsonArray

Alternatives

None.

Additional context

None.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Apr 8, 2022

Yes that is how I understood you now, you want to disallow a Json Null or Prisma.JsonNull for a specific field to further clarify what is expected and wanted in a column. The database will not be able to express that constraint (without getting fancy), but Prisma could definitely do that in the types and via validations or similar.

I think that sounds like a valid feature request if you can describe it in a way the majority of my colleagues will understand 😄

0reactions
yss14commented, Apr 10, 2022

@aqrln Exactly! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Optional and Nullable Properties in API Requests
OpenAPI supports values of data types to be null. ... Required Non-Nullable attributes are also a must in JSON string during serialization.
Read more >
C# 8.0 nullable references and serialization - Endjin
JSON.NET recognizes a pattern in which a type offers a constructor ... a property as non-nullable, but use a nullable backing field for...
Read more >
How to deserialize json array with nulls to non-nullable int list ...
Json.Serialization.JsonContract, System.Type) Inner Exception 1: InvalidCastException: Null object cannot be converted to a value type.
Read more >
Resolve nullable warnings | Microsoft Learn
This set of warnings alerts you that you're assigning a variable whose type is nonnullable to an expression whose null-state is maybe-null.
Read more >
Ignore Null Fields with Jackson | Baeldung
The full guide to persistence with Spring Data JPA ... Jackson - Change the name of a field to adhere to a specific...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found