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.

Incorrect InputJsonValue type

See original GitHub issue

Bug description

Hello!

Right now JsonValue is not assignable to InputJsonValue and I think it is incorrect because I believe InputJsonValue is supposed to be a wider type.

Probably

export type InputJsonValue = string | number | boolean | InputJsonObject | InputJsonArray
// should be 
export type InputJsonValue = string | number | boolean | InputJsonObject | InputJsonArray | null

How to reproduce

Get any model that has a JSON field and try to use this field in for example in update and see

Type 'JsonValue' is not assignable to type 'InputJsonValue'.
Type 'null' is not assignable to type 'InputJsonValue'.

Expected behavior

JsonValue being assignable to InputJsonValue

Prisma information

model AnyModel {
 ...
 jsonField Json
 ...
}
const rec = prisma.anyModel.findUnique({});
prisma.anyModel.update({  data: { jsonField: rec.jsonField  } });

Environment & setup

Typescritp: 4.4.3 { "strict": true }

Prisma Version

prisma                  : 3.0.2
@prisma/client          : 3.0.2
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 2452cc6313d52b8b9a96999ac0e974d0aedf88db (at ../../node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 2452cc6313d52b8b9a96999ac0e974d0aedf88db
Studio                  : 0.423.0
Preview Features        : orderByRelation

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:21
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
initplatformcommented, Apr 29, 2022

work around using spread:

        savedUser = await prisma().user.update({
            where: {
                id: user.id,
            },
            data: {
                ...user,
                postMetaData: user.postMetaData as Prisma.JsonObject,
            },
            include: userForAuthenticationInclude,
        });

This is quite annoying… if I don’t do this I get ->

  Types of property 'postMetaData' are incompatible.
    Type 'JsonValue' is not assignable to type 'string | number | boolean | InputJsonObject | InputJsonArray | undefined'.
      Type 'null' is not assignable to type 'string | number | boolean | InputJsonObject | InputJsonArray | undefined'

Full code for context:

export const saveUser = async function (
    request: FastifyRequest,
    userForAuthentication: UserForAuthentication,
    verifyOrg = false
): Promise<UserForAuthentication> {
    if (verifyOrg) {
        verifyOrganizationInMemberships(request, userForAuthentication.memberships);
    }
    // Need to remove included relations before saving data object
    // eslint-disable-next-line
    const { auth, memberships, ...user } = userForAuthentication;
    let savedUser: UserForAuthentication | null;
    try {
        savedUser = await prisma().user.update({
            where: {
                id: user.id,
            },
            data: {
                ...user,
                postMetaData: user.postMetaData as Prisma.JsonObject,
            },
            include: userForAuthenticationInclude,
        });
    } catch (error) {
        throw request.generateError<SaveUserErrorCodes>(
            httpCodes.INTERNAL_SERVER_ERROR,
            'ERROR_SAVING_USER',
            error
        );
    }

    return savedUser;
};

and then in user.model.ts

import { Prisma } from '@prisma/client';

import { MembershipWithOrgSelect } from '#public-types/membership';

export type AuthInclude = {
    authFacebook: true;
    authGithub: true;
    authGoogle: true;
    authLocal: true;
    authTwitter: true;
};
export type UserForAuthentication = Prisma.UserGetPayload<{
    include: {
        auth: {
            include: AuthInclude;
        };
        memberships: {
            select: MembershipWithOrgSelect;
        };
    };
}>;

export const userForAuthenticationInclude = {
    auth: {
        include: {
            authFacebook: true,
            authGithub: true,
            authGoogle: true,
            authLocal: true,
            authTwitter: true,
        },
    },
    memberships: {
        select: {
            id: true,
            organizationId: true,
            organization: {
                select: {
                    name: true,
                },
            },
            role: {
                select: {
                    name: true,
                },
            },
            groups: {
                select: {
                    id: true,
                    name: true,
                },
            },
        },
    },
};
5reactions
GeeWeecommented, Dec 27, 2021

Just want to confirm this bug still exists in 3.7.0 and I just stumbled upon it today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow interface to implicitly convert to JsonData - Stack Overflow
But I get the error Type 'AttachedFile' is not assignable to type '{ [property: string]: JsonValue; }'. Index signature is missing in type...
Read more >
JSON field type invalid input - Questions - Prisma 1 Forum
Old answer: Is that in the Playground or a specific GraphQL Client? Here's an example with Lokka. In the Playground, this should work:...
Read more >
prisma is not assignable to type - You.com | The AI Search ...
I'm having an issue with this strange typescript error stating that type number is not assignable to ... prisma/prismaIncorrect InputJsonValue type#9247.
Read more >
Request model null in .NET core API if input json value integer
However, passing a telephone "number" as a number is a mistake. Despite the name, it's not a "number" in that sense.
Read more >
type-graphql/prisma2 - Gitter
Hi, I'm try use typegraphql-prisma and the generated label(Label.ts) has a bad path to the prisma client import { JsonValue, InputJsonValue } from...
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