This website is in progress, you can read the docs from old onefrom Here →
ts-validation
How To Validate?

How To Validate Objects?

When validating objects, ensuring they conform to the expected schema is essential. Here, we present three methods for handling validation results, each offering a different approach to error handling and result management.

1. validate(): Simple Boolean Check

The validate() method returns a boolean value indicating whether the validation passed or failed. This method is straightforward and suitable when only the validation outcome is needed, without detailed information about the validation results.

const result = userValidator.validate();
 
if (result) {
  console.log('Validation passed!');
} else {
  console.log('Validation failed.');
}

2. validateInfo(): Detailed Validation Results

The validateInfo() method returns an object with success and results properties. These results are of type ValidatorResult, providing detailed information about why the validation failed, enabling precise error handling.

const validationResult = userValidator.validateInfo();
 
if (validationResult.success) {
  console.log('Validation passed!');
} else {
  console.log('Validation failed.');
  console.log('Validation errors:', validationResult.results);
}

3. validateThrow(): Throwing Errors on Validation Failure

The validateThrow() method throws a BshValidationError error if the validation fails. This method is useful for handling validation errors using exception handling mechanisms.

try {
  userValidator.validateThrow();
  console.log('Validation passed!');
} catch (e) {
  const validationError = e as BshValidationError;
  console.log('Validation failed.');
  console.log('Validation errors:', validationError.results);