Skip to content

Commit

Permalink
[x] Avoid validate() to fail when there are no validation rules defined
Browse files Browse the repository at this point in the history
[x] Add generics to validation rules
[x] Export and document validation rules parameters
[x] Add some comments to validation rules for clarification
  • Loading branch information
DavidValin committed Apr 3, 2023
1 parent 73870bd commit e5b0cac
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 39 deletions.
22 changes: 17 additions & 5 deletions src/howerest.sdkzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Sdkzer<T extends SdkzerParams> {
// Reset previous invalid messages from previous validations
this.invalidMessages = {};
let toValidateAttr, validationRule;
const toValidateAttrs = Object.keys(this.validationRules);
const toValidateAttrs = Object.keys(this.validationRules || {});

// Validate attribute's ValidationRules
for(toValidateAttr of toValidateAttrs) {
Expand Down Expand Up @@ -572,8 +572,20 @@ export interface ISdkzerConfigOptions {

export { ValidationRule } from "./validation_rule";
export { RequiredValidator } from "./validation_rules/required_validator"
export { RegExpValidator } from "./validation_rules/reg_exp_validator"
export { NumberValidator } from "./validation_rules/number_validator"
export { LengthValidator } from "./validation_rules/length_validator"
export {
RegExpValidator,
IParams as IRegExpValidatorParams
} from "./validation_rules/reg_exp_validator"
export {
NumberValidator,
IParams as INumberValidatorParams
} from "./validation_rules/number_validator"
export {
LengthValidator,
IParams as ILengthValidatorParams
} from "./validation_rules/length_validator"
export { EmailValidator } from "./validation_rules/email_validator"
export { AllowedValueSwitchValidator } from "./validation_rules/allowed_value_switch_validator"
export {
AllowedValueSwitchValidator,
IParams as IAllowedValueSwitchValidatorParams
} from "./validation_rules/allowed_value_switch_validator"
4 changes: 2 additions & 2 deletions src/validation_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
--------------------------------------------------------------------------- */

export class ValidationRule implements IValidationRule {
export class ValidationRule<IParams = {}> implements IValidationRule {
protected conditions: Array<Function|any> = [];
public fromValue:any;
public toValue:any
public params:object
public params: IParams
private _invalidMessage = "Invalid";

constructor(params?:any) {
Expand Down
39 changes: 23 additions & 16 deletions src/validation_rules/allowed_value_switch_validator.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import {ValidationRule} from '../validation_rule'

export class AllowedValueSwitchValidator extends ValidationRule {
// Sample declaration of allowed transition:
export interface IParams {
allowed: Array<{
from: string;
to: any[];
}>
}

export class AllowedValueSwitchValidator extends ValidationRule<IParams> {
// Sample declaration of allowed transition:

// {
// allowed: [
// { from: "open", to: ["scheduled", "canceled", "closed"] },
// { from: "close", to: ["open"] }
// ]
// }
// {
// allowed: [
// { from: "open", to: ["scheduled", "canceled", "closed"] },
// { from: "close", to: ["open"] }
// ]
// }

protected conditions:Array<Function> = [
() => {
let match:boolean = false,
rule;

if (this.params && this.params['allowed'] && this.params['allowed'].length) {
for (let i = 0; i < this.params['allowed'].length; i++) {
rule = this.params['allowed'][i];
if (rule['from'] && rule['to'] && rule['to'].length) {
if (this.params && this.params.allowed && this.params.allowed.length) {
for (let i = 0; i < this.params.allowed.length; i++) {
rule = this.params.allowed[i];
if (rule.from && rule.to && rule.to.length) {
// Origin value matched
if (rule['from'] === this.fromValue) {
if (rule.from === this.fromValue) {
// Check that the destination value also allowed
for (let i2 = 0; i2 < rule['to'].length; i2++) {
for (let i2 = 0; i2 < rule.to.length; i2++) {
if (!match) {
if (this.toValue === rule['to'][i2]) {
if (this.toValue === rule.to[i2]) {
match = true;
}
}
Expand All @@ -34,7 +41,7 @@ export class AllowedValueSwitchValidator extends ValidationRule {
}
}
if (!match) {
this.addInvalidMessage(this.fromValue + " cannot change to '" + this.toValue + "'");
this.addInvalidMessage(`${this.fromValue} cannot change to '${this.toValue}'`);
}

return match;
Expand Down
2 changes: 1 addition & 1 deletion src/validation_rules/email_validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class EmailValidator extends ValidationRule {
let match:boolean = true;
const regExpValidator = new RegExpValidator({ rule: /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i });
if (!regExpValidator.isValid(this.fromValue, this.toValue)) {
this.addInvalidMessage(this.toValue + ' is not a valid email address');
this.addInvalidMessage(`${this.toValue} is not a valid email address`);
match = false;
}
return match;
Expand Down
15 changes: 9 additions & 6 deletions src/validation_rules/length_validator.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import {ValidationRule} from '../validation_rule'

interface LengthValidatorParams { min: number, max: number }
export interface IParams {
min: number; // min number of items in the array
max: number; // max number of items in the array
}

export class LengthValidator extends ValidationRule {
export class LengthValidator extends ValidationRule<IParams> {
protected conditions:Array<Function> = [
() => {
let match:boolean = true;

if (this.toValue.length < this.params['min']) {
if (this.toValue.length < this.params.min) {
match = false;
this.addInvalidMessage(`${this.toValue} contains less than ${this.params['min']} items`);
this.addInvalidMessage(`${this.toValue} contains less than ${this.params.min} items`);
}
if (this.toValue.length > this.params['max']) {
if (this.toValue.length > this.params.max) {
match = false;
this.addInvalidMessage(`${this.toValue} contains more than ${this.params['max']} items`);
this.addInvalidMessage(`${this.toValue} contains more than ${this.params.max} items`);
}

return match;
Expand Down
15 changes: 9 additions & 6 deletions src/validation_rules/number_validator.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import {ValidationRule} from '../validation_rule'

interface NumberValidatorParams { min: number, max: number }
export interface IParams {
min: number; // min number value allowed
max: number; // max number value allowed
}

export class NumberValidator extends ValidationRule {
export class NumberValidator extends ValidationRule<IParams> {
protected conditions:Array<Function> = [
() => {
let match:boolean = true;

if (this.toValue < this.params['min']) {
if (this.toValue < this.params.min) {
match = false;
this.addInvalidMessage(this.toValue + " is smaller than " + this.params['min']);
this.addInvalidMessage(`${this.toValue} is smaller than ${this.params.min}`);
}
if (this.toValue > this.params['max']) {
if (this.toValue > this.params.max) {
match = false;
this.addInvalidMessage(this.toValue + " is bigger than " + this.params['max']);
this.addInvalidMessage(`${this.toValue} is bigger than ${this.params.max}`);
}

return match;
Expand Down
10 changes: 7 additions & 3 deletions src/validation_rules/reg_exp_validator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import {ValidationRule} from '../validation_rule'

export class RegExpValidator extends ValidationRule {
export interface IParams {
rule: RegExp
}

export class RegExpValidator extends ValidationRule<IParams> {
protected conditions:Array<Function> = [
() => {
let match:boolean = true;
if (!this.toValue || !this.toValue.match || !this.toValue.match(this.params['rule'])) {
if (!this.toValue || !this.toValue.match || !this.toValue.match(this.params.rule)) {
match = false;
this.addInvalidMessage(this.toValue + " is not valid");
this.addInvalidMessage(`${this.toValue} is not valid`);
}
return match;
}
Expand Down

0 comments on commit e5b0cac

Please sign in to comment.