markusbohl / fluent-ts-validator

Fluent Validation in TypeScript
30 stars 8 forks source link

Nesting validators via fulfills - property names (paths) not nested #16

Open hidegh opened 2 years ago

hidegh commented 2 years ago

When property validation on a nested class fails, i'd expect:

  1. to have a "path" to the given property in the validation result
  2. to have also an error message related to that final property

In my case if the home address street property is failing, i'd expect:

  1. the "full" path to be homeAddress.street
  2. and also i'd expect a street property related error message: e.g. street must contain a number or street name must not be empty and same for the city

Currently what i get:

  1. no full path, just a plain propertyName: homeAddress
  2. and just a homeAddress is invalid message - without the detail on which fields (2) were failed

NOTES:

image


Given the situation below using the validator gives me 2 failures, but unfortunately on the 2nd (nested) failure it just says: Home Address is invalid.

Although this statement is true, the detail that the home address Street property that's invalid get's missing.

Also i'd expect a path property that would contain the parent object's path property extended with then failed property to be present, like: homeAddress.location.lat property is missing. Given this property it would be possible to easily match the control on the UI and ensure that the error is displayed there

NOTE: If you remove the return statement from the pesronComponent's executeValidation fn, then there's a code which emulates such errors, everything rest is done by this "framework" of mine. The only thing I miss is a VALIDATION library that's able to provide the data in the format (with that path) - the only one I've found is not working in StackBlitz and it's not supported...

Full sample: https://stackblitz.com/edit/ups-validation-sample?file=src%2Fapp%2Ftest-validation%2Fperson.module.ts,src%2Fapp%2Ftest-validation%2Fperson.component.ts,src%2Fapp%2Ftest-validation%2Fvalidator.ts

I have a model:

interface Person {
  firstName: string;
  lastName: string;
  ssn: string;

  homeAddress: Address;
  mailingAddress: Address;
}

interface Address { /* usual street, city, zip, state ... ev. location with long/lat */ }

I have created a PersonValidator and an AddressValidator

export class PersonModelValidator extends AbstractValidator<PersonModel> {
  constructor() {
    super();

    this.validateIfString((p) => p.LastName)
      .isAlphanumeric()
      .isUppercase();

    this.validateIfString((p) => p.SSN).matches(
      new RegExp('\\d{3}-\\d{2}-\\d{4}')
    );

    this.validateIf((p) => p.HomeAddress).fulfills(
      new AddressModelValidatorLocationRequired()
    );
  }
}

export class AddressModelValidatorLocationRequired extends AbstractValidator<AddressModel> {
  constructor() {
    super();

    this.validateIfString((a) => a.Street).isNotEmpty();
  }
}
hidegh commented 2 years ago

This ticket #12 also relates to fulfills vs suggested validateNested! My request (beside that described in #12) is also to have fully expanded property paths too.