-
Notifications
You must be signed in to change notification settings - Fork 56
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
Consider ModelNode.Validate<TDefinition>(Func<TDefinition, bool> callback) method #882
Comments
First draft for the fluent validation API: var model = SPMeta2Model.NewSiteModel(site =>
{
site
.AddField(new FieldDefinition { InternalName = "my_field1" })
.AddField(new FieldDefinition { InternalName = "my_field2" })
.AddField(new FieldDefinition { InternalName = "my_field3" });
});
// all fields have "my_" prefix
// fieldPrefixValidationResult contains a set of 'validation context' per every model node (definition) in the model
var fieldPrefixValidationResult = model.Validate<FieldDefinition>(context =>
{
// context exposes three pair of props to get current, parent and child model node / definition
// IsValid and Message is other props for the validation result
// context.ValidationContext.ChildDefinitions
// context.ValidationContext.ChildModelNodes
// context.ValidationContext.CurrentDefinition
// context.ValidationContext.CurrentModelNode
// context.ValidationContext.ParentDefinition
// context.ValidationContext.ParentModelNode
// context.IsValid
// context.Message
context.IsValid = context.ValidationContext.CurrentDefinition.InternalName.StartsWith("my_");
});
Assert.IsNotNull(fieldPrefixValidationResult);
Assert.AreEqual(true, fieldPrefixValidationResult.IsValid);
// all fields have unique GUID
var uniqueFieldIdValidationResult = model.Validate<SiteDefinition>(context =>
{
var allSiteFields = context.ValidationContext.GetChildDefinitions<FieldDefinition>();
// all fields have non-default Ids
// and amount of unique IDs is the same as the amount of fields
context.IsValid = allSiteFields.All(f => f.Id != default(Guid))
&& allSiteFields.Select(f => f.Id).Distinct().Count() == allSiteFields.Count();
if (!context.IsValid)
{
context.Message = "Either some fields have empty ID or ID is non unique";
}
});
Assert.IsNotNull(uniqueFieldIdValidationResult); |
…llback) method #882 + added DefaultFluentModelValidationService prototype and tests
Implemented as per drafts early, added "Regression.Services.DefaultFluentModelValidationService" tests. |
Looks like in the second example ( |
Here is and improved version of second example (I check for InternalName in this case): siteFieldsModel.Validate(context =>
{
var allSiteFields = context.ValidationContext
.GetChildDefinitions<FieldDefinition>()
.Select(f => f.InternalName);
var duplicates = allSiteFields
.GroupBy(x => x)
.Where(x => x.Count() > 1)
.Select(x => x.Key)
.ToList();
duplicates.ForEach(d => Console.WriteLine("Duplicate field:" + d));
Assert.IsTrue(duplicates.Count == 0);
}); |
Alright, so that is the issue if any? |
SPMeta2 should provide an easy way to validate the model. Something like as following:
Model node should have an extension method to plug a custom validation via fun callback with the following criteria:
Most likely, a "validation context" is to be pass to validation callback so that more properties can be added later.
Related to community feedback on ListDefinition validation:
https://www.yammer.com/spmeta2feedback/#/Threads/show?threadId=759523765
The text was updated successfully, but these errors were encountered: