Skip to content

Instantly share code, notes, and snippets.

@buildmotion
Created August 28, 2021 20:06
Show Gist options
  • Save buildmotion/556edd54918afcbd8a371617216c6627 to your computer and use it in GitHub Desktop.
Save buildmotion/556edd54918afcbd8a371617216c6627 to your computer and use it in GitHub Desktop.
import dCompareResult = require('typescript-dotnet-commonjs/System/CompareResult');
import CompareResult = dCompareResult.CompareResult;
import dCompare = require('typescript-dotnet-commonjs/System/Compare');
import Compare = dCompare;
import {CompositeRule} from './index';
import {RuleResult} from './RuleResult';
import {Primitive} from './index';
import {IsNotNullOrUndefined} from './index';
import {Min} from './index';
import {Max} from './index';
/**
* Use this rule to determine if the specified target is within the specified range (start and end) values.
*
* The range values are inclusive.
*
* Ex: 1 is within 1 and 3. The target is valid.
* Ex: 2 is within 1 and 3. The target is valid.
* Ex: 0 is not within 1 and 3. The target is not valid.
* Ex: 4 is not within 1 and 3. The target is not valid.
*/
export class Range extends CompositeRule {
end: number;
start: number;
target: Primitive;
/**
* Constructor for the [Range] rule.
* @param name: The name of the rule.
* @param message: A message to display if the rule is violated.
* @param target: The target object that the rules will be applied to.
* @param start: The start range value - the lowest allowed boundary value.
* @param end: The end range value - the highest allowed boundary value.
* @param isDisplayable: Indicates if the rule violation may be displayed or visible to the caller or client.
*/
constructor(name: string, message: string, target: Primitive, start: number, end: number, isDisplayable: boolean = false) {
super(name, message, isDisplayable);
this.target = target;
this.start = start;
this.end = end;
this.isDisplayable = isDisplayable;
this.rules.push(new IsNotNullOrUndefined('TargetIsNotNull', 'The target is null or undefined.', this.target));
if (this.target != null) {
this.rules.push(new Min('MinValue', 'The value must be equal to or greater than the start range value.', this.target, this.start));
this.rules.push(new Max('MaxValue', 'The value must be equal to or less than the end range value.', this.target, this.end));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment