Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mavilein committed May 22, 2020
1 parent 32ef1a7 commit 601b3b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/datamodel/core/src/validator/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,14 @@ impl<'a> Validator<'a> {
}
}

if rel_info.fields.len() != rel_info.to_fields.len() {
errors.push(DatamodelError::new_directive_validation_error(
&format!("You must specify the same number of fields in `fields` and `references`.",),
RELATION_DIRECTIVE_NAME,
ast_field.span.clone(),
));
}

if !fields_with_wrong_type.is_empty() && !errors.has_errors() {
// don't output too much errors
errors.append_vec(fields_with_wrong_type);
Expand Down
25 changes: 25 additions & 0 deletions libs/datamodel/core/tests/directives/relations_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,31 @@ fn relation_must_error_when_types_of_base_field_and_referenced_field_do_not_matc
errors.assert_is(DatamodelError::new_directive_validation_error("The type of the field `userId` in the model `Post` is not matching the type of the referenced field `id` in model `User`.","@relation", Span::new(204, 265)));
}

#[test]
fn relation_must_error_when_number_of_fields_and_references_is_not_equal() {
let dml = r#"
model User {
id Int @id
firstName String
posts Post[]
}
model Post {
id Int @id
userId Int
userName String
user User @relation(fields: [userId, userName], references: [id])
}
"#;

let errors = parse_error(dml);
errors.assert_is(DatamodelError::new_directive_validation_error(
"You must specify the same number of fields in `fields` and `references`.",
"@relation",
Span::new(200, 273),
));
}

#[test]
fn relation_must_succeed_when_type_alias_is_used_for_referenced_field() {
let dml = r#"
Expand Down

0 comments on commit 601b3b0

Please sign in to comment.