You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In CustomizedValidator implementation, LocalValues reflects the change requests and Resource reflects the object's last state as expected, but the OriginalValues doesn't get filled which makes it impossible to compare old and new data for validation purposes.
publicclassCustomizedValidator:IChangeSetItemValidator{privateIChangeSetItemValidatorInner{get;set;}public Task ValidateChangeSetItemAsync(SubmitContextcontext,ChangeSetItemitem,Collection<ChangeSetItemValidationResult>validationResults,CancellationTokencancellationToken){if(item is not DataModificationItem dataModificationItem){return Inner.ValidateChangeSetItemAsync(context, item, validationResults, cancellationToken);}objectentity= dataModificationItem.Resource;boolisNewRequest= dataModificationItem.OriginalValues ==null&& dataModificationItem.ResourceKey ==null;boolisUpdateRequest= dataModificationItem.OriginalValues !=null&& dataModificationItem.LocalValues !=null;boolisDeleteRequest= dataModificationItem.LocalValues ==null;if(isUpdateRequest&& entity is Organization organization){
dataModificationItem.LocalValues.TryGetValue("ParentId",outobject localValueParentId);
dataModificationItem.OriginalValues.TryGetValue("ParentId",outobject originalValueParentId);boolchangeExists= localValueParentId is not null&& originalValueParentId is not null&&localValueParentId!=originalValueParentId;if(organization.ParentId is not null&&!changeExists){varchangeSetValidationResult=new ChangeSetItemValidationResult
{Message="You can not change this record's `ParentId`. It is not `null`.",Severity= EventLevel.Error,PropertyName= dataModificationItem.ResourceSetName + dataModificationItem.ResourceKey,Target=entity};
validationResults.Add(changeSetValidationResult);}using(varservice= context.GetApiService<SmartProcureApi>()){varexistingRecordParentId= service.DbContext.Organizations.Where(o => o.Id.Equals(organization.Id)).FirstOrDefault()?.ParentId;if(existingRecordParentId!=null&& organization.ParentId!=existingRecordParentId){}}}return Inner.ValidateChangeSetItemAsync(context, item, validationResults, cancellationToken);}}
The text was updated successfully, but these errors were encountered:
I'm not sure about why they designed it this way. The only way to make OriginalValues work in the pipeline is to load up the original object from the database, and then use the submitted data to then play the modifications and understand the differences.
This is the first implementation I've seen of anyone trying to use it the way it was designed. In my company's libraries, we do this sort of validation through the OnInserting/OnUpdating methods.
I would be interested in seeing if this architecture makes sense, because adding a database lookup on every call would slow the response rate down quite a bit.
In
CustomizedValidator
implementation,LocalValues
reflects the change requests andResource
reflects the object's last state as expected, but theOriginalValues
doesn't get filled which makes it impossible to compare old and new data for validation purposes.csproj
startup.cs
CustomizedValidator.cs
The text was updated successfully, but these errors were encountered: