npm fluentvalidation-ts 2.0.0
v2.0.0

latest releases: 3.2.0, 3.1.0, 3.0.2...
4 years ago

This release overhauls the target type of TModel in rules and conditions that are part of a .ruleForEach rule chain.

Previously the parameter of type TModel in such rule/condition definitions referred to the array property itself, but this was not of much use and went against the intuitive expectation that it would refer to the base model. As a result, the parameter of type TModel is now the base model itself, as opposed to the array property.

More specifically, this change affects the following rules and conditions:

  • .must
  • .setValidator
  • .when
  • .unless

💥 Breaking changes

Note that this is a breaking change as any rules/conditions defined within a .ruleForEach rule chain which make use of the TModel parameter will need to be modified. See the examples below for some guidance on how to migrate existing code.

Previous behaviour

An example based on the old behaviour might look something like this:

import { Validator } from 'fluentvalidation-ts';

type ExampleModel = {
  arrayProperty: Array<number>;
};

class ExampleValidator extends Validator<ExampleModel> {
  constructor() {
    super();

    this.ruleForEach('arrayProperty')
      .must((item, array) => item.displayIndex < array.length);
  }
}

As you can see, the second argument to the .must rule is the value of arrayProperty, rather than the value of the entire model.

New behaviour

An example based on the new behaviour might look something like this:

import { Validator } from 'fluentvalidation-ts';

type ExampleModel = {
  arrayProperty: Array<number>;
};

class ExampleValidator extends Validator<ExampleModel> {
  constructor() {
    super();

    this.ruleForEach('arrayProperty')
      .must((item, model) => item.displayIndex < model.arrayProperty.length);
  }
}

As you can see, the second argument to the .must rule is now the value of the entire model, but we can easily drill down into the array property if we need to.

Don't miss a new fluentvalidation-ts release

NewReleases is sending notifications on new releases.