-
-
Notifications
You must be signed in to change notification settings - Fork 158
Feature/#252 valid modelstate #316
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
Feature/#252 valid modelstate #316
Conversation
…otNetCore into feature/#252_valid_modelstate
…into feature/#252_valid_modelstate
@@ -1,6 +1,6 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<VersionPrefix>2.3.2</VersionPrefix> | |||
<VersionPrefix>2.3.3</VersionPrefix> |
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.
Wondering if this should be a major version bump. It could break existing apps if they have annotations but aren't currently checking them. We could release this as a patch version behind a feature flag (e.g. options.ValidateModelState
) with a default disabled value. Then in the next major release enable it by default. What do you think?
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.
You could argue that if they have annotations already on their entities, then its already invalid and it's now being flagged. However, the feature flag route makes sense for a minor release :-D
Made the changes
@@ -152,6 +153,7 @@ public class BaseJsonApiController<T, TId> | |||
|
|||
if (!_jsonApiContext.Options.AllowClientGeneratedIds && !string.IsNullOrEmpty(entity.StringId)) | |||
return Forbidden(); | |||
if (!ModelState.IsValid) return BadRequest(ModelState.ConvertToErrorCollection()); |
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.
nit: return
on newline
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.
Haha no problem
ErrorCollection errors = new ErrorCollection(); | ||
foreach (var entry in modelState) | ||
{ | ||
if (!entry.Value.Errors.Any()) continue; |
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.
nit: continue
on newline
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.
Thanks! Just some minor comments on style (need a xplat style checker 🤔) and a note on versioning.
Feature/#252 valid modelstate
Closes #252
FEATURE
Addressing Breaking Change - As there are no implicit Validation providers in MVC core, you need to explicitly add validation to the pipeline e.g. services.AddMvcCore().AddDataAnnotations()
Without this ModelState should always be valid.