SapphireDb logo SapphireDb

Model validation

SapphireDb performs model validation when performing the operations add and update.

Example

\f:(typescript:Angular) export class ModelValidationComponent implements OnInit {\n \tcollection: DefaultCollection<any>;\n \tvalues$: Observable<any[]>;\n\n \tform: FormGroup;\n\n \tconstructor(private db: SapphireDbService) { }\n\n \tngOnInit() {\n \t\tthis.collection = this.db.collection('demo.validationDemos');\n \t\tthis.values$ = this.collection.values();\n\n \t\tthis.form = new M4FormGroup('demo_form', {\n \t\t\tid: new FormControl(null),\n \t\t\tusername: new FormControl(''),\n \t\t\temail: new FormControl(''),\n \t\t\tpassword: new FormControl('')\n \t\t});\n \t}\n\n \tresetForm() {\n \t\tthis.form.patchValue({\n \t\t\tid: null,\n \t\t\tusername: '',\n \t\t\temail: '',\n \t\t\tpassword: ''\n \t\t});\n \t}\n\n \tsetFormValue(value: any) {\n \t\tthis.form.patchValue(value);\n \t}\n\n \tremove(value: any) {\n \t\tthis.collection.remove(value);\n \t}\n\n \tstore() {\n \t\tconst rawFormValue = this.form.getRawValue();\n\n \t\tlet result$: Observable<CommandResult<any>>;\n\n \t\tif (!!rawFormValue.id) {\n \t\t\tresult$ = this.collection.update(rawFormValue);\n \t\t} else {\n \t\t\tdelete rawFormValue.id;\n \t\t\tresult$ = this.collection.add(rawFormValue);\n \t\t}\n\n \t\tresult$.pipe(take(1)).subscribe((result: CommandResult<any>) => {\n \t\t\tif (result.hasSuccess()) {\n \t\t\t\tthis.resetForm();\n \t\t\t} else {\n \t\t\t\tObject.keys(result.validationResults).forEach(key => {\n \t\t\t\tconst errors = {};\n \t\t\t\t\tresult.validationResults[key].forEach(error => errors[error] = true);\n \t\t\t\t\tthis.form.get(key).setErrors(errors);\n \t\t\t\t});\n \t\t\t}\n \t\t});\n \t}\n } \f:(csharp:ValidationDemo.cs:Server) public class ValidationDemo\n {\n \t[Key]\n \tpublic Guid Id { get; set; }\n\n \t[Updatable]\n \t[Required(ErrorMessage = "username is required")]\n \t[MinLength(3, ErrorMessage = "username min length not reached")]\n \tpublic string Username { get; set; }\n\n \t[Updatable]\n \t[EmailAddress(ErrorMessage = "not a valid email address")]\n \tpublic string Email { get; set; }\n\n \t[Updatable]\n \t[Required(ErrorMessage = "password is required")]\n \t[MinLength(5, ErrorMessage = "password min length not reached")]\n \tpublic string Password { get; set; }\n }

Values

Form

This website uses Google Analytics. If you want to disable it just unselect the option: