public IEnumerable <IValidator> CreateValueValidators(ValidatorContext context, IField field, ValidatorFactory createFieldValidator) { if (context.Mode == ValidationMode.Optimized) { yield break; } var isRequired = IsRequired(context, field.RawProperties); if (field is IField <AssetsFieldProperties> assetsField) { var checkAssets = new CheckAssets(async ids => { return(await assetRepository.QueryAsync(context.AppId.Id, null, Q.Empty.WithIds(ids))); }); yield return(new AssetsValidator(isRequired, assetsField.Properties, checkAssets)); } if (field is IField <ReferencesFieldProperties> referencesField) { var checkReferences = new CheckContentsByIds(async ids => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, ids, SearchScope.All)); }); yield return(new ReferencesValidator(isRequired, referencesField.Properties, checkReferences)); } if (field is IField <NumberFieldProperties> numberField && numberField.Properties.IsUnique) { var checkUniqueness = new CheckUniqueness(async filter => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, context.SchemaId.Id, filter)); }); yield return(new UniqueValidator(checkUniqueness)); } if (field is IField <StringFieldProperties> stringField && stringField.Properties.IsUnique) { var checkUniqueness = new CheckUniqueness(async filter => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, context.SchemaId.Id, filter)); }); yield return(new UniqueValidator(checkUniqueness)); } }
public IEnumerable <IValidator> CreateValueValidators(ValidationContext context, IField field, FieldValidatorFactory createFieldValidator) { if (field is IField <AssetsFieldProperties> assetsField) { var checkAssets = new CheckAssets(async ids => { return(await assetRepository.QueryAsync(context.AppId.Id, new HashSet <Guid>(ids))); }); yield return(new AssetsValidator(assetsField.Properties, checkAssets)); } if (field is IField <ReferencesFieldProperties> referencesField) { var checkReferences = new CheckContentsByIds(async ids => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, ids, SearchScope.All)); }); yield return(new ReferencesValidator(referencesField.Properties.SchemaIds, checkReferences)); } if (field is IField <NumberFieldProperties> numberField && numberField.Properties.IsUnique) { var checkUniqueness = new CheckUniqueness(async filter => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, context.SchemaId.Id, filter)); }); yield return(new UniqueValidator(checkUniqueness)); } if (field is IField <StringFieldProperties> stringField && stringField.Properties.IsUnique) { var checkUniqueness = new CheckUniqueness(async filter => { return(await contentRepository.QueryIdsAsync(context.AppId.Id, context.SchemaId.Id, filter)); }); yield return(new UniqueValidator(checkUniqueness)); } }
public UniqueValidator(CheckUniqueness checkUniqueness) { this.checkUniqueness = checkUniqueness; }