@alanszp/validations 4.0.13

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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ *.log
3
+ dist
package/.npmignore ADDED
@@ -0,0 +1,3 @@
1
+ node_modules
2
+ src
3
+ *.log
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Alan Szpigiel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ /**
2
+ * BaseModel represents a va
3
+ */
4
+ export declare abstract class BaseModel {
5
+ validate(): Promise<void>;
6
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.BaseModel = void 0;
13
+ const class_validator_1 = require("class-validator");
14
+ const ModelValidationError_1 = require("./ModelValidationError");
15
+ /**
16
+ * BaseModel represents a va
17
+ */
18
+ class BaseModel {
19
+ // TODO: This model is shared between baseEntity and baseModel. Do a mixin.
20
+ validate() {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ const validationResult = yield (0, class_validator_1.validate)(this);
23
+ if (validationResult.length > 0) {
24
+ return Promise.reject(new ModelValidationError_1.ModelValidationError(validationResult));
25
+ }
26
+ return Promise.resolve();
27
+ });
28
+ }
29
+ }
30
+ exports.BaseModel = BaseModel;
31
+ //# sourceMappingURL=BaseModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseModel.js","sourceRoot":"","sources":["../src/BaseModel.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAA2C;AAC3C,iEAA8D;AAE9D;;GAEG;AACH,MAAsB,SAAS;IAC7B,2EAA2E;IAC9D,QAAQ;;YACnB,MAAM,gBAAgB,GAAG,MAAM,IAAA,0BAAQ,EAAC,IAAI,CAAC,CAAC;YAE9C,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE;gBAC/B,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,2CAAoB,CAAC,gBAAgB,CAAC,CAAC,CAAC;aACnE;YACD,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;QAC3B,CAAC;KAAA;CACF;AAVD,8BAUC"}
@@ -0,0 +1,16 @@
1
+ import { ValidationError } from "class-validator";
2
+ import { BaseError, RenderableError, RenderableContext } from "@alanszp/errors";
3
+ export interface ValidationObject {
4
+ property: string;
5
+ constraints: {
6
+ [type: string]: string;
7
+ };
8
+ }
9
+ export declare class ModelValidationError extends BaseError implements RenderableError {
10
+ errors: ValidationError[];
11
+ constructor(errors: ValidationError[]);
12
+ static from(objects: ValidationObject | ValidationObject[]): ModelValidationError;
13
+ renderMessage(): string;
14
+ code(): string;
15
+ context(): RenderableContext;
16
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ModelValidationError = void 0;
4
+ const class_validator_1 = require("class-validator");
5
+ const lodash_1 = require("lodash");
6
+ const errors_1 = require("@alanszp/errors");
7
+ class ModelValidationError extends errors_1.BaseError {
8
+ constructor(errors) {
9
+ super("Model Validation Error");
10
+ this.errors = errors;
11
+ }
12
+ static from(objects) {
13
+ const arrayObjects = (0, lodash_1.isArray)(objects) ? objects : [objects];
14
+ const validationErrors = arrayObjects.map((o) => {
15
+ const validationError = new class_validator_1.ValidationError();
16
+ validationError.property = o.property;
17
+ validationError.children = [];
18
+ validationError.constraints = o.constraints;
19
+ return validationError;
20
+ });
21
+ return new ModelValidationError(validationErrors);
22
+ }
23
+ renderMessage() {
24
+ return "Model validation error";
25
+ }
26
+ code() {
27
+ return "model_validation_error";
28
+ }
29
+ context() {
30
+ return {
31
+ errors: this.errors.map((e) => ({
32
+ property: e.property,
33
+ errors: e.constraints,
34
+ children: e.children,
35
+ })),
36
+ };
37
+ }
38
+ }
39
+ exports.ModelValidationError = ModelValidationError;
40
+ //# sourceMappingURL=ModelValidationError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ModelValidationError.js","sourceRoot":"","sources":["../src/ModelValidationError.ts"],"names":[],"mappings":";;;AAAA,qDAAkD;AAClD,mCAAiC;AACjC,4CAAgF;AAOhF,MAAa,oBAAqB,SAAQ,kBAAS;IAGjD,YAAY,MAAyB;QACnC,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;IAEM,MAAM,CAAC,IAAI,CAChB,OAA8C;QAE9C,MAAM,YAAY,GAAG,IAAA,gBAAO,EAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;QAE5D,MAAM,gBAAgB,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;YAC9C,MAAM,eAAe,GAAG,IAAI,iCAAe,EAAE,CAAC;YAE9C,eAAe,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;YACtC,eAAe,CAAC,QAAQ,GAAG,EAAE,CAAC;YAC9B,eAAe,CAAC,WAAW,GAAG,CAAC,CAAC,WAAW,CAAC;YAE5C,OAAO,eAAe,CAAC;QACzB,CAAC,CAAC,CAAC;QAEH,OAAO,IAAI,oBAAoB,CAAC,gBAAgB,CAAC,CAAC;IACpD,CAAC;IAEM,aAAa;QAClB,OAAO,wBAAwB,CAAC;IAClC,CAAC;IAEM,IAAI;QACT,OAAO,wBAAwB,CAAC;IAClC,CAAC;IAEM,OAAO;QACZ,OAAO;YACL,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;gBAC9B,QAAQ,EAAE,CAAC,CAAC,QAAQ;gBACpB,MAAM,EAAE,CAAC,CAAC,WAAW;gBACrB,QAAQ,EAAE,CAAC,CAAC,QAAQ;aACrB,CAAC,CAAC;SACJ,CAAC;IACJ,CAAC;CACF;AA3CD,oDA2CC"}
@@ -0,0 +1,2 @@
1
+ import { ValidationOptions } from "class-validator";
2
+ export declare function IsJustEditable(validationOptions?: ValidationOptions): (object: object, propertyName: string) => void;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IsJustEditable = void 0;
4
+ const class_validator_1 = require("class-validator");
5
+ function IsJustEditable(validationOptions) {
6
+ // eslint-disable-next-line @typescript-eslint/ban-types
7
+ return function justEditableDecorator(object, propertyName) {
8
+ (0, class_validator_1.registerDecorator)({
9
+ name: class_validator_1.ValidationTypes.CONDITIONAL_VALIDATION,
10
+ target: object.constructor,
11
+ propertyName,
12
+ constraints: [(_object, value) => value !== undefined],
13
+ options: validationOptions,
14
+ validator: {
15
+ validate(value) {
16
+ return value !== null;
17
+ },
18
+ defaultMessage() {
19
+ return `${propertyName} should not be defined or different than null`;
20
+ },
21
+ },
22
+ });
23
+ };
24
+ }
25
+ exports.IsJustEditable = IsJustEditable;
26
+ //# sourceMappingURL=IsJustEditable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IsJustEditable.js","sourceRoot":"","sources":["../../src/common-validators/IsJustEditable.ts"],"names":[],"mappings":";;;AAAA,qDAAwF;AAExF,SAAgB,cAAc,CAAC,iBAAqC;IAClE,wDAAwD;IACxD,OAAO,SAAS,qBAAqB,CAAC,MAAc,EAAE,YAAoB;QACxE,IAAA,mCAAiB,EAAC;YAChB,IAAI,EAAE,iCAAe,CAAC,sBAAsB;YAC5C,MAAM,EAAE,MAAM,CAAC,WAAW;YAC1B,YAAY;YACZ,WAAW,EAAE,CAAC,CAAC,OAAgB,EAAE,KAAc,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC;YACxE,OAAO,EAAE,iBAAiB;YAC1B,SAAS,EAAE;gBACT,QAAQ,CAAC,KAAc;oBACrB,OAAO,KAAK,KAAK,IAAI,CAAC;gBACxB,CAAC;gBACD,cAAc;oBACZ,OAAO,GAAG,YAAY,+CAA+C,CAAC;gBACxE,CAAC;aACF;SACF,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAnBD,wCAmBC"}
@@ -0,0 +1 @@
1
+ export * from "./IsJustEditable";
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./IsJustEditable"), exports);
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/common-validators/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,mDAAiC"}
@@ -0,0 +1 @@
1
+ export declare function entityOrValidationError<T>(entity: T | undefined, property: string): T;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.entityOrValidationError = void 0;
4
+ const ModelValidationError_1 = require("./ModelValidationError");
5
+ function entityOrValidationError(entity, property) {
6
+ if (!entity) {
7
+ throw ModelValidationError_1.ModelValidationError.from({
8
+ property,
9
+ constraints: {
10
+ mustBePresent: `${property} is not present`,
11
+ },
12
+ });
13
+ }
14
+ return entity;
15
+ }
16
+ exports.entityOrValidationError = entityOrValidationError;
17
+ //# sourceMappingURL=entityOrValidationError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entityOrValidationError.js","sourceRoot":"","sources":["../src/entityOrValidationError.ts"],"names":[],"mappings":";;;AAAA,iEAA8D;AAE9D,SAAgB,uBAAuB,CACrC,MAAqB,EACrB,QAAgB;IAEhB,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,2CAAoB,CAAC,IAAI,CAAC;YAC9B,QAAQ;YACR,WAAW,EAAE;gBACX,aAAa,EAAE,GAAG,QAAQ,iBAAiB;aAC5C;SACF,CAAC,CAAC;KACJ;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAbD,0DAaC"}
@@ -0,0 +1,4 @@
1
+ export * from "./BaseModel";
2
+ export * from "./ModelValidationError";
3
+ export * from "./entityOrValidationError";
4
+ export * from "./common-validators";
package/dist/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./BaseModel"), exports);
14
+ __exportStar(require("./ModelValidationError"), exports);
15
+ __exportStar(require("./entityOrValidationError"), exports);
16
+ __exportStar(require("./common-validators"), exports);
17
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,8CAA4B;AAC5B,yDAAuC;AACvC,4DAA0C;AAC1C,sDAAoC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@alanszp/validations",
3
+ "version": "4.0.13",
4
+ "description": "Alan's validation utils.",
5
+ "main": "dist/index.js",
6
+ "typings": "dist/index.d.ts",
7
+ "license": "MIT",
8
+ "files": [
9
+ "**/*"
10
+ ],
11
+ "publishConfig": {
12
+ "access": "public"
13
+ },
14
+ "scripts": {
15
+ "compile": "rm -rf ./dist && tsc --declaration",
16
+ "compile-watch": "tsc -w",
17
+ "build": "yarn run compile",
18
+ "prepack": "yarn run build",
19
+ "yalc-publish": "yarn run yalc publish"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^15.12.3",
23
+ "class-validator": "^0.13.1",
24
+ "ts-node": "^10.0.0",
25
+ "tslint": "^6.1.3",
26
+ "typescript": "^4.3.4"
27
+ },
28
+ "peerDependencies": {
29
+ "class-validator": "^0.13.1"
30
+ },
31
+ "dependencies": {
32
+ "@alanszp/errors": "^4.0.5"
33
+ },
34
+ "gitHead": "71a7bfa562fe5cfc52262f6bb4cadd6b977c6a4c"
35
+ }
@@ -0,0 +1,17 @@
1
+ import { validate } from "class-validator";
2
+ import { ModelValidationError } from "./ModelValidationError";
3
+
4
+ /**
5
+ * BaseModel represents a va
6
+ */
7
+ export abstract class BaseModel {
8
+ // TODO: This model is shared between baseEntity and baseModel. Do a mixin.
9
+ public async validate(): Promise<void> {
10
+ const validationResult = await validate(this);
11
+
12
+ if (validationResult.length > 0) {
13
+ return Promise.reject(new ModelValidationError(validationResult));
14
+ }
15
+ return Promise.resolve();
16
+ }
17
+ }
@@ -0,0 +1,53 @@
1
+ import { ValidationError } from "class-validator";
2
+ import { isArray } from "lodash";
3
+ import { BaseError, RenderableError, RenderableContext } from "@alanszp/errors";
4
+
5
+ export interface ValidationObject {
6
+ property: string;
7
+ constraints: { [type: string]: string };
8
+ }
9
+
10
+ export class ModelValidationError extends BaseError implements RenderableError {
11
+ public errors: ValidationError[];
12
+
13
+ constructor(errors: ValidationError[]) {
14
+ super("Model Validation Error");
15
+ this.errors = errors;
16
+ }
17
+
18
+ public static from(
19
+ objects: ValidationObject | ValidationObject[]
20
+ ): ModelValidationError {
21
+ const arrayObjects = isArray(objects) ? objects : [objects];
22
+
23
+ const validationErrors = arrayObjects.map((o) => {
24
+ const validationError = new ValidationError();
25
+
26
+ validationError.property = o.property;
27
+ validationError.children = [];
28
+ validationError.constraints = o.constraints;
29
+
30
+ return validationError;
31
+ });
32
+
33
+ return new ModelValidationError(validationErrors);
34
+ }
35
+
36
+ public renderMessage(): string {
37
+ return "Model validation error";
38
+ }
39
+
40
+ public code(): string {
41
+ return "model_validation_error";
42
+ }
43
+
44
+ public context(): RenderableContext {
45
+ return {
46
+ errors: this.errors.map((e) => ({
47
+ property: e.property,
48
+ errors: e.constraints,
49
+ children: e.children,
50
+ })),
51
+ };
52
+ }
53
+ }
@@ -0,0 +1,22 @@
1
+ import { registerDecorator, ValidationOptions, ValidationTypes } from "class-validator";
2
+
3
+ export function IsJustEditable(validationOptions?: ValidationOptions) {
4
+ // eslint-disable-next-line @typescript-eslint/ban-types
5
+ return function justEditableDecorator(object: object, propertyName: string): void {
6
+ registerDecorator({
7
+ name: ValidationTypes.CONDITIONAL_VALIDATION,
8
+ target: object.constructor,
9
+ propertyName,
10
+ constraints: [(_object: unknown, value: unknown) => value !== undefined],
11
+ options: validationOptions,
12
+ validator: {
13
+ validate(value: unknown): boolean {
14
+ return value !== null;
15
+ },
16
+ defaultMessage() {
17
+ return `${propertyName} should not be defined or different than null`;
18
+ },
19
+ },
20
+ });
21
+ };
22
+ }
@@ -0,0 +1 @@
1
+ export * from "./IsJustEditable";
@@ -0,0 +1,16 @@
1
+ import { ModelValidationError } from "./ModelValidationError";
2
+
3
+ export function entityOrValidationError<T>(
4
+ entity: T | undefined,
5
+ property: string
6
+ ): T {
7
+ if (!entity) {
8
+ throw ModelValidationError.from({
9
+ property,
10
+ constraints: {
11
+ mustBePresent: `${property} is not present`,
12
+ },
13
+ });
14
+ }
15
+ return entity;
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./BaseModel";
2
+ export * from "./ModelValidationError";
3
+ export * from "./entityOrValidationError";
4
+ export * from "./common-validators";
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "src",
4
+ "outDir": "dist",
5
+ "module": "commonjs",
6
+ "target": "es6",
7
+ "types": ["node"],
8
+ "esModuleInterop": true,
9
+ "sourceMap": true,
10
+ "experimentalDecorators": true,
11
+
12
+ "alwaysStrict": true,
13
+ "strictNullChecks": true
14
+ },
15
+ "exclude": ["node_modules"]
16
+ }