@spinajs/validation 1.2.74 → 1.2.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/bootstrap.d.ts +4 -0
- package/lib/bootstrap.js +15 -0
- package/lib/bootstrap.js.map +1 -0
- package/lib/config/validation.d.ts +9 -0
- package/lib/config/validation.js +16 -0
- package/lib/config/validation.js.map +1 -0
- package/lib/decorators.d.ts +8 -0
- package/lib/decorators.js +18 -0
- package/lib/decorators.js.map +1 -0
- package/lib/exceptions/index.d.ts +11 -0
- package/lib/exceptions/index.js +15 -0
- package/lib/exceptions/index.js.map +1 -0
- package/lib/index.d.ts +13 -0
- package/lib/index.js +35 -0
- package/lib/index.js.map +1 -0
- package/lib/schemas/validation.d.ts +21 -0
- package/lib/schemas/validation.js +16 -0
- package/lib/schemas/validation.js.map +1 -0
- package/lib/sources.d.ts +5 -0
- package/lib/sources.js +63 -0
- package/lib/sources.js.map +1 -0
- package/lib/types.d.ts +17 -0
- package/lib/types.js +7 -0
- package/lib/types.js.map +1 -0
- package/lib/validator.d.ts +51 -0
- package/lib/validator.js +151 -0
- package/lib/validator.js.map +1 -0
- package/package.json +9 -7
package/lib/bootstrap.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ValidatorBootstraper = void 0;
|
|
7
|
+
const di_1 = require("@spinajs/di");
|
|
8
|
+
const validation_1 = __importDefault(require("./schemas/validation"));
|
|
9
|
+
class ValidatorBootstraper extends di_1.Bootstrapper {
|
|
10
|
+
bootstrap() {
|
|
11
|
+
di_1.DI.register(validation_1.default).asValue('__configurationSchema__');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.ValidatorBootstraper = ValidatorBootstraper;
|
|
15
|
+
//# sourceMappingURL=bootstrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":";;;;;;AAAA,oCAA+C;AAC/C,sEAAwD;AAExD,MAAa,oBAAqB,SAAQ,iBAAY;IAC7C,SAAS;QACd,OAAE,CAAC,QAAQ,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAAC,yBAAyB,CAAC,CAAC;IACvE,CAAC;CACF;AAJD,oDAIC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const config = {
|
|
4
|
+
validation: {
|
|
5
|
+
// enable all errors on validation, not only first one that occurred
|
|
6
|
+
allErrors: true,
|
|
7
|
+
// remove properties that are not defined in schema
|
|
8
|
+
removeAdditional: true,
|
|
9
|
+
// set default values if possible
|
|
10
|
+
useDefaults: true,
|
|
11
|
+
// The option coerceTypes allows you to have your data types coerced to the types specified in your schema type keywords
|
|
12
|
+
coerceTypes: true,
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
exports.default = config;
|
|
16
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/config/validation.ts"],"names":[],"mappings":";;AAAA,MAAM,MAAM,GAAG;IACb,UAAU,EAAE;QACV,qEAAqE;QACrE,SAAS,EAAE,IAAI;QAEf,mDAAmD;QACnD,gBAAgB,EAAE,IAAI;QAEtB,iCAAiC;QACjC,WAAW,EAAE,IAAI;QAEjB,wHAAwH;QACxH,WAAW,EAAE,IAAI;KAClB;CACF,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Schema = exports.SCHEMA_SYMBOL = void 0;
|
|
4
|
+
exports.SCHEMA_SYMBOL = Symbol('SCHEMA_SYMBOL');
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* Add schema for object eg. model or dto.
|
|
8
|
+
*
|
|
9
|
+
* @param schema - schema for object or schema name
|
|
10
|
+
*/
|
|
11
|
+
function Schema(schema) {
|
|
12
|
+
return (target) => {
|
|
13
|
+
var _a;
|
|
14
|
+
Reflect.defineMetadata(exports.SCHEMA_SYMBOL, schema, (_a = target.prototype) !== null && _a !== void 0 ? _a : target);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
exports.Schema = Schema;
|
|
18
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../src/decorators.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AAErD;;;;;GAKG;AACH,SAAgB,MAAM,CAAC,MAAuB;IAC5C,OAAO,CAAC,MAAW,EAAE,EAAE;;QACrB,OAAO,CAAC,cAAc,CAAC,qBAAa,EAAE,MAAM,EAAE,MAAA,MAAM,CAAC,SAAS,mCAAI,MAAM,CAAC,CAAC;IAC5E,CAAC,CAAC;AACJ,CAAC;AAJD,wBAIC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Exception } from '@spinajs/exceptions';
|
|
2
|
+
import { ErrorObject } from 'ajv';
|
|
3
|
+
/**
|
|
4
|
+
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
5
|
+
*/
|
|
6
|
+
export declare class ValidationFailed extends Exception {
|
|
7
|
+
parameter: any;
|
|
8
|
+
constructor(message: string, validationErrors: IValidationError[]);
|
|
9
|
+
}
|
|
10
|
+
export interface IValidationError extends ErrorObject {
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValidationFailed = void 0;
|
|
4
|
+
const exceptions_1 = require("@spinajs/exceptions");
|
|
5
|
+
/**
|
|
6
|
+
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
7
|
+
*/
|
|
8
|
+
class ValidationFailed extends exceptions_1.Exception {
|
|
9
|
+
constructor(message, validationErrors) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.parameter = validationErrors;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.ValidationFailed = ValidationFailed;
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/exceptions/index.ts"],"names":[],"mappings":";;;AAAA,oDAAgD;AAEhD;;GAEG;AACH,MAAa,gBAAiB,SAAQ,sBAAS;IAG7C,YAAY,OAAe,EAAE,gBAAoC;QAC/D,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,SAAS,GAAG,gBAAgB,CAAC;IACpC,CAAC;CACF;AAPD,4CAOC"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* We export default configuration for webpack modules
|
|
3
|
+
* Normally we load configuration from disk via filesystem
|
|
4
|
+
* But webpack is bundlig all files into one.
|
|
5
|
+
*
|
|
6
|
+
* When we export, we can see configuration variable
|
|
7
|
+
* in webpack module cache and webpack config loader can see it
|
|
8
|
+
*/
|
|
9
|
+
export { default } from './config/validation';
|
|
10
|
+
export * from './decorators';
|
|
11
|
+
export * from './exceptions';
|
|
12
|
+
export * from './sources';
|
|
13
|
+
export * from './validator';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.default = void 0;
|
|
21
|
+
/**
|
|
22
|
+
* We export default configuration for webpack modules
|
|
23
|
+
* Normally we load configuration from disk via filesystem
|
|
24
|
+
* But webpack is bundlig all files into one.
|
|
25
|
+
*
|
|
26
|
+
* When we export, we can see configuration variable
|
|
27
|
+
* in webpack module cache and webpack config loader can see it
|
|
28
|
+
*/
|
|
29
|
+
var validation_1 = require("./config/validation");
|
|
30
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(validation_1).default; } });
|
|
31
|
+
__exportStar(require("./decorators"), exports);
|
|
32
|
+
__exportStar(require("./exceptions"), exports);
|
|
33
|
+
__exportStar(require("./sources"), exports);
|
|
34
|
+
__exportStar(require("./validator"), exports);
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;GAOG;AACH,kDAA8C;AAArC,sHAAA,OAAO,OAAA;AAChB,+CAA6B;AAC7B,+CAA6B;AAC7B,4CAA0B;AAC1B,8CAA4B"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
declare const CONFIGURATION_SCHEMA: {
|
|
2
|
+
$id: string;
|
|
3
|
+
$configurationModule: string;
|
|
4
|
+
description: string;
|
|
5
|
+
type: string;
|
|
6
|
+
properties: {
|
|
7
|
+
allErrors: {
|
|
8
|
+
type: string;
|
|
9
|
+
};
|
|
10
|
+
removeAdditional: {
|
|
11
|
+
type: string;
|
|
12
|
+
};
|
|
13
|
+
useDefaults: {
|
|
14
|
+
type: string;
|
|
15
|
+
};
|
|
16
|
+
coerceTypes: {
|
|
17
|
+
type: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export default CONFIGURATION_SCHEMA;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const CONFIGURATION_SCHEMA = {
|
|
4
|
+
$id: 'spinajs/validation.configuration.schema.json',
|
|
5
|
+
$configurationModule: 'validation',
|
|
6
|
+
description: 'Validation module configuration schema',
|
|
7
|
+
type: 'object',
|
|
8
|
+
properties: {
|
|
9
|
+
allErrors: { type: 'boolean' },
|
|
10
|
+
removeAdditional: { type: 'boolean' },
|
|
11
|
+
useDefaults: { type: 'boolean' },
|
|
12
|
+
coerceTypes: { type: 'boolean' },
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
exports.default = CONFIGURATION_SCHEMA;
|
|
16
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/schemas/validation.ts"],"names":[],"mappings":";;AAAA,MAAM,oBAAoB,GAAG;IAC3B,GAAG,EAAE,8CAA8C;IACnD,oBAAoB,EAAE,YAAY;IAClC,WAAW,EAAE,wCAAwC;IACrD,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE;QACV,SAAS,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;QAC9B,gBAAgB,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;QACrC,WAAW,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;QAChC,WAAW,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE;KACjC;CACF,CAAC;AAEF,kBAAe,oBAAoB,CAAC"}
|
package/lib/sources.d.ts
ADDED
package/lib/sources.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
25
|
+
if (mod && mod.__esModule) return mod;
|
|
26
|
+
var result = {};
|
|
27
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
28
|
+
__setModuleDefault(result, mod);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
32
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.FileSystemSource = void 0;
|
|
36
|
+
const lib_1 = require("@spinajs/configuration/lib");
|
|
37
|
+
const di_1 = require("@spinajs/di");
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const glob = __importStar(require("glob"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
const types_1 = require("./types");
|
|
42
|
+
let FileSystemSource = class FileSystemSource extends types_1.SchemaSource {
|
|
43
|
+
Load() {
|
|
44
|
+
return this.SchemaDirs.filter((dir) => fs.existsSync(dir))
|
|
45
|
+
.flatMap((d) => glob.sync(path.join(d, '/**/*.+(json|js)')))
|
|
46
|
+
.map((f) => {
|
|
47
|
+
//Log.trace(`Found schema at: ${f}`, 'validation');
|
|
48
|
+
return {
|
|
49
|
+
schema: require(f),
|
|
50
|
+
file: path.basename(f),
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
__decorate([
|
|
56
|
+
(0, lib_1.Config)('system.dirs.schemas'),
|
|
57
|
+
__metadata("design:type", Array)
|
|
58
|
+
], FileSystemSource.prototype, "SchemaDirs", void 0);
|
|
59
|
+
FileSystemSource = __decorate([
|
|
60
|
+
(0, di_1.Injectable)(types_1.SchemaSource)
|
|
61
|
+
], FileSystemSource);
|
|
62
|
+
exports.FileSystemSource = FileSystemSource;
|
|
63
|
+
//# sourceMappingURL=sources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.js","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,oDAAoD;AACpD,oCAAyC;AACzC,uCAAyB;AACzB,2CAA6B;AAC7B,2CAA6B;AAC7B,mCAA+D;AAG/D,IAAa,gBAAgB,GAA7B,MAAa,gBAAiB,SAAQ,oBAAY;IAIzC,IAAI;QACT,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;aACvD,OAAO,CAAC,CAAC,CAAS,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAAC;aACnE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YACT,mDAAmD;YAEnD,OAAO;gBACL,MAAM,EAAE,OAAO,CAAC,CAAC,CAAkB;gBACnC,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;aACvB,CAAC;QACJ,CAAC,CAAC,CAAC;IACP,CAAC;CACF,CAAA;AAdC;IADC,IAAA,YAAM,EAAC,qBAAqB,CAAC;;oDACF;AAFjB,gBAAgB;IAD5B,IAAA,eAAU,EAAC,oBAAY,CAAC;GACZ,gBAAgB,CAgB5B;AAhBY,4CAAgB"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ISchema {
|
|
2
|
+
schema: ISchemaObject;
|
|
3
|
+
file?: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ISchemaObject {
|
|
6
|
+
$id: string;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}
|
|
9
|
+
export declare abstract class SchemaSource {
|
|
10
|
+
abstract Load(): ISchema[];
|
|
11
|
+
}
|
|
12
|
+
export interface IValidationOptions {
|
|
13
|
+
allErrors: boolean;
|
|
14
|
+
removeAdditional: boolean;
|
|
15
|
+
useDefaults: boolean;
|
|
16
|
+
coerceTypes: boolean;
|
|
17
|
+
}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAUA,MAAsB,YAAY;CAEjC;AAFD,oCAEC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SyncModule, Container } from '@spinajs/di';
|
|
2
|
+
import Ajv from 'ajv';
|
|
3
|
+
import { IValidationError } from './exceptions';
|
|
4
|
+
import { IValidationOptions, SchemaSource } from './types';
|
|
5
|
+
import { ILog } from '@spinajs/log';
|
|
6
|
+
export declare class DataValidator extends SyncModule {
|
|
7
|
+
Options: IValidationOptions;
|
|
8
|
+
protected Sources: SchemaSource[];
|
|
9
|
+
protected Log: ILog;
|
|
10
|
+
protected Validator: Ajv;
|
|
11
|
+
protected Container: Container;
|
|
12
|
+
resolve(): void;
|
|
13
|
+
addSchema(schemaObject: object, identifier: string): void;
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
* Checks if schema is loaded ( from file )
|
|
17
|
+
*
|
|
18
|
+
* @param schemaId - schema id to check
|
|
19
|
+
* @returns true if schema is loaded, false otherwise
|
|
20
|
+
*/
|
|
21
|
+
hasSchema(schemaId: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Tries to validate given data
|
|
24
|
+
*
|
|
25
|
+
* @param data - data to validate. Function will try to extract schema attached to object via `@Schema` decorator
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
tryValidate(data: object): [boolean, IValidationError[]];
|
|
29
|
+
/**
|
|
30
|
+
* Tries to validate given data
|
|
31
|
+
*
|
|
32
|
+
* @param schemaKeyRef - key, ref or schema object
|
|
33
|
+
* @param data - to be validated
|
|
34
|
+
*/
|
|
35
|
+
tryValidate(schema: object | string, data: object): [boolean, IValidationError[]];
|
|
36
|
+
/**
|
|
37
|
+
* Validate given data. When failed, exception is thrown
|
|
38
|
+
*
|
|
39
|
+
* @param data - data to validate. Function will try to extract schema attached to object via `@Schema` decorator
|
|
40
|
+
* @throws {@link InvalidArgument | ValidationFailed }
|
|
41
|
+
*/
|
|
42
|
+
validate(data: object): void;
|
|
43
|
+
/**
|
|
44
|
+
* Validate given data
|
|
45
|
+
*
|
|
46
|
+
* @param schemaKeyRef - key, ref or schema object
|
|
47
|
+
* @param data - to be validated
|
|
48
|
+
* @throws {@link InvalidArgumen | ValidationFailed }
|
|
49
|
+
*/
|
|
50
|
+
validate(schema: object | string, data: object): void;
|
|
51
|
+
}
|
package/lib/validator.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.DataValidator = void 0;
|
|
16
|
+
const di_1 = require("@spinajs/di");
|
|
17
|
+
const ajv_1 = __importDefault(require("ajv"));
|
|
18
|
+
const configuration_1 = require("@spinajs/configuration");
|
|
19
|
+
const exceptions_1 = require("./exceptions");
|
|
20
|
+
const exceptions_2 = require("@spinajs/exceptions");
|
|
21
|
+
const decorators_1 = require("./decorators");
|
|
22
|
+
const types_1 = require("./types");
|
|
23
|
+
const log_1 = require("@spinajs/log");
|
|
24
|
+
class DataValidator extends di_1.SyncModule {
|
|
25
|
+
resolve() {
|
|
26
|
+
if (!this.Sources || this.Sources.length === 0) {
|
|
27
|
+
throw new exceptions_2.InvalidOperation('No schema sources avaible. Register any in DI container');
|
|
28
|
+
}
|
|
29
|
+
const ajvConfig = Object.assign({ logger: {
|
|
30
|
+
log: (msg) => this.Log.info(msg),
|
|
31
|
+
warn: (msg) => this.Log.warn(msg),
|
|
32
|
+
error: (msg) => this.Log.error(msg),
|
|
33
|
+
} }, this.Options);
|
|
34
|
+
this.Validator = new ajv_1.default(ajvConfig);
|
|
35
|
+
// add $merge & $patch for json schema
|
|
36
|
+
require('ajv-merge-patch')(this.Validator);
|
|
37
|
+
// add common formats validation eg: date time
|
|
38
|
+
require('ajv-formats')(this.Validator);
|
|
39
|
+
// add keywords
|
|
40
|
+
require('ajv-keywords')(this.Validator);
|
|
41
|
+
this.Sources.map((x) => x.Load())
|
|
42
|
+
.reduce((prev, curr) => {
|
|
43
|
+
return prev.concat(curr);
|
|
44
|
+
}, [])
|
|
45
|
+
.filter((s) => {
|
|
46
|
+
// validate schema can throw sometimes
|
|
47
|
+
try {
|
|
48
|
+
const vResult = this.Validator.validateSchema(s.schema, true);
|
|
49
|
+
if (!vResult) {
|
|
50
|
+
this.Log.error(`Schema at ${s.file} invalid`, 'validator');
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
this.Log.error(`Schema at ${s.file} invalid, reason: ${err.message}`, 'validator');
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
.forEach((s) => {
|
|
61
|
+
this.addSchema(s.schema, s.schema.$id);
|
|
62
|
+
});
|
|
63
|
+
super.resolve();
|
|
64
|
+
}
|
|
65
|
+
addSchema(schemaObject, identifier) {
|
|
66
|
+
if (!this.hasSchema(identifier)) {
|
|
67
|
+
this.Validator.addSchema(schemaObject, identifier);
|
|
68
|
+
this.Log.trace(`Schema ${identifier} added !`, 'validator');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
*
|
|
73
|
+
* Checks if schema is loaded ( from file )
|
|
74
|
+
*
|
|
75
|
+
* @param schemaId - schema id to check
|
|
76
|
+
* @returns true if schema is loaded, false otherwise
|
|
77
|
+
*/
|
|
78
|
+
hasSchema(schemaId) {
|
|
79
|
+
return !!this.Validator.getSchema(schemaId);
|
|
80
|
+
}
|
|
81
|
+
tryValidate(schemaOrData, data) {
|
|
82
|
+
var _a, _b, _c;
|
|
83
|
+
let schema = null;
|
|
84
|
+
if (!data) {
|
|
85
|
+
schema = Reflect.getMetadata(decorators_1.SCHEMA_SYMBOL, schemaOrData);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (typeof schemaOrData === 'object') {
|
|
89
|
+
schema = schemaOrData;
|
|
90
|
+
}
|
|
91
|
+
else if (typeof schemaOrData === 'string') {
|
|
92
|
+
/* eslint-disable */
|
|
93
|
+
schema = (_b = (_a = this.Validator.getSchema(schemaOrData)) === null || _a === void 0 ? void 0 : _a.schema) !== null && _b !== void 0 ? _b : null;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
schema = Reflect.getMetadata(decorators_1.SCHEMA_SYMBOL, schemaOrData);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (!schema) {
|
|
100
|
+
if (!schema) {
|
|
101
|
+
return [
|
|
102
|
+
false,
|
|
103
|
+
[
|
|
104
|
+
{
|
|
105
|
+
keyword: 'empty_schema',
|
|
106
|
+
instancePath: './',
|
|
107
|
+
schemaPath: '',
|
|
108
|
+
params: { data: '' },
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const result = this.Validator.validate(schema, data ? data : schemaOrData);
|
|
115
|
+
if (!result) {
|
|
116
|
+
return [false, (_c = this.Validator.errors) !== null && _c !== void 0 ? _c : null];
|
|
117
|
+
}
|
|
118
|
+
return [true, null];
|
|
119
|
+
}
|
|
120
|
+
validate(schemaOrData, data) {
|
|
121
|
+
const [isValid, errors] = this.tryValidate(schemaOrData, data);
|
|
122
|
+
if (!isValid) {
|
|
123
|
+
switch (errors[0].keyword) {
|
|
124
|
+
case 'invalid_argument':
|
|
125
|
+
throw new exceptions_2.InvalidArgument('data is null or undefined');
|
|
126
|
+
case 'empty_schema':
|
|
127
|
+
throw new exceptions_2.InvalidArgument('objects schema is not set');
|
|
128
|
+
default:
|
|
129
|
+
throw new exceptions_1.ValidationFailed('validation error', errors);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
__decorate([
|
|
135
|
+
(0, configuration_1.Config)('validation'),
|
|
136
|
+
__metadata("design:type", Object)
|
|
137
|
+
], DataValidator.prototype, "Options", void 0);
|
|
138
|
+
__decorate([
|
|
139
|
+
(0, di_1.Autoinject)(types_1.SchemaSource),
|
|
140
|
+
__metadata("design:type", Array)
|
|
141
|
+
], DataValidator.prototype, "Sources", void 0);
|
|
142
|
+
__decorate([
|
|
143
|
+
(0, log_1.Logger)('validation'),
|
|
144
|
+
__metadata("design:type", Object)
|
|
145
|
+
], DataValidator.prototype, "Log", void 0);
|
|
146
|
+
__decorate([
|
|
147
|
+
(0, di_1.Autoinject)(),
|
|
148
|
+
__metadata("design:type", di_1.Container)
|
|
149
|
+
], DataValidator.prototype, "Container", void 0);
|
|
150
|
+
exports.DataValidator = DataValidator;
|
|
151
|
+
//# sourceMappingURL=validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../src/validator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oCAAgE;AAChE,8CAAsB;AACtB,0DAAgD;AAChD,6CAAkE;AAClE,oDAAwE;AACxE,6CAA6C;AAC7C,mCAA0E;AAC1E,sCAA4C;AAE5C,MAAa,aAAc,SAAQ,eAAU;IAepC,OAAO;QACZ,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;YAC9C,MAAM,IAAI,6BAAgB,CAAC,yDAAyD,CAAC,CAAC;SACvF;QAED,MAAM,SAAS,mBACb,MAAM,EAAE;gBACN,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;gBACxC,IAAI,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;gBACzC,KAAK,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC;aAC5C,IACE,IAAI,CAAC,OAAO,CAChB,CAAC;QAEF,IAAI,CAAC,SAAS,GAAG,IAAI,aAAG,CAAC,SAAS,CAAC,CAAC;QAEpC,sCAAsC;QACtC,OAAO,CAAC,iBAAiB,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAE3C,8CAA8C;QAC9C,OAAO,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEvC,eAAe;QACf,OAAO,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAExC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,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AC;IACtB,CAAC;IAkBM,QAAQ,CAAC,YAA6B,EAAE,IAAa;QAC1D,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,IAAI,CAAC,WAAW,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;QAC/D,IAAI,CAAC,OAAO,EAAE;YACZ,QAAQ,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE;gBACzB,KAAK,kBAAkB;oBACrB,MAAM,IAAI,4BAAe,CAAC,2BAA2B,CAAC,CAAC;gBACzD,KAAK,cAAc;oBACjB,MAAM,IAAI,4BAAe,CAAC,2BAA2B,CAAC,CAAC;gBACzD;oBACE,MAAM,IAAI,6BAAgB,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC;aAC1D;SACF;IACH,CAAC;CACF;AAtKC;IADC,IAAA,sBAAM,EAAC,YAAY,CAAC;;8CACc;AAGnC;IADC,IAAA,eAAU,EAAC,oBAAY,CAAC;;8CACS;AAGlC;IADC,IAAA,YAAM,EAAC,YAAY,CAAC;;0CACD;AAKpB;IADC,IAAA,eAAU,GAAE;8BACQ,cAAS;gDAAC;AAbjC,sCAwKC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spinajs/validation",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.81",
|
|
4
4
|
"description": "validation library for spinajs framework",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -8,8 +8,10 @@
|
|
|
8
8
|
"test": "ts-mocha -p tsconfig.json test/**/*.test.ts",
|
|
9
9
|
"coverage": "nyc npm run test",
|
|
10
10
|
"build-docs": "rimraf docs && typedoc --options typedoc.json src/",
|
|
11
|
-
"build": "
|
|
11
|
+
"build": "npm run clean && npm run compile",
|
|
12
|
+
"compile": "tsc -p tsconfig.build.json",
|
|
12
13
|
"prepare": "npm run build",
|
|
14
|
+
"clean": "",
|
|
13
15
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
14
16
|
"lint": "eslint -c .eslintrc.js --ext .ts src",
|
|
15
17
|
"prepublishOnly": "npm test && npm run lint",
|
|
@@ -36,15 +38,15 @@
|
|
|
36
38
|
},
|
|
37
39
|
"homepage": "https://github.com/spinajs/validation#readme",
|
|
38
40
|
"dependencies": {
|
|
39
|
-
"@spinajs/configuration": "^1.2.
|
|
40
|
-
"@spinajs/di": "^1.2.
|
|
41
|
-
"@spinajs/exceptions": "^1.2.
|
|
42
|
-
"@spinajs/log": "^1.2.
|
|
41
|
+
"@spinajs/configuration": "^1.2.81",
|
|
42
|
+
"@spinajs/di": "^1.2.81",
|
|
43
|
+
"@spinajs/exceptions": "^1.2.81",
|
|
44
|
+
"@spinajs/log": "^1.2.81",
|
|
43
45
|
"ajv": "^8.8.2",
|
|
44
46
|
"ajv-formats": "^2.1.1",
|
|
45
47
|
"ajv-keywords": "^5.1.0",
|
|
46
48
|
"ajv-merge-patch": "^5.0.1",
|
|
47
49
|
"lodash": "^4.17.21"
|
|
48
50
|
},
|
|
49
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "dd58385e770069586eb8814a13365ce889d46b34"
|
|
50
52
|
}
|