-
Notifications
You must be signed in to change notification settings - Fork 9.3k
fix: disallow undefined where clause #21062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6ed89d4
b1053bf
4b84ced
f8f1477
7a3d909
5afeab9
7a835e6
d71ac03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { describe, it, expect } from "vitest"; | ||
|
||
import { validateWhereClause } from "./disallow-undefined-where-clause"; | ||
|
||
describe("Disallow undefined where", () => { | ||
it("validateWhereClause should throw exception when the 'in' field of where object is undefined", async () => { | ||
const where = { | ||
id: { | ||
in: undefined, | ||
}, | ||
}; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError( | ||
'The "in" value for the field "id" cannot be undefined.' | ||
); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when a field of where object is undefined", async () => { | ||
const where = { | ||
from: undefined, | ||
}; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError( | ||
'The value for the field "from" cannot be undefined.' | ||
); | ||
}); | ||
|
||
it("validateWhereClause should not throw exception when the where object does not contain undefined values", async () => { | ||
const where = { | ||
id: { | ||
in: [1, 2, 3], | ||
}, | ||
name: "test name", | ||
}; | ||
|
||
validateWhereClause(where); | ||
}); | ||
|
||
it("validateWhereClause should not throw exception when the where object contain null values", async () => { | ||
const where = { | ||
teamId: null, | ||
parentId: null, | ||
}; | ||
|
||
validateWhereClause(where); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when the where object contain null values and 'in' field is undefined", async () => { | ||
const where = { | ||
teamId: null, | ||
parentId: null, | ||
id: { | ||
in: undefined, | ||
}, | ||
}; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError( | ||
'The "in" value for the field "id" cannot be undefined.' | ||
); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when the where object is undefined", async () => { | ||
const where = undefined; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError('The "where" clause cannot be undefined.'); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when the where object is {}", async () => { | ||
const where = {}; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError('The "where" clause cannot be an empty object {}.'); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when the where object is []", async () => { | ||
const where = []; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError('The "where" clause cannot be an empty array [].'); | ||
}); | ||
|
||
it("validateWhereClause should throw exception when the 'in' field of where object is []", async () => { | ||
const where = { | ||
id: { | ||
in: [], | ||
}, | ||
}; | ||
|
||
expect(() => validateWhereClause(where)).toThrowError( | ||
'The "in" value for the field "id" cannot be an empty array [].' | ||
); | ||
}); | ||
}); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Prisma } from "@prisma/client"; | ||
|
||
export const validateWhereClause = (where: any) => { | ||
// Check if where is undefined | ||
if (where === undefined) { | ||
throw new Error('The "where" clause cannot be undefined.'); | ||
Check failure on line 6 in packages/prisma/extensions/disallow-undefined-where-clause.ts
|
||
} | ||
|
||
// Check if where is an empty object | ||
if (typeof where === "object" && !Array.isArray(where) && Object.keys(where || {}).length === 0) { | ||
throw new Error('The "where" clause cannot be an empty object {}.'); | ||
} | ||
|
||
// Check if where is an empty array | ||
if (Array.isArray(where) && where.length === 0) { | ||
throw new Error('The "where" clause cannot be an empty array [].'); | ||
} | ||
|
||
if (where) { | ||
for (const key in where) { | ||
// INFO: Since this is for $allModels, we don't have a way to get the correct | ||
// where type | ||
const whereInput = where[key as any] as any; | ||
let message; | ||
if (whereInput === undefined) { | ||
message = `The value for the field "${key}" cannot be undefined.`; | ||
throw new Error(message); | ||
} | ||
|
||
if (whereInput === null) { | ||
continue; | ||
} | ||
|
||
if (whereInput.hasOwnProperty("in") && typeof whereInput.in === "undefined") { | ||
message = `The "in" value for the field "${key}" cannot be undefined.`; | ||
throw new Error(message); | ||
} | ||
|
||
if (whereInput.hasOwnProperty("in") && Array.isArray(whereInput.in) && whereInput.in.length === 0) { | ||
message = `The "in" value for the field "${key}" cannot be an empty array [].`; | ||
throw new Error(message); | ||
Check failure on line 41 in packages/prisma/extensions/disallow-undefined-where-clause.ts
|
||
} | ||
} | ||
} | ||
}; | ||
|
||
export function disallowUndefinedWhereExtension() { | ||
return Prisma.defineExtension({ | ||
query: { | ||
$allModels: { | ||
async deleteMany({ args, query }) { | ||
validateWhereClause(args.where); | ||
return query(args); | ||
}, | ||
async updateMany({ args, query }) { | ||
validateWhereClause(args.where); | ||
return query(args); | ||
}, | ||
async findMany({ args, query }) { | ||
validateWhereClause(args.where); | ||
return query(args); | ||
}, | ||
}, | ||
}, | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added new test