Socket
Socket
Sign inDemoInstall

@spinajs/validation

Package Overview
Dependencies
Maintainers
1
Versions
267
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@spinajs/validation - npm Package Compare versions

Comparing version 1.1.3 to 1.1.5

lib/sources.d.ts

10

lib/config/validation.d.ts

@@ -1,1 +0,9 @@

export {};
declare const config: {
validation: {
allErrors: boolean;
removeAdditional: boolean;
useDefaults: boolean;
coerceTypes: boolean;
};
};
export default config;

12

lib/config/validation.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
function dir(path) {
return (0, path_1.resolve)((0, path_1.normalize)((0, path_1.join)(__dirname, path)));
}
module.exports = {
system: {
dirs: {
schemas: [dir('./../schemas')],
}
},
const config = {
validation: {

@@ -24,2 +15,3 @@ // enable all errors on validation, not only first one that occurred

};
exports.default = config;
//# sourceMappingURL=validation.js.map

@@ -13,3 +13,3 @@ "use strict";

return (target) => {
Reflect.defineMetadata(exports.SCHEMA_SYMBOL, schema, target);
Reflect.defineMetadata(exports.SCHEMA_SYMBOL, schema, target.prototype || target);
};

@@ -16,0 +16,0 @@ }

@@ -8,5 +8,5 @@ import { Exception } from "@spinajs/exceptions";

parameter: any;
constructor(message: string, validationErrors: ValidationError[]);
constructor(message: string, validationErrors: IValidationError[]);
}
export interface ValidationError extends ErrorObject {
export interface IValidationError extends ErrorObject {
}
import { SyncModule, IContainer } from "@spinajs/di";
import Ajv from "ajv";
import { Configuration } from "@spinajs/configuration";
import { ValidationError } from "./exceptions";
import { IValidationError } from "./exceptions";
import { SchemaSource } from "./sources";
export declare class DataValidator extends SyncModule {
Options: any;
SchemaDirs: string[];
protected Sources: SchemaSource[];
protected Validator: Ajv;
protected Configuration: Configuration;
protected Container: IContainer;
resolve(container: IContainer): void;
addSchema(schemaObject: any, identifier: string): void;
/**
*
* Checks if schema is loaded ( from file )
*
* @param schemaId schema id to check
* @returns { boolean } true if schema is loaded, false otherwise
*/
hasSchema(schemaId: string): boolean;
/**
* Tries to validate given data

@@ -19,3 +27,3 @@ *

*/
tryValidate(data: any): [boolean, ValidationError[]];
tryValidate(data: any): [boolean, IValidationError[]];
/**

@@ -29,3 +37,3 @@ * Tries to validate given data

*/
tryValidate(schema: object | string, data: any): [boolean, ValidationError[]];
tryValidate(schema: object | string, data: any): [boolean, IValidationError[]];
/**

@@ -32,0 +40,0 @@ * Validate given data. When failed, exception is thrown

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {

@@ -20,9 +8,2 @@ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;

};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {

@@ -39,12 +20,19 @@ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);

const configuration_1 = require("@spinajs/configuration");
const fs = __importStar(require("fs"));
const glob = __importStar(require("glob"));
const path = __importStar(require("path"));
const exceptions_1 = require("./exceptions");
const exceptions_2 = require("@spinajs/exceptions");
const decorators_1 = require("./decorators");
const log_1 = require("@spinajs/log/lib/log");
const sources_1 = require("./sources");
class DataValidator extends di_1.SyncModule {
resolve(container) {
this.Container = container;
this.Validator = new ajv_1.default(this.Options);
this.Sources = container.resolve(Array.ofType(sources_1.SchemaSource));
if (!this.Sources || this.Sources.length === 0) {
throw new exceptions_2.InvalidOperation("No schema sources avaible. Register any in DI container");
}
const ajvConfig = Object.assign({ logger: {
log: (msg) => log_1.Log.info(msg, "validation"),
warn: (msg) => log_1.Log.warn(msg, "validation"),
error: (msg) => log_1.Log.error(msg, "validation")
} }, this.Options);
this.Validator = new ajv_1.default(ajvConfig);
// add $merge & $patch for json schema

@@ -56,14 +44,41 @@ require("ajv-merge-patch")(this.Validator);

require("ajv-keywords")(this.Validator);
this.SchemaDirs.filter(dir => fs.existsSync(dir))
.flatMap((d) => glob.sync(path.join(d, "/**/*.+(json|js)")))
.map(f => {
return {
schema: require(f),
file: path.basename(f)
};
})
.filter(s => this.Validator.validateSchema(s.schema))
.forEach(s => { var _a; return this.Validator.addSchema(s.schema, (_a = s.schema.$id) !== null && _a !== void 0 ? _a : path.basename(s.file)); });
this.Sources.map(x => x.Load()).reduce((prev, curr) => {
return prev.concat(curr);
}, []).filter(s => {
// validate schema can throw sometimes
try {
const vResult = this.Validator.validateSchema(s.schema, true);
if (!vResult) {
log_1.Log.error(`Schema at ${s.file} invalid`, "validator");
return false;
}
return true;
}
catch (err) {
log_1.Log.error(`Schema at ${s.file} invalid, reason: ${err.message}`, "validator");
return false;
}
}).forEach(s => {
this.addSchema(s.schema, s.schema.$id);
});
super.resolve(container);
}
addSchema(schemaObject, identifier) {
if (!this.hasSchema(identifier)) {
this.Validator.addSchema(schemaObject, identifier);
log_1.Log.trace(`Schema ${identifier} added !`, "validator");
}
}
/**
*
* Checks if schema is loaded ( from file )
*
* @param schemaId schema id to check
* @returns { boolean } true if schema is loaded, false otherwise
*/
hasSchema(schemaId) {
return !!this.Validator.getSchema(schemaId);
}
tryValidate(schemaOrData, data) {
var _a, _b;
if (arguments.length === 1) {

@@ -81,3 +96,3 @@ const schema = Reflect.getMetadata(decorators_1.SCHEMA_SYMBOL, schemaOrData);

if (!result) {
return [false, this.Validator.errors];
return [false, (_a = this.Validator.errors) !== null && _a !== void 0 ? _a : null];
}

@@ -115,6 +130,6 @@ }

if (!result) {
return [false, this.Validator.errors];
return [false, (_b = this.Validator.errors) !== null && _b !== void 0 ? _b : null];
}
}
return [true, undefined];
return [true, null];
}

@@ -139,7 +154,3 @@ validate(schemaOrData, data) {

], DataValidator.prototype, "Options", void 0);
__decorate([
(0, configuration_1.Config)("system.dirs.schemas"),
__metadata("design:type", Array)
], DataValidator.prototype, "SchemaDirs", void 0);
exports.DataValidator = DataValidator;
//# sourceMappingURL=validator.js.map
{
"name": "@spinajs/validation",
"version": "1.1.3",
"version": "1.1.5",
"description": "validation library for spinajs framework",

@@ -15,3 +15,3 @@ "main": "lib/index.js",

"lint": "tslint -p tsconfig.json",
"prepublishOnly": "",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",

@@ -40,6 +40,6 @@ "version": "npm run format && git add -A src",

"dependencies": {
"@spinajs/configuration": "^1.1.4",
"@spinajs/configuration": "^1.1.6",
"@spinajs/di": "^1.1.7",
"@spinajs/exceptions": "^1.0.5",
"@spinajs/log": "^1.1.1",
"@spinajs/log": "^1.1.5",
"ajv": "^8.8.2",

@@ -46,0 +46,0 @@ "ajv-formats": "^2.1.1",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc