Skip to content

Instantly share code, notes, and snippets.

@buildmotion
Created August 28, 2021 20:04
Show Gist options
  • Save buildmotion/612749535590cb6d6789616453c8cdf3 to your computer and use it in GitHub Desktop.
Save buildmotion/612749535590cb6d6789616453c8cdf3 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 {Range} from './index';
/**
* Use this rule to validate a string target. A valid string is not null or undefined; and it
* is within the specified minimum and maximum length.
*/
export class StringIsNotNullEmptyRange extends CompositeRule {
maxLength: number;
minLength: number;
target: Primitive;
/**
* The constructor for the [StringIsNotNullEmptyRangeRule].
* @param name: The name of the rule.
* @param message: The message to display when the rule is violated.
* @param target: The target that the rule(s) will be evaluated against.
* @param minLength: The minimum allowed length of the target value.
* @param maxLength: The maximum allowed length of the target value.
*/
constructor(name: string, message: string, target: Primitive, minLength: number, maxLength: number, isDisplayable: boolean = false) {
super(name, message, isDisplayable);
this.target = target;
this.minLength = minLength;
this.maxLength = maxLength;
this.configureRules();
}
/**
* A helper method to configure/add rules to the validation context.
*/
configureRules() {
this.rules.push(new IsNotNullOrUndefined('StringIsNotNull', 'The string target is null or undefined.', this.target));
if (this.target != null) {
this.rules.push(new Range('TargetLengthIsWithinRange', 'The string value is not within the specified range.', this.target.toString().length, this.minLength, this.maxLength));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment