Currently, I am developing a website with Angular that is linked to an ASP.Net backend for database access. The error I am encountering pertains to a custom Async Validator integrated into a Form Control. This validator serves the purpose of verifying if an input ID exists in the database before allowing form submission.
An unusual aspect is that the error is not consistently occurring during input validation; it mostly functions correctly. However, the only scenario where the error appears is when the input string begins with "0" and contains either "8" or "9" at specific positions. If the string commences with any other digit, no HTTP 400 error is thrown.
For instance, when the input is "01823", the error message reads
"{"":["Input string '01823' is not a valid number. Path '', line 1, position 5."]}"
, accompanied by "Http failure response for https://localhost:5001/api/FS/vendor: 400 Bad Request"
. The position
specified in the error message always corresponds to the last character in the string.
Despite meeting the specified criteria in the database, the error persists regardless of the existence of the input value.
Validation Procedure
// Validate Vendor ID
invalidVendor(): AsyncValidatorFn {
return (control: AbstractControl): | Observable<{ [key: string]: any } | null> | Promise<{ [key: string]: any } | null> => {
if (control.value === null || control.value === '') {
return of(null);
}
else {
return control.valueChanges.pipe(
debounceTime(500),
take(1),
switchMap(_ =>
this.dbService.checkVendor(control.value).pipe(map(v =>
v == '' ? { invalidVendor: { value: control.value } } : null)
)
)
);
}
};
}
dbService.checkVendor
// Check for valid vendor ID
public checkVendor(vendor: string) {
var httpOptions = {
headers: { 'Content-Type': 'application/json' },
responseType: 'text' as 'json'
};
return this.http.post<string>('api/FS/vendor', vendor, httpOptions);
}
FsController.ValidVendor
// POST: api/FS/vendor
[HttpPost("vendor")]
public async Task<ActionResult<string>> ValidVendor([FromBody] string vendor)
{
var result = await _fsContext.FS_Vendor.Where(x => x.VendorID.Equals(vendor)).FirstOrDefaultAsync();
return result != null ? result.VendorID : "";
}
UPDATE:
The problem has been resolved by utilizing an object instead of a string
. Nonetheless, the root cause behind this particular issue remains unknown, so I will continue investigating.