Skip to content

Commit

Permalink
Smaller bundle - next bunch of migrations...
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Linder committed Nov 13, 2018
1 parent 1320d29 commit 3907863
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 151 deletions.
3 changes: 2 additions & 1 deletion sample/sample1-simple-validation/Post.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Contains, MinLength, MaxLength, IsEmail, IsFQDN, ArrayNotEmpty, ArrayMinSize, ArrayMaxSize} from "../../src/decorator/decorators";
import {MinLength, MaxLength, IsEmail, IsFQDN, ArrayNotEmpty, ArrayMinSize, ArrayMaxSize} from "../../src/decorator/decorators";
import {IsDate} from "../../src/decorator/IsDate";
import {IsInt} from "../../src/decorator/IsInt";
import {IsEnum} from "../../src/decorator/IsEnum";
import {Contains} from "../../src/decorator/Contains";

export enum PostType {
Public,
Expand Down
3 changes: 2 additions & 1 deletion sample/sample2-using-groups/Post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Contains, Length, IsEmail, IsFQDN} from "../../src/decorator/decorators";
import {Length, IsEmail, IsFQDN} from "../../src/decorator/decorators";
import {IsDate} from "../../src/decorator/IsDate";
import {IsInt} from "../../src/decorator/IsInt";
import {Contains} from "../../src/decorator/Contains";

export class Post {

Expand Down
3 changes: 2 additions & 1 deletion sample/sample7-inheritance-support/Post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Contains, MinLength, MaxLength} from "../../src/decorator/decorators";
import {MinLength, MaxLength} from "../../src/decorator/decorators";
import {BaseContent} from "./BaseContent";
import {IsInt} from "../../src/decorator/IsInt";
import {Contains} from "../../src/decorator/Contains";

export class Post extends BaseContent {

Expand Down
25 changes: 25 additions & 0 deletions src/decorator/Contains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {buildMessage, ValidateBy} from "./ValidateBy";
import {ValidationOptions} from "./ValidationOptions";
import validatorJsContains = require("validator/lib/contains");

/**
* Checks if the string contains the seed.
* If given value is not a string, then it returns false.
*/
export function contains(value: string, seed: string): boolean {
return typeof value === "string" && validatorJsContains(value, seed);
}

/**
* Checks if the string contains the seed.
*/
export function Contains(seed: string, validationOptions?: ValidationOptions) {
return ValidateBy({
name: "contains",
validate: (value, args) => contains(value, args.constraints[0]),
constraints: [seed],
defaultMessage: buildMessage((eachPrefix) => eachPrefix + "$property must contain a $constraint1 string", validationOptions),
},
validationOptions
);
}
24 changes: 24 additions & 0 deletions src/decorator/IsBooleanString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {buildMessage, ValidateBy} from "./ValidateBy";
import {ValidationOptions} from "./ValidationOptions";
import validatorJsIsBoolean = require("validator/lib/isBoolean");

/**
* Checks if a string is a boolean.
* If given value is not a string, then it returns false.
*/
export function isBooleanString(value: string): boolean {
return typeof value === "string" && validatorJsIsBoolean(value);
}

/**
* Checks if a string is a boolean.
*/
export function IsBooleanString(validationOptions?: ValidationOptions) {
return ValidateBy({
name: "isBooleanString",
validate: (value) => isBooleanString(value),
defaultMessage: buildMessage((eachPrefix) => eachPrefix + eachPrefix + "$property must be a boolean string", validationOptions)
},
validationOptions
);
}
24 changes: 24 additions & 0 deletions src/decorator/IsNumberString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {ValidationOptions} from "./ValidationOptions";
import {buildMessage, ValidateBy} from "./ValidateBy";
import validatorJsIsNumeric = require("validator/lib/isNumeric");

/**
* Checks if the string is numeric.
* If given value is not a string, then it returns false.
*/
export function isNumberString(value: string, options?: ValidatorJS.IsNumericOptions): boolean {
return typeof value === "string" && validatorJsIsNumeric(value, options);
}

/**
* Checks if the string is a number.
*/
export function IsNumberString(validationOptions?: ValidationOptions) {
return ValidateBy({
name: "isNumberString",
validate: (value) => isNumberString(value),
defaultMessage: buildMessage((eachPrefix) => eachPrefix + "$property must be a number string", validationOptions)
},
validationOptions
);
}
49 changes: 0 additions & 49 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,59 +86,10 @@ export function IsOptional(validationOptions?: ValidationOptions) {
};
}

