-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2094 from prisma/datamodel/move-name-validation-i…
…nto-db
- Loading branch information
Showing
7 changed files
with
89 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 80 additions & 65 deletions
145
libs/datamodel/core/src/transform/ast_to_dml/reserved_model_names.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,88 @@ | ||
pub struct TypeNameValidator { | ||
reserved_names: &'static [&'static str], | ||
use crate::{ | ||
ast, | ||
diagnostics::{DatamodelError, Diagnostics}, | ||
}; | ||
|
||
pub fn is_reserved_type_name(name: &str) -> bool { | ||
RESERVED_NAMES.contains(&name) | ||
} | ||
|
||
impl TypeNameValidator { | ||
// The source of the following list is from prisma-client-js. Any edit should be done in both places. | ||
// https://github.com/prisma/prisma/blob/master/src/packages/client/src/generation/generateClient.ts#L443 | ||
pub fn new() -> Self { | ||
Self { | ||
reserved_names: &[ | ||
"PrismaClient", | ||
// JavaScript keywords | ||
"break", | ||
"case", | ||
"catch", | ||
"class", | ||
"const", | ||
"continue", | ||
"debugger", | ||
"default", | ||
"delete", | ||
"do", | ||
"else", | ||
"enum", | ||
"export", | ||
"extends", | ||
"false", | ||
"finally", | ||
"for", | ||
"function", | ||
"if", | ||
"implements", | ||
"import", | ||
"in", | ||
"instanceof", | ||
"interface", | ||
"let", | ||
"new", | ||
"null", | ||
"package", | ||
"private", | ||
"protected", | ||
"public", | ||
"return", | ||
"super", | ||
"switch", | ||
"this", | ||
"throw", | ||
"true", | ||
"try", | ||
"typeof", | ||
"var", | ||
"void", | ||
"while", | ||
"with", | ||
"yield", | ||
], | ||
} | ||
pub(crate) fn validate_model_name(ast_model: &ast::Model, diagnostics: &mut Diagnostics) { | ||
if !is_reserved_type_name(&ast_model.name.name) { | ||
return; | ||
} | ||
|
||
pub fn is_reserved<T>(&self, name: T) -> bool | ||
where | ||
T: AsRef<str>, | ||
{ | ||
self.reserved_names.contains(&name.as_ref()) | ||
} | ||
diagnostics.push_error(DatamodelError::new_model_validation_error( | ||
&format!( | ||
"The model name `{}` is invalid. It is a reserved name. Please change it. Read more at https://pris.ly/d/naming-models", | ||
&ast_model.name.name | ||
), | ||
&ast_model.name.name, | ||
ast_model.span, | ||
)) | ||
} | ||
|
||
impl Default for TypeNameValidator { | ||
fn default() -> Self { | ||
Self::new() | ||
pub(crate) fn validate_enum_name(ast_enum: &ast::Enum, diagnostics: &mut Diagnostics) { | ||
if !is_reserved_type_name(&ast_enum.name.name) { | ||
return; | ||
} | ||
|
||
diagnostics.push_error(DatamodelError::new_enum_validation_error( | ||
&format!( | ||
"The enum name `{}` is invalid. It is a reserved name. Please change it. Read more at https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-schema/data-model#naming-enums", | ||
&ast_enum.name.name | ||
), &ast_enum.name.name, | ||
ast_enum.span, | ||
)); | ||
} | ||
|
||
// The source of the following list is from prisma-client-js. Any edit should be done in both places. | ||
// https://github.com/prisma/prisma/blob/master/src/packages/client/src/generation/generateClient.ts#L443 | ||
const RESERVED_NAMES: &[&str] = &[ | ||
"PrismaClient", | ||
// JavaScript keywords | ||
"break", | ||
"case", | ||
"catch", | ||
"class", | ||
"const", | ||
"continue", | ||
"debugger", | ||
"default", | ||
"delete", | ||
"do", | ||
"else", | ||
"enum", | ||
"export", | ||
"extends", | ||
"false", | ||
"finally", | ||
"for", | ||
"function", | ||
"if", | ||
"implements", | ||
"import", | ||
"in", | ||
"instanceof", | ||
"interface", | ||
"let", | ||
"new", | ||
"null", | ||
"package", | ||
"private", | ||
"protected", | ||
"public", | ||
"return", | ||
"super", | ||
"switch", | ||
"this", | ||
"throw", | ||
"true", | ||
"try", | ||
"typeof", | ||
"var", | ||
"void", | ||
"while", | ||
"with", | ||
"yield", | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters