Field validation error when using object reference
See original GitHub issueExpected behavior
Create item (document) using __ref
, when model defined with required: true
and without many: true
.
Actual behavior
Error occurs when applying an update.
{ [ValidationError: Post validation failed]
message: 'Post validation failed',
name: 'ValidationError',
errors:
{ category:
{ [ValidatorError: Path `category` is required.]
message: 'Path `category` is required.',
name: 'ValidatorError',
properties: [Object],
kind: 'required',
path: 'category',
value: undefined } },
model: 'Post',
data:
{ name: 'A draft post',
category: 'keystone',
__doc: { _id: 574e9e74a7f5369c84ac372c, name: 'A draft post' } } }
Steps to reproduce the behavior
Apply update using models and update script below.
// models/Post.js
var keystone = require('keystone');
var Types = keystone.Field.Types;
var Post = new keystone.List('Post', {
autokey: { path: 'slug', from: 'name', unique: true }
});
Post.add({
name: { type: String, required: true },
category: {
type: Types.Relationship,
ref: 'PostCategory',
initial: true,
required: true
}
});
Post.register();
// models/PostCategory.js
var keystone = require('keystone');
var PostCategory = new keystone.List('PostCategory', {
autokey: { from: 'name', path: 'key', unique: true }
});
PostCategory.add({
name: { type: String, required: true }
});
PostCategory.relationship({ ref: 'Post', path: 'category' });
PostCategory.register();
// updates/0.0.1.js
exports.create = {
PostCategory: [{
name: 'Keystone JS',
__ref: 'keystone'
}],
Post: [{
name: 'A draft post',
category: 'keystone'
}]
};
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
An object reference is required for the non-static field, method ...
I have tried this method as static but i am getting this error Keyword 'this' is not valid in a static property, static...
Read more >Error 'You have reached the maximum number of 15 object ...
This error occurs because you have reached the limit of spanning relationships (also known as cross-object reference) per object. What is a ...
Read more >GridView - The "Object reference not set to an instance of ...
Get validation error on Memo field when attempting to remove all data from column. This only occurs on Memo columns that have had...
Read more >Model instance reference - Django documentation
fields or Meta.exclude ). Doing so will raise a ValueError because the validation error won't be able to be associated with the excluded ......
Read more >Validation Errors when saving new Validation rule
Reaching 10 reference on object · What is the meaning of this message about reference limits in formula fields? · You have reached...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is one way to handle it, but it requires changing data a little bit - it isn’t very dynamic so it won’t handle child refs but it could probably be improved easy enough
The root cause is likely to be non-trivial to solve, so for anyone needing to press forward, a possible workaround is to have two update scripts, the first of which adds items to be depended upon, the second as a function that retrieves the items by their reference, before saving the new items.