@@ -9,10 +9,21 @@ import {
9
9
PRIMARY_KEY ,
10
10
Value ,
11
11
} from '../glossary'
12
- import { RelationKind , RelationsList } from '../relations/Relation'
12
+ import { Relation , RelationKind , RelationsList } from '../relations/Relation'
13
13
14
14
const log = debug ( 'defineRelationalProperties' )
15
15
16
+ const getInvariantMessagePrefix = (
17
+ entity : Entity < any , any > ,
18
+ relation : Relation < any , any , any , any > ,
19
+ propertyPath : string [ ] ,
20
+ ) =>
21
+ `Failed to define a "${ relation . kind } " relationship to "${
22
+ relation . target . modelName
23
+ } " at "${ entity [ ENTITY_TYPE ] } .${ propertyPath . join ( '.' ) } " (${
24
+ entity [ PRIMARY_KEY ]
25
+ } : "${ entity [ entity [ PRIMARY_KEY ] ] } ")`
26
+
16
27
export function defineRelationalProperties (
17
28
entity : Entity < any , any > ,
18
29
initialValues : Partial < Value < any , ModelDictionary > > ,
@@ -23,12 +34,15 @@ export function defineRelationalProperties(
23
34
log ( 'defining relational properties...' , { entity, initialValues, relations } )
24
35
25
36
for ( const { propertyPath, relation } of relations ) {
37
+ const invariantMessagePrefix = getInvariantMessagePrefix (
38
+ entity ,
39
+ relation ,
40
+ propertyPath ,
41
+ )
42
+
26
43
invariant (
27
44
dictionary [ relation . target . modelName ] ,
28
- 'Failed to define a "%s" relational property to "%s" on "%s": cannot find a model by the name "%s".' ,
29
- relation . kind ,
30
- propertyPath . join ( '.' ) ,
31
- entity [ ENTITY_TYPE ] ,
45
+ `${ invariantMessagePrefix } : cannot find a model by the name "%s".` ,
32
46
relation . target . modelName ,
33
47
)
34
48
@@ -39,14 +53,14 @@ export function defineRelationalProperties(
39
53
40
54
invariant (
41
55
references !== null || relation . attributes . nullable ,
42
- 'Failed to define a "%s" relationship to "%s" at "%s.%s" (%s: "%s"): cannot set a non-nullable relationship to null.' ,
56
+ `${ invariantMessagePrefix } : cannot set a non-nullable relationship to null.` ,
57
+ )
43
58
44
- relation . kind ,
45
- relation . target . modelName ,
46
- entity [ ENTITY_TYPE ] ,
47
- propertyPath . join ( '.' ) ,
48
- entity [ PRIMARY_KEY ] ,
49
- entity [ entity [ PRIMARY_KEY ] ] ,
59
+ invariant (
60
+ relation . kind !== RelationKind . OneOf ||
61
+ references !== undefined ||
62
+ relation . attributes . nullable ,
63
+ `${ invariantMessagePrefix } : a value must be provided for a non-nullable relationship.` ,
50
64
)
51
65
52
66
log (
0 commit comments