// -------------------------------------------------------------------------
// String-as-types checkers
// -------------------------------------------------------------------------

/**
* Checks if a string is a boolean.
*/
export function IsBooleanString(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_BOOLEAN_STRING,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if the string is a number.
*/
export function IsNumberString(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.IS_NUMBER_STRING,
target: object.constructor,
propertyName: propertyName,
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

// -------------------------------------------------------------------------
// String checkers
// -------------------------------------------------------------------------

/**
* Checks if the string contains the seed.
*/
export function Contains(seed: string, validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
const args: ValidationMetadataArgs = {
type: ValidationTypes.CONTAINS,
target: object.constructor,
propertyName: propertyName,
constraints: [seed],
validationOptions: validationOptions
};
getFromContainer(MetadataStorage).addValidationMetadata(new ValidationMetadata(args));
};
}

/**
* Checks if the string does not contain the seed.
Expand Down
15 changes: 1 addition & 14 deletions src/validation/ValidationTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ export class ValidationTypes {
static NESTED_VALIDATION = "nestedValidation";
static CONDITIONAL_VALIDATION = "conditionalValidation";
static WHITELIST = "whitelistValidation";
// FIXME: delete
// FIXME: delete?
static IS_DEFINED = "isDefined";

/* string-as-type checkers */
static IS_BOOLEAN_STRING = "isBooleanString";
static IS_NUMBER_STRING = "isNumberString";

/* string checkers */
static CONTAINS = "contains";
static NOT_CONTAINS = "notContains";
static IS_ALPHA = "isAlpha";
static IS_ALPHANUMERIC = "isAlphanumeric";
Expand Down Expand Up @@ -86,15 +81,7 @@ export class ValidationTypes {
case this.IS_DEFINED:
return eachPrefix + "$property should not be null or undefined";

/* string-as-type checkers */
case this.IS_BOOLEAN_STRING:
return eachPrefix + "$property must be a boolean string";
case this.IS_NUMBER_STRING:
return eachPrefix + "$property must be a number string";

/* string checkers */
case this.CONTAINS:
return eachPrefix + "$property must contain a $constraint1 string";
case this.NOT_CONTAINS:
return eachPrefix + "$property should not contain a $constraint1 string";
case this.IS_ALPHA:
Expand Down
35 changes: 0 additions & 35 deletions src/validation/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,7 @@ export class Validator {
case ValidationTypes.IS_DEFINED:
return this.isDefined(value);

/* string-as-type checkers */
case ValidationTypes.IS_BOOLEAN_STRING:
return this.isBooleanString(value);
case ValidationTypes.IS_NUMBER_STRING:
return this.isNumberString(value);

/* string checkers */
case ValidationTypes.CONTAINS:
return this.contains(value, metadata.constraints[0]);
case ValidationTypes.NOT_CONTAINS:
return this.notContains(value, metadata.constraints[0]);
case ValidationTypes.IS_ALPHA:
Expand Down Expand Up @@ -218,37 +210,10 @@ export class Validator {
return value !== undefined && value !== null;
}

// -------------------------------------------------------------------------
// Validation Methods: string-as-type checkers
// -------------------------------------------------------------------------

/**
* Checks if a string is a boolean.
* If given value is not a string, then it returns false.
*/
isBooleanString(value: string): boolean {
return typeof value === "string" && this.validatorJs.isBoolean(value);
}

/**
* Checks if the string is numeric.
* If given value is not a string, then it returns false.
*/
isNumberString(value: string, options?: ValidatorJS.IsNumericOptions): boolean {
return typeof value === "string" && this.validatorJs.isNumeric(value, options);
}

// -------------------------------------------------------------------------
// Validation Methods: string checkers
// -------------------------------------------------------------------------

/**
* Checks if the string contains the seed.
* If given value is not a string, then it returns false.
*/
contains(value: string, seed: string): boolean {
return typeof value === "string" && this.validatorJs.contains(value, seed);
}

/**
* Checks if the string does not contain the seed.
Expand Down
Loading

0 comments on commit 3907863

Please sign in to comment.