@handy-tools/validator
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -66,5 +66,5 @@ var __defProp = Object.defineProperty; | ||
const reason = validateFn ? "because validate function returned false" : `${value} does not match the regexp ${re.toString()}`; | ||
message = errorMessage != null ? errorMessage : `Field ${field} is invalid, ${reason}`; | ||
message = errorMessage != null ? errorMessage : `Field is invalid, ${reason}`; | ||
} | ||
return { result, errors: [message] }; | ||
return { result, errors: { [field]: message } }; | ||
}; | ||
@@ -115,4 +115,6 @@ | ||
} | ||
validate(obj, schema = this.schema, errors = [], isOr = false) { | ||
validate(obj, schema = this.schema, errors = {}, isOr = false, isRecursive = false) { | ||
var _a, _b; | ||
if (!isRecursive) | ||
this.count = 0; | ||
this.count++; | ||
@@ -123,8 +125,9 @@ const objectKeys = Object.keys(obj); | ||
if (incompatibleKeys.length) { | ||
const errors2 = incompatibleKeys.map((key) => `Object is incompatible with scheme: hasn't this '${key}' field`); | ||
this.count = 0; | ||
const errors2 = incompatibleKeys.reduce((acc, key) => { | ||
acc[key] = `Object is incompatible with scheme: hasn't this '${key}' field`; | ||
return acc; | ||
}, {}); | ||
return { result: false, errors: errors2 }; | ||
} | ||
if (this.count > this.maxDepth) { | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -138,4 +141,3 @@ Max depth of validation is reached | ||
if (!schemaType && isOr) { | ||
this.count = 0; | ||
return { result: false, errors: ["Schema not compatible with object"] }; | ||
return { result: false, errors: { common: "Schema not compatible with object" } }; | ||
} | ||
@@ -148,5 +150,3 @@ if (!schemaType) { | ||
if (this.options.validateExtraFields) { | ||
errors.push(errMessage); | ||
this.count = 0; | ||
return { result: false, errors }; | ||
return { result: false, errors: { [field]: errMessage } }; | ||
} else { | ||
@@ -179,3 +179,3 @@ continue; | ||
const results = _schemas.map((_scheme) => { | ||
const result = this.validate(_validatableObj, _scheme, errors, true); | ||
const result = this.validate(_validatableObj, _scheme, errors, true, true); | ||
return result; | ||
@@ -186,4 +186,5 @@ }); | ||
} | ||
this.count = 0; | ||
return { result: false, errors: results.map((r) => r.errors).flat() }; | ||
return { result: false, errors: results.reduce((acc, r) => { | ||
return Object.assign(acc, r.errors); | ||
}, {}) }; | ||
} else if (typeof value === "object" && Array.isArray(value) && schemaType instanceof ValidatorArraySchema) { | ||
@@ -193,19 +194,14 @@ const _schema = convertToSchema(schemaType.schema); | ||
for (const v of _values) { | ||
const result = this.validate(v, _schema, errors); | ||
if (!(result == null ? void 0 : result.result)) { | ||
this.count = 0; | ||
const result = this.validate(v, _schema, errors, false, true); | ||
if (!(result == null ? void 0 : result.result)) | ||
return result; | ||
} | ||
} | ||
continue; | ||
} else if (typeof value === "object" && typeof schemaType === "object" && schemaType instanceof ValidatorNestedSchema) { | ||
const result = this.validate(value, schemaType.schema, errors); | ||
if (!result.result) { | ||
this.count = 0; | ||
const result = this.validate(value, schemaType.schema, errors, false, true); | ||
if (!result.result) | ||
return result; | ||
} | ||
continue; | ||
} else if (typeof value !== "object" && typeof schemaType === "object" && schemaType instanceof ValidatorNestedSchema) { | ||
this.count = 0; | ||
return { result: false, errors: [`Field ${field} has not matched with scheme`] }; | ||
return { result: false, errors: { [field]: `Field has not matched with scheme` } }; | ||
} else if (typeof schemaType === "object" && typeof (schemaType == null ? void 0 : schemaType.type) === "string" && SCHEME_TYPES[schemaType.type]) { | ||
@@ -215,3 +211,2 @@ schemaType = schemaType.type; | ||
if (typeof schemaType === "string" && !SCHEME_TYPES[schemaType]) { | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -227,4 +222,3 @@ Validator can't handle passed type | ||
const reason = "because validate function returned false"; | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not valid, ${reason}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not valid, ${reason}`; | ||
return { result: Boolean(result), errors }; | ||
@@ -237,3 +231,2 @@ } else { | ||
console.log({ schemaType }); | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -248,4 +241,3 @@ Unexpected type for validation | ||
if (!result) { | ||
errors.push(`Field ${field} is not an array`); | ||
this.count = 0; | ||
errors[field] = `Field is not an array`; | ||
return { result, errors }; | ||
@@ -260,9 +252,7 @@ } | ||
const reason = "because validate function returned false"; | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a json, ${reason}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not a json, ${reason}`; | ||
return { result: Boolean(result), errors }; | ||
} | ||
} catch (e) { | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a json`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field ${field} is not a json`; | ||
return { result: false, errors }; | ||
@@ -280,6 +270,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -295,6 +283,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -310,6 +296,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -325,6 +309,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -340,6 +322,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -355,6 +335,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -370,6 +348,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -385,6 +361,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -400,6 +374,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -411,4 +383,3 @@ } | ||
if (typeof value !== schemaType) { | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a ${schemaType}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not a ${schemaType}`; | ||
return { result: false, errors }; | ||
@@ -418,4 +389,2 @@ } | ||
default: | ||
console.dir({ schemaType }); | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -427,4 +396,3 @@ Unexpected type for validation | ||
} | ||
this.count = 0; | ||
return { result: errors.length === 0, errors }; | ||
return { result: Object.keys(errors).length === 0, errors }; | ||
} | ||
@@ -431,0 +399,0 @@ }; |
@@ -41,5 +41,5 @@ // src/constants.ts | ||
const reason = validateFn ? "because validate function returned false" : `${value} does not match the regexp ${re.toString()}`; | ||
message = errorMessage != null ? errorMessage : `Field ${field} is invalid, ${reason}`; | ||
message = errorMessage != null ? errorMessage : `Field is invalid, ${reason}`; | ||
} | ||
return { result, errors: [message] }; | ||
return { result, errors: { [field]: message } }; | ||
}; | ||
@@ -90,4 +90,6 @@ | ||
} | ||
validate(obj, schema = this.schema, errors = [], isOr = false) { | ||
validate(obj, schema = this.schema, errors = {}, isOr = false, isRecursive = false) { | ||
var _a, _b; | ||
if (!isRecursive) | ||
this.count = 0; | ||
this.count++; | ||
@@ -98,8 +100,9 @@ const objectKeys = Object.keys(obj); | ||
if (incompatibleKeys.length) { | ||
const errors2 = incompatibleKeys.map((key) => `Object is incompatible with scheme: hasn't this '${key}' field`); | ||
this.count = 0; | ||
const errors2 = incompatibleKeys.reduce((acc, key) => { | ||
acc[key] = `Object is incompatible with scheme: hasn't this '${key}' field`; | ||
return acc; | ||
}, {}); | ||
return { result: false, errors: errors2 }; | ||
} | ||
if (this.count > this.maxDepth) { | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -113,4 +116,3 @@ Max depth of validation is reached | ||
if (!schemaType && isOr) { | ||
this.count = 0; | ||
return { result: false, errors: ["Schema not compatible with object"] }; | ||
return { result: false, errors: { common: "Schema not compatible with object" } }; | ||
} | ||
@@ -123,5 +125,3 @@ if (!schemaType) { | ||
if (this.options.validateExtraFields) { | ||
errors.push(errMessage); | ||
this.count = 0; | ||
return { result: false, errors }; | ||
return { result: false, errors: { [field]: errMessage } }; | ||
} else { | ||
@@ -154,3 +154,3 @@ continue; | ||
const results = _schemas.map((_scheme) => { | ||
const result = this.validate(_validatableObj, _scheme, errors, true); | ||
const result = this.validate(_validatableObj, _scheme, errors, true, true); | ||
return result; | ||
@@ -161,4 +161,5 @@ }); | ||
} | ||
this.count = 0; | ||
return { result: false, errors: results.map((r) => r.errors).flat() }; | ||
return { result: false, errors: results.reduce((acc, r) => { | ||
return Object.assign(acc, r.errors); | ||
}, {}) }; | ||
} else if (typeof value === "object" && Array.isArray(value) && schemaType instanceof ValidatorArraySchema) { | ||
@@ -168,19 +169,14 @@ const _schema = convertToSchema(schemaType.schema); | ||
for (const v of _values) { | ||
const result = this.validate(v, _schema, errors); | ||
if (!(result == null ? void 0 : result.result)) { | ||
this.count = 0; | ||
const result = this.validate(v, _schema, errors, false, true); | ||
if (!(result == null ? void 0 : result.result)) | ||
return result; | ||
} | ||
} | ||
continue; | ||
} else if (typeof value === "object" && typeof schemaType === "object" && schemaType instanceof ValidatorNestedSchema) { | ||
const result = this.validate(value, schemaType.schema, errors); | ||
if (!result.result) { | ||
this.count = 0; | ||
const result = this.validate(value, schemaType.schema, errors, false, true); | ||
if (!result.result) | ||
return result; | ||
} | ||
continue; | ||
} else if (typeof value !== "object" && typeof schemaType === "object" && schemaType instanceof ValidatorNestedSchema) { | ||
this.count = 0; | ||
return { result: false, errors: [`Field ${field} has not matched with scheme`] }; | ||
return { result: false, errors: { [field]: `Field has not matched with scheme` } }; | ||
} else if (typeof schemaType === "object" && typeof (schemaType == null ? void 0 : schemaType.type) === "string" && SCHEME_TYPES[schemaType.type]) { | ||
@@ -190,3 +186,2 @@ schemaType = schemaType.type; | ||
if (typeof schemaType === "string" && !SCHEME_TYPES[schemaType]) { | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -202,4 +197,3 @@ Validator can't handle passed type | ||
const reason = "because validate function returned false"; | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not valid, ${reason}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not valid, ${reason}`; | ||
return { result: Boolean(result), errors }; | ||
@@ -212,3 +206,2 @@ } else { | ||
console.log({ schemaType }); | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -223,4 +216,3 @@ Unexpected type for validation | ||
if (!result) { | ||
errors.push(`Field ${field} is not an array`); | ||
this.count = 0; | ||
errors[field] = `Field is not an array`; | ||
return { result, errors }; | ||
@@ -235,9 +227,7 @@ } | ||
const reason = "because validate function returned false"; | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a json, ${reason}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not a json, ${reason}`; | ||
return { result: Boolean(result), errors }; | ||
} | ||
} catch (e) { | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a json`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field ${field} is not a json`; | ||
return { result: false, errors }; | ||
@@ -255,6 +245,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -270,6 +258,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -285,6 +271,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -300,6 +284,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -315,6 +297,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -330,6 +310,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -345,6 +323,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -360,6 +336,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -375,6 +349,4 @@ } | ||
}); | ||
if (!res.result) { | ||
this.count = 0; | ||
if (!res.result) | ||
return res; | ||
} | ||
break; | ||
@@ -386,4 +358,3 @@ } | ||
if (typeof value !== schemaType) { | ||
errors.push(errorMessage != null ? errorMessage : `Field ${field} is not a ${schemaType}`); | ||
this.count = 0; | ||
errors[field] = errorMessage != null ? errorMessage : `Field is not a ${schemaType}`; | ||
return { result: false, errors }; | ||
@@ -393,4 +364,2 @@ } | ||
default: | ||
console.dir({ schemaType }); | ||
this.count = 0; | ||
throw new Error(` | ||
@@ -402,4 +371,3 @@ Unexpected type for validation | ||
} | ||
this.count = 0; | ||
return { result: errors.length === 0, errors }; | ||
return { result: Object.keys(errors).length === 0, errors }; | ||
} | ||
@@ -406,0 +374,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { ObjectSchema, SchemeTypes, ValidatableObj, ValidateResult } from "./types"; | ||
import { ObjectSchema, SchemeTypes, ValidatableObj, ValidateResult, ValidationErrors } from "./types"; | ||
import { ValidatorNestedSchema, ValidatorArraySchema, ValidatorOrSchema } from "./validator-helpers"; | ||
@@ -25,4 +25,4 @@ declare class Validator { | ||
static or(schema: Array<ObjectSchema | SchemeTypes>): ValidatorOrSchema; | ||
validate(obj: ValidatableObj, schema?: ObjectSchema, errors?: string[], isOr?: boolean): ValidateResult; | ||
validate(obj: ValidatableObj, schema?: ObjectSchema, /* Need for Recursive calls */ errors?: ValidationErrors, /* Need for Recursive calls */ isOr?: boolean, /* Need for Recursive calls */ isRecursive?: boolean): ValidateResult; | ||
} | ||
export { Validator }; |
@@ -20,5 +20,8 @@ import { ValidatorNestedSchema, ValidatorArraySchema, ValidatorOrSchema } from "src/validator-helpers"; | ||
}; | ||
type FieldNameStr = string; | ||
type ErrorMessageStr = string; | ||
export type ValidationErrors = Record<FieldNameStr, ErrorMessageStr>; | ||
export type ValidateResult = { | ||
result: boolean; | ||
errors: string[]; | ||
errors: ValidationErrors; | ||
}; | ||
@@ -37,1 +40,2 @@ export type SchemeTypes = typeof SCHEME_TYPES[keyof typeof SCHEME_TYPES]; | ||
export type ValidatableObj = Record<string, unknown>; | ||
export {}; |
{ | ||
"name": "@handy-tools/validator", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Dependency free validator for objects", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -52,3 +52,3 @@ # Validator | ||
} | ||
}); // { result: true, errors: [] } | ||
}); // { result: true, errors: {} } | ||
@@ -61,5 +61,5 @@ const ipScheme = new Validator({ | ||
ip: 'not an ip', | ||
}); // { result: false, errors: [ 'Field ip is invalid' ] } | ||
}); // { result: false, errors: { ip: 'Field is invalid' } } | ||
``` | ||
See [tests](./__tests__/common.ts) for more examples. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29990
825