Skip to content

Commit

Permalink
build generated
Browse files Browse the repository at this point in the history
  • Loading branch information
vasuvanka committed Apr 18, 2020
1 parent b6b1266 commit 594c9cc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 86 deletions.
1 change: 0 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './src/validator';
//# sourceMappingURL=index.d.ts.map
1 change: 0 additions & 1 deletion dist/index.d.ts.map

This file was deleted.

1 change: 0 additions & 1 deletion dist/index.js.map

This file was deleted.

77 changes: 15 additions & 62 deletions dist/src/validator.d.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,24 @@
export declare type defaultType = string | number | boolean | Date;
export declare type valueType = String | Number | Boolean | Date;
export declare type validateWith = (arg: any, body?: any) => any;
export declare const type: {
number: string;
object: string;
array: string;
boolean: string;
date: string;
string: string;
undefined: string;
null: string;
function: string;
};
/**
* valueSchema - represents a value and its rules
* validate - will compare json object and defined schema
* @param value - json object
* @param schema - schema definition
* @returns string | null - if any schema fails it will return string otherwise null.
*/
export interface valueSchema {
/**
* type - type of value. it can be String,Number,Boolean,Date
*/
type: valueType;
/**
* default - if there is no key exist this value will be set
*/
default?: defaultType;
/**
* required - if required true it will check for existance, if not found will throw an error
*/
required?: boolean;
/**
* pattern - Validate strings using regex
*/
pattern?: string;
/**
* min - value should be grater than min
*/
min?: number;
/**
* max - value should be less than max
*/
max?: number;
/**
* length - length of string
*/
length?: number;
/**
* validateWith - function will take 2 arguments one is value and other is body and return a value
*/
validateWith?: validateWith;
}
/**
* valueValidation - will validate value aganist schema
*/
export interface valueValidation {
/**
* key - key name of object
*/
key: string;
/**
* value - value
*/
value: defaultType;
/**
* schema - value schema
*/
schema: valueSchema;
/**
* error - if value is not suits the schema defined then it will return error string or null
*/
error: string | null;
}
export declare function validate(value: any, schema: any): string | null;
/**
* findType - will return type of given value
* @param value : any
* @returns string
*/
export declare function findType(value: any): string;
export declare function buildValueObj(key: string, value: defaultType, schema: any): valueValidation;
export declare function compareTypes(valueObj: valueValidation): valueValidation;
//# sourceMappingURL=validator.d.ts.map
1 change: 0 additions & 1 deletion dist/src/validator.d.ts.map

This file was deleted.

35 changes: 18 additions & 17 deletions dist/src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exports.type = {
'function': 'function'
};
// trace - will trace error path
const trace = [];
var trace = [];
/**
* validateType - validates value and type
* @param value - value
Expand All @@ -26,7 +26,7 @@ function validateType(value, schema, valueType, schemaType) {
if (schemaType != exports.type.object || !schema.type) {
return buildErrorMessage(value, valueType, schemaType);
}
const shType = findType(schema.type);
var shType = findType(schema.type);
if (valueType !== shType) {
return buildErrorMessage(value, valueType, shType);
}
Expand All @@ -40,7 +40,7 @@ function validateType(value, schema, valueType, schemaType) {
* @returns string - will return constructed error message
*/
function buildErrorMessage(value, valueType, schemaType) {
return `required type '${schemaType}' for value '${JSON.stringify(value)}' but found '${valueType}' at path ${trace.join('.')}`;
return "required type '" + schemaType + "' for value '" + JSON.stringify(value) + "' but found '" + valueType + "' at path " + trace.join('.');
}
/**
* validate - will compare json object and defined schema
Expand All @@ -51,7 +51,7 @@ function buildErrorMessage(value, valueType, schemaType) {
function validate(value, schema) {
// trace = []
trace.length = 0;
const error = validateData(value, schema);
var error = validateData(value, schema);
return error;
}
exports.validate = validate;
Expand All @@ -62,9 +62,9 @@ exports.validate = validate;
* @returns string | null - if any schema fails it will return string otherwise null.
*/
function validateData(value, schema) {
const valueType = findType(value);
const schemaType = findType(schema);
let error = null;
var valueType = findType(value);
var schemaType = findType(schema);
var error = null;
switch (valueType) {
case exports.type.string:
error = validateType(value, schema, valueType, schemaType);
Expand All @@ -83,14 +83,15 @@ function validateData(value, schema) {
error = buildErrorMessage(value, valueType, schemaType);
}
if (Object.keys(schema).length === 0) {
error = `found '${valueType}' for value '${JSON.stringify(value)}' but no schema definition found : ${trace.join('.')}`;
error = "found '" + valueType + "' for value '" + JSON.stringify(value) + "' but no schema definition found : " + trace.join('.');
}
if (!error) {
const keys = Object.keys(value);
for (const key of keys) {
var keys = Object.keys(value);
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
var key = keys_1[_i];
trace.push(key);
if (!schema[key]) {
error = `no schema definition found for value ${JSON.stringify(value[key])} : ${trace.join('.')}`;
error = "no schema definition found for value " + JSON.stringify(value[key]) + " : " + trace.join('.');
}
if (!error) {
error = validateData(value[key], schema[key]);
Expand All @@ -107,12 +108,12 @@ function validateData(value, schema) {
error = buildErrorMessage(value, valueType, schemaType);
}
else if (schema.length == 0) {
error = `no schema definition found for value ${JSON.stringify(value)} at path ${trace.join('.')}`;
error = "no schema definition found for value " + JSON.stringify(value) + " at path " + trace.join('.');
}
if (!error) {
for (let index = 0; index < value.length; index++) {
const subValue = value[index];
trace.push(`[${index}]`);
for (var index = 0; index < value.length; index++) {
var subValue = value[index];
trace.push("[" + index + "]");
error = validateData(subValue, schema[0]);
if (error != null) {
break;
Expand All @@ -122,7 +123,7 @@ function validateData(value, schema) {
}
break;
default:
error = `no schema definition found for value ${JSON.stringify(value)} at path ${trace.join('.')}`;
error = "no schema definition found for value " + JSON.stringify(value) + " at path " + trace.join('.');
break;
}
return error;
Expand All @@ -133,7 +134,7 @@ function validateData(value, schema) {
* @returns string
*/
function findType(value) {
const valueType = typeof value;
var valueType = typeof value;
if (valueType === exports.type.string) {
if (!isNaN(Date.parse(value))) {
return exports.type.date;
Expand Down
1 change: 0 additions & 1 deletion dist/src/validator.js.map

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"compilerOptions": {
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"target": "ES5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
Expand Down

0 comments on commit 594c9cc

Please sign in to comment.