From 9870229d50cec1eb613d0be00e73cac45beb61f9 Mon Sep 17 00:00:00 2001 From: "ashish.gurjar_Backend" Date: Fri, 15 Feb 2019 19:23:44 +0530 Subject: [PATCH] location added --- src/lib/impl/index.ts | 19 +----- src/lib/index.ts | 3 +- src/lib/schema/index.ts | 7 +-- src/lib/schema/locations/base.location.ts | 19 ++++++ src/lib/schema/locations/body.location.ts | 7 +++ src/lib/schema/locations/body.schema.ts | 12 ---- src/lib/schema/locations/cookie.location.ts | 7 +++ src/lib/schema/locations/header.location.ts | 7 +++ src/lib/schema/locations/header.schema.ts | 0 src/lib/schema/locations/index.ts | 6 +- src/lib/schema/locations/param.location.ts | 7 +++ src/lib/schema/locations/param.schema.ts | 0 src/lib/schema/locations/query.location.ts | 7 +++ src/lib/schema/locations/query.schema.ts | 0 src/lib/schema/types/any.schema.ts | 3 - src/lib/schema/types/any.type.ts | 18 ++++++ src/lib/schema/types/array.schema.ts | 0 .../cookie.schema.ts => types/array.type.ts} | 0 .../types/{base.schema.ts => base.type.ts} | 44 ++++++++++++-- src/lib/schema/types/index.ts | 8 +-- src/lib/schema/types/number.schema.ts | 52 ----------------- src/lib/schema/types/number.type.ts | 54 +++++++++++++++++ src/lib/schema/types/object.schema.ts | 0 src/lib/schema/types/object.type.ts | 18 ++++++ .../{string.schema.ts => string.type.ts} | 58 +++++++++++-------- tslint.json | 3 +- 26 files changed, 235 insertions(+), 124 deletions(-) create mode 100644 src/lib/schema/locations/base.location.ts create mode 100644 src/lib/schema/locations/body.location.ts delete mode 100644 src/lib/schema/locations/body.schema.ts create mode 100644 src/lib/schema/locations/cookie.location.ts create mode 100644 src/lib/schema/locations/header.location.ts delete mode 100644 src/lib/schema/locations/header.schema.ts create mode 100644 src/lib/schema/locations/param.location.ts delete mode 100644 src/lib/schema/locations/param.schema.ts create mode 100644 src/lib/schema/locations/query.location.ts delete mode 100644 src/lib/schema/locations/query.schema.ts delete mode 100644 src/lib/schema/types/any.schema.ts create mode 100644 src/lib/schema/types/any.type.ts delete mode 100644 src/lib/schema/types/array.schema.ts rename src/lib/schema/{locations/cookie.schema.ts => types/array.type.ts} (100%) rename src/lib/schema/types/{base.schema.ts => base.type.ts} (61%) delete mode 100644 src/lib/schema/types/number.schema.ts create mode 100644 src/lib/schema/types/number.type.ts delete mode 100644 src/lib/schema/types/object.schema.ts create mode 100644 src/lib/schema/types/object.type.ts rename src/lib/schema/types/{string.schema.ts => string.type.ts} (65%) diff --git a/src/lib/impl/index.ts b/src/lib/impl/index.ts index 5d3f4c8..4b9e806 100644 --- a/src/lib/impl/index.ts +++ b/src/lib/impl/index.ts @@ -1,18 +1,5 @@ +import { BodyLocation } from '../schema/index'; - - -export interface IEmail { - email(): string; -} - -export interface IString { - email(): IEmail; +export function body(field: string) { + return new BodyLocation(field); } - -export interface INumber { - number(): string; -} - -export interface IBoolean { - boolean(): string; -} \ No newline at end of file diff --git a/src/lib/index.ts b/src/lib/index.ts index 36be004..c4c765c 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,2 +1,3 @@ -export class MyLibrary {} +// import { body } from './impl/index'; + diff --git a/src/lib/schema/index.ts b/src/lib/schema/index.ts index 36f91b4..5f9504f 100644 --- a/src/lib/schema/index.ts +++ b/src/lib/schema/index.ts @@ -1,6 +1,3 @@ -// import { BodySchema } from './locations/body.schema'; -import { NumberSchema } from './types/number.schema'; -import { StringSchema } from './types/string.schema'; -// new StringSchema().allow(); -// new NumberSchema().min(10).allow(); +export * from './types/index'; +export * from './locations/index'; diff --git a/src/lib/schema/locations/base.location.ts b/src/lib/schema/locations/base.location.ts new file mode 100644 index 0000000..26ecc42 --- /dev/null +++ b/src/lib/schema/locations/base.location.ts @@ -0,0 +1,19 @@ +import { AnyType } from '../types/any.type'; +import { StringType } from '../types/string.type'; +import { NumberType } from '../types/number.type'; + +export class BaseLocation { + constructor( + private _location: string, + private _fieldName: string, + ) {} + any(): AnyType { + return new AnyType(this._location, this._fieldName); + } + string(): StringType { + return new StringType(this._location, this._fieldName); + } + number(): NumberType { + return new NumberType(this._location, this._fieldName); + } +} diff --git a/src/lib/schema/locations/body.location.ts b/src/lib/schema/locations/body.location.ts new file mode 100644 index 0000000..f450a61 --- /dev/null +++ b/src/lib/schema/locations/body.location.ts @@ -0,0 +1,7 @@ +import { BaseLocation } from './base.location'; + +export class BodyLocation extends BaseLocation { + constructor(fieldName: string) { + super('body', fieldName); + } +} diff --git a/src/lib/schema/locations/body.schema.ts b/src/lib/schema/locations/body.schema.ts deleted file mode 100644 index b9ddb8a..0000000 --- a/src/lib/schema/locations/body.schema.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { AnySchema } from '../types/any.schema'; -import { StringSchema } from '../types/string.schema'; -import { NumberSchema } from '../types/number.schema'; - -export class BodySchema extends AnySchema { - string(): StringSchema { - return new StringSchema(); - } - number(): NumberSchema { - return new NumberSchema(); - } -} \ No newline at end of file diff --git a/src/lib/schema/locations/cookie.location.ts b/src/lib/schema/locations/cookie.location.ts new file mode 100644 index 0000000..e90c783 --- /dev/null +++ b/src/lib/schema/locations/cookie.location.ts @@ -0,0 +1,7 @@ +import { BaseLocation } from './base.location'; + +export class CookieLocation extends BaseLocation { + constructor(fieldName: string) { + super('cookie', fieldName); + } +} diff --git a/src/lib/schema/locations/header.location.ts b/src/lib/schema/locations/header.location.ts new file mode 100644 index 0000000..fda7f34 --- /dev/null +++ b/src/lib/schema/locations/header.location.ts @@ -0,0 +1,7 @@ +import { BaseLocation } from './base.location'; + +export class HeaderLocation extends BaseLocation { + constructor(fieldName: string) { + super('header', fieldName); + } +} diff --git a/src/lib/schema/locations/header.schema.ts b/src/lib/schema/locations/header.schema.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/schema/locations/index.ts b/src/lib/schema/locations/index.ts index 6141cf8..7084bbb 100644 --- a/src/lib/schema/locations/index.ts +++ b/src/lib/schema/locations/index.ts @@ -1,2 +1,6 @@ -export * from './body.schema'; \ No newline at end of file +export * from './body.location'; +export * from './query.location'; +export * from './param.location'; +export * from './header.location'; +export * from './cookie.location'; diff --git a/src/lib/schema/locations/param.location.ts b/src/lib/schema/locations/param.location.ts new file mode 100644 index 0000000..de7cfa0 --- /dev/null +++ b/src/lib/schema/locations/param.location.ts @@ -0,0 +1,7 @@ +import { BaseLocation } from './base.location'; + +export class ParamLocation extends BaseLocation { + constructor(fieldName: string) { + super('param', fieldName); + } +} diff --git a/src/lib/schema/locations/param.schema.ts b/src/lib/schema/locations/param.schema.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/schema/locations/query.location.ts b/src/lib/schema/locations/query.location.ts new file mode 100644 index 0000000..ed8b210 --- /dev/null +++ b/src/lib/schema/locations/query.location.ts @@ -0,0 +1,7 @@ +import { BaseLocation } from './base.location'; + +export class QueryLocation extends BaseLocation { + constructor(fieldName: string) { + super('query', fieldName); + } +} diff --git a/src/lib/schema/locations/query.schema.ts b/src/lib/schema/locations/query.schema.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/schema/types/any.schema.ts b/src/lib/schema/types/any.schema.ts deleted file mode 100644 index a365ec6..0000000 --- a/src/lib/schema/types/any.schema.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { BaseSchema } from './base.schema'; - -export class AnySchema extends BaseSchema {} diff --git a/src/lib/schema/types/any.type.ts b/src/lib/schema/types/any.type.ts new file mode 100644 index 0000000..82bad0c --- /dev/null +++ b/src/lib/schema/types/any.type.ts @@ -0,0 +1,18 @@ +/** + * @name AnyType + * @type Any + * @description AnyType + */ + +import { BaseType } from './base.type'; + +export class AnyType extends BaseType { + protected _metaData: any; + protected _schema: string[]; + constructor( + protected _location: string, + protected _fieldName: string, + ) { + super(); + } +} diff --git a/src/lib/schema/types/array.schema.ts b/src/lib/schema/types/array.schema.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/schema/locations/cookie.schema.ts b/src/lib/schema/types/array.type.ts similarity index 100% rename from src/lib/schema/locations/cookie.schema.ts rename to src/lib/schema/types/array.type.ts diff --git a/src/lib/schema/types/base.schema.ts b/src/lib/schema/types/base.type.ts similarity index 61% rename from src/lib/schema/types/base.schema.ts rename to src/lib/schema/types/base.type.ts index 28702ea..b76536f 100644 --- a/src/lib/schema/types/base.schema.ts +++ b/src/lib/schema/types/base.type.ts @@ -1,8 +1,14 @@ +/** + * @name BaseType + * @type Generic + * @description BaseType + */ -export abstract class BaseSchema { - protected abstract location: string; - protected abstract fieldName: string; - protected abstract metaData: any; +export abstract class BaseType { + protected abstract _location: string; + protected abstract _fieldName: string; + protected abstract _metaData: any; + protected abstract _schema: string[]; /** * @type Filter * @description This allow the given argument values to be truthy for filter. @@ -61,16 +67,46 @@ export abstract class BaseSchema { // TO DO return this; } + /** + * @type MetaData + * @description This function will set the description metadata to given value. + */ description(): this { // TO DO return this; } + /** + * @type Filter, Modifier + * @description This function will accept a custom validator function. + */ custom(cb: (value: T) => void): this { // TO DO return this; } + /** + * @type Filter + * @description This function check equal filter to given value. + */ equalTo(value: T): this { // TO DO return this; } + /** + * @type MetaData + * @description This function set location metdata. + * @param location Location identifier. + */ + location(loc: string): this { + // TO DO + return this; + } + /** + * @type Filter + * @description This function validate. + * @param values Location identifier. + */ + enum(...values: T[]): this { + // TO DO + return this; + } } diff --git a/src/lib/schema/types/index.ts b/src/lib/schema/types/index.ts index 0957ad6..6ea5414 100644 --- a/src/lib/schema/types/index.ts +++ b/src/lib/schema/types/index.ts @@ -1,5 +1,5 @@ -export * from './base.schema'; -export * from './any.schema'; -export * from './string.schema'; -export * from './number.schema'; +export * from './base.type'; +export * from './any.type'; +export * from './string.type'; +export * from './number.type'; diff --git a/src/lib/schema/types/number.schema.ts b/src/lib/schema/types/number.schema.ts deleted file mode 100644 index cdb5f48..0000000 --- a/src/lib/schema/types/number.schema.ts +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @name NumberSchema - * @type Number - * @description NumberSchema - */ - -import { BaseSchema } from './base.schema'; - -export class NumberSchema extends BaseSchema { - protected location: string; - protected metaData: any; - protected schema: string[]; - constructor(protected fieldName: string) { - super(); - } - integer(convert: boolean = false): NumberSchema { - // TO DO - return this; - } - float(convert: boolean = false): NumberSchema { - // TO DO - return this; - } - unsafe(): NumberSchema { - // TO DO - return this; - } - negative(): NumberSchema { - // TO DO - return this; - } - position(): NumberSchema { - // TO DO - return this; - } - precision(): NumberSchema { - // TO DO - return this; - } - min(num: number): NumberSchema { - // TO DO - return this; - } - max(num: number): NumberSchema { - // TO DO - return this; - } - port(): NumberSchema { - // TO DO - return this; - } -} diff --git a/src/lib/schema/types/number.type.ts b/src/lib/schema/types/number.type.ts new file mode 100644 index 0000000..2e0d935 --- /dev/null +++ b/src/lib/schema/types/number.type.ts @@ -0,0 +1,54 @@ +/** + * @name NumberType + * @type Number + * @description NumberType + */ + +import { BaseType } from './base.type'; + +export class NumberType extends BaseType { + protected _metaData: any; + protected _schema: string[]; + constructor( + protected _location: string, + protected _fieldName: string, + ) { + super(); + } + integer(convert: boolean = false): this { + // TO DO + return this; + } + float(convert: boolean = false): this { + // TO DO + return this; + } + unsafe(): this { + // TO DO + return this; + } + negative(): this { + // TO DO + return this; + } + position(): this { + // TO DO + return this; + } + precision(): this { + // TO DO + return this; + } + min(num: number): this { + // TO DO + return this; + } + max(num: number): this { + // TO DO + return this; + } + port(): this { + // TO DO + return this; + } +} diff --git a/src/lib/schema/types/object.schema.ts b/src/lib/schema/types/object.schema.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/lib/schema/types/object.type.ts b/src/lib/schema/types/object.type.ts new file mode 100644 index 0000000..6442982 --- /dev/null +++ b/src/lib/schema/types/object.type.ts @@ -0,0 +1,18 @@ +/** + * @name ObjectType + * @type Object + * @description ObjectType + */ + +import { BaseType } from './base.type'; + +export class ObjectType extends BaseType { + protected _metaData: any; + protected _schema: string[]; + constructor( + protected _location: string, + protected _fieldName: string, + ) { + super(); + } +} diff --git a/src/lib/schema/types/string.schema.ts b/src/lib/schema/types/string.type.ts similarity index 65% rename from src/lib/schema/types/string.schema.ts rename to src/lib/schema/types/string.type.ts index 1c8e630..e62efe0 100644 --- a/src/lib/schema/types/string.schema.ts +++ b/src/lib/schema/types/string.type.ts @@ -1,23 +1,31 @@ -import { BaseSchema } from './base.schema'; +/** + * @name StringType + * @type Number + * @description StringType + */ -export class StringSchema extends BaseSchema { - protected location: string; - protected metaData: any; - protected schema: string[]; - constructor(protected fieldName: string) { +import { BaseType } from './base.type'; + +export class StringType extends BaseType { + protected _metaData: any; + protected _schema: string[]; + constructor( + protected _location: string, + protected _fieldName: string, + ) { super(); } /** * @type Filter */ - alphanum(): StringSchema { + alphanum(): this { // check for alphanum value return this; } /** * @type Filter */ - regex(): StringSchema { + regex(): this { // check for regex return this; } @@ -25,7 +33,7 @@ export class StringSchema extends BaseSchema { * @type Modifier, Filter * @description Lowercase value or filter lowercase value */ - lowercase(convert: boolean = true): StringSchema { + lowercase(convert: boolean = true): this { // TO DO return this; } @@ -33,7 +41,7 @@ export class StringSchema extends BaseSchema { * @type Modifier, Filter * @description Upercase value or filter upercase value */ - uppercase(convert: boolean = true): StringSchema { + uppercase(convert: boolean = true): this { // TO DO return this; } @@ -41,105 +49,105 @@ export class StringSchema extends BaseSchema { * @type Modifier, Filter * @description Trim value or filter trimmed value */ - trim(convert: boolean = true): StringSchema { + trim(convert: boolean = true): this { // trim value or check if value trim return this; } /** * @type Modifier, Filter */ - trimEnd(convert: boolean = true): StringSchema { + trimEnd(convert: boolean = true): this { // check for alphanum value return this; } /** * @type Modifier, Filter */ - trimStart(convert: boolean = true): StringSchema { + trimStart(convert: boolean = true): this { // check for alphanum value return this; } /** * @type Filter */ - replace(): StringSchema { + replace(): this { // check for alphanum value return this; } /** * @type Filter */ - truncate(): StringSchema { + truncate(): this { // check for alphanum value return this; } /** * @type Filter */ - email(): StringSchema { + email(): this { // check for alphanum value return this; } /** * @type Filter */ - isoDate(): StringSchema { + isoDate(): this { // check for alphanum value return this; } /** * @type Filter */ - jwt(): StringSchema { + jwt(): this { // check for alphanum value return this; } /** * @type Filter */ - uri(): StringSchema { + uri(): this { // check for alphanum value return this; } /** * @type Filter */ - dataUri(): StringSchema { + dataUri(): this { // check for alphanum value return this; } /** * @type Filter */ - ip(): StringSchema { + ip(): this { // check for alphanum value return this; } /** * @type Filter */ - creditCard(): StringSchema { + creditCard(): this { // check for alphanum value return this; } /** * @type Filter */ - hostname(): StringSchema { + hostname(): this { // check for alphanum value return this; } /** * @type Modifier */ - normalize(): StringSchema { + normalize(): this { // check for alphanum value return this; } /** * @type Modifier */ - split(): StringSchema { + split(): this { // check for alphanum value return this; } diff --git a/tslint.json b/tslint.json index 9b0ae22..ef5e90d 100644 --- a/tslint.json +++ b/tslint.json @@ -11,7 +11,8 @@ "no-namespace": false, "interface-name": false, "no-reference": false, - "member-access": [true, "no-public"] + "member-access": [true, "no-public"], + "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"] }, "rulesDirectory": [] } \ No newline at end of file