schema-shield 0.0.2

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.
@@ -0,0 +1,14 @@
1
+ import { ArrayKeywords } from "./keywords/array-keywords";
2
+ import { NumberKeywords } from "./keywords/number-keywords";
3
+ import { ObjectKeywords } from "./keywords/object-keywords";
4
+ import { OtherKeywords } from "./keywords/other-keywords";
5
+ import { StringKeywords } from "./keywords/string-keywords";
6
+ import { ValidatorFunction } from "./index";
7
+
8
+ export const keywords: Record<string, ValidatorFunction> = {
9
+ ...ObjectKeywords,
10
+ ...ArrayKeywords,
11
+ ...StringKeywords,
12
+ ...NumberKeywords,
13
+ ...OtherKeywords
14
+ };
package/lib/types.ts ADDED
@@ -0,0 +1,176 @@
1
+ import { ValidationError } from "./utils";
2
+ import { ValidatorFunction } from "./index";
3
+
4
+ export const Types: Record<string, ValidatorFunction> = {
5
+ object(schema, data, pointer) {
6
+ if (typeof data === "object" && data !== null && !Array.isArray(data)) {
7
+ return {
8
+ valid: true,
9
+ errors: [],
10
+ data
11
+ };
12
+ }
13
+
14
+ return {
15
+ valid: false,
16
+ errors: [
17
+ new ValidationError("Data is not an object", {
18
+ pointer,
19
+ value: data,
20
+ code: "NOT_AN_OBJECT"
21
+ })
22
+ ],
23
+ data
24
+ };
25
+ },
26
+ array(schema, data, pointer) {
27
+ if (Array.isArray(data)) {
28
+ return {
29
+ valid: true,
30
+ errors: [],
31
+ data
32
+ };
33
+ }
34
+
35
+ if (typeof data === "object" && data !== null && "length" in data) {
36
+ // Check if the first key is a number and the length is the same as the number of keys - 1 (length)
37
+ const keys = Object.keys(data);
38
+ if (keys.length > 0 && (keys[0] !== "0" || keys.length !== data.length)) {
39
+ return {
40
+ valid: false,
41
+ errors: [
42
+ new ValidationError("Data is not an array", {
43
+ pointer,
44
+ value: data,
45
+ code: "NOT_AN_ARRAY"
46
+ })
47
+ ],
48
+ data
49
+ };
50
+ }
51
+
52
+ return {
53
+ valid: true,
54
+ errors: [],
55
+ data
56
+ };
57
+ }
58
+
59
+ return {
60
+ valid: false,
61
+ errors: [
62
+ new ValidationError("Data is not an array", {
63
+ pointer,
64
+ value: data,
65
+ code: "NOT_AN_ARRAY"
66
+ })
67
+ ],
68
+ data
69
+ };
70
+ },
71
+ string(schema, data, pointer) {
72
+ if (typeof data === "string") {
73
+ return {
74
+ valid: true,
75
+ errors: [],
76
+ data
77
+ };
78
+ }
79
+
80
+ return {
81
+ valid: false,
82
+ errors: [
83
+ new ValidationError("Data is not a string", {
84
+ pointer,
85
+ value: data,
86
+ code: "NOT_A_STRING"
87
+ })
88
+ ],
89
+ data
90
+ };
91
+ },
92
+ number(schema, data, pointer) {
93
+ if (typeof data === "number") {
94
+ return {
95
+ valid: true,
96
+ errors: [],
97
+ data
98
+ };
99
+ }
100
+
101
+ return {
102
+ valid: false,
103
+ errors: [
104
+ new ValidationError("Data is not a number", {
105
+ pointer,
106
+ value: data,
107
+ code: "NOT_A_NUMBER"
108
+ })
109
+ ],
110
+ data
111
+ };
112
+ },
113
+ integer(schema, data, pointer) {
114
+ if (typeof data === "number" && Number.isInteger(data)) {
115
+ return {
116
+ valid: true,
117
+ errors: [],
118
+ data
119
+ };
120
+ }
121
+
122
+ return {
123
+ valid: false,
124
+ errors: [
125
+ new ValidationError("Data is not an integer", {
126
+ pointer,
127
+ value: data,
128
+ code: "NOT_AN_INTEGER"
129
+ })
130
+ ],
131
+ data
132
+ };
133
+ },
134
+ boolean(schema, data, pointer) {
135
+ if (typeof data === "boolean") {
136
+ return {
137
+ valid: true,
138
+ errors: [],
139
+ data
140
+ };
141
+ }
142
+
143
+ return {
144
+ valid: false,
145
+ errors: [
146
+ new ValidationError("Data is not a boolean", {
147
+ pointer,
148
+ value: data,
149
+ code: "NOT_A_BOOLEAN"
150
+ })
151
+ ],
152
+ data
153
+ };
154
+ },
155
+ null(schema, data, pointer) {
156
+ if (data === null) {
157
+ return {
158
+ valid: true,
159
+ errors: [],
160
+ data
161
+ };
162
+ }
163
+
164
+ return {
165
+ valid: false,
166
+ errors: [
167
+ new ValidationError("Data is not null", {
168
+ pointer,
169
+ value: data,
170
+ code: "NOT_NULL"
171
+ })
172
+ ],
173
+ data
174
+ };
175
+ }
176
+ };
package/lib/utils.ts ADDED
@@ -0,0 +1,79 @@
1
+ import { SchemaShield, ValidationErrorProps } from "./index";
2
+
3
+ export class ValidationError extends Error {
4
+ name: string;
5
+ pointer: string;
6
+ message: string;
7
+ value: any;
8
+ code: string;
9
+
10
+ constructor(
11
+ message: string,
12
+ options: ValidationErrorProps = {
13
+ pointer: "",
14
+ value: null,
15
+ code: ""
16
+ }
17
+ ) {
18
+ super(message);
19
+ this.name = "ValidationError";
20
+ this.pointer = options.pointer;
21
+ this.message = message;
22
+ this.value = options.value;
23
+ this.code = options.code;
24
+ }
25
+ }
26
+
27
+ export const defaultValidator = (schema, data, pointer) => {
28
+ return [
29
+ new ValidationError("No validator for this schema", {
30
+ pointer,
31
+ value: data,
32
+ code: "NO_VALIDATOR"
33
+ })
34
+ ];
35
+ };
36
+
37
+ export function deepEqual(
38
+ obj: Array<any> | Record<string, any>,
39
+ other: Array<any> | Record<string, any>
40
+ ) {
41
+ if (Array.isArray(obj) && Array.isArray(other)) {
42
+ if (obj.length !== other.length) {
43
+ return false;
44
+ }
45
+
46
+ for (let i = 0; i < obj.length; i++) {
47
+ if (!deepEqual(obj[i], other[i])) {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ return true;
53
+ }
54
+
55
+ if (typeof obj === "object" && typeof other === "object") {
56
+ if (obj === null || other === null) {
57
+ return obj === other;
58
+ }
59
+
60
+ const keys = Object.keys(obj);
61
+ if (keys.length !== Object.keys(other).length) {
62
+ return false;
63
+ }
64
+
65
+ for (const key of keys) {
66
+ if (!deepEqual(obj[key], other[key])) {
67
+ return false;
68
+ }
69
+ }
70
+
71
+ return true;
72
+ }
73
+
74
+ return obj === other;
75
+ }
76
+
77
+ export function isObject(data) {
78
+ return typeof data === "object" && data !== null && !Array.isArray(data);
79
+ }
package/package.json ADDED
@@ -0,0 +1,189 @@
1
+ {
2
+ "name": "schema-shield",
3
+ "version": "0.0.2",
4
+ "description": "A fast library that protects your JSON schema from invalid data.",
5
+ "repository": "git@github.com:Masquerade-Circus/schema-shield.git",
6
+ "author": "Masquerade <christian@masquerade-circus.net>",
7
+ "funding": "https://github.com/sponsors/Masquerade-Circus",
8
+ "license": "Apache-2.0",
9
+ "private": false,
10
+ "sideEffects": false,
11
+ "source": "lib/index.ts",
12
+ "main": "dist/index.js",
13
+ "module": "dist/index.mjs",
14
+ "unpkg": "dist/index.min.js",
15
+ "browser": "dist/index.min.js",
16
+ "types": "dist/**/*.d.ts",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.mjs",
21
+ "require": "./dist/index.js"
22
+ },
23
+ "./*": {
24
+ "types": "./dist/*.d.ts",
25
+ "import": "./dist/*/index.mjs",
26
+ "require": "./dist/*/index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist",
31
+ "lib",
32
+ "package-lock.json",
33
+ "LICENSE",
34
+ "tsconfig.json"
35
+ ],
36
+ "keywords": [
37
+ "Schema Shield",
38
+ "JSON",
39
+ "JSON Schema",
40
+ "Validation",
41
+ "Validator",
42
+ "Validate",
43
+ "Data",
44
+ "Object",
45
+ "Node.js",
46
+ "Web",
47
+ "TypeScript",
48
+ "Schema",
49
+ "Shield",
50
+ "Protection",
51
+ "Constraints",
52
+ "Compliance",
53
+ "Integrity",
54
+ "Accuracy",
55
+ "Efficiency",
56
+ "Performance",
57
+ "Lightweight",
58
+ "Extensible",
59
+ "Reliable",
60
+ "Error free",
61
+ "Fast",
62
+ "Robust",
63
+ "Precise",
64
+ "Secure",
65
+ "Easy to use",
66
+ "User friendly"
67
+ ],
68
+ "engines": {
69
+ "node": ">=12.0.0"
70
+ },
71
+ "scripts": {
72
+ "test": "mocha --bail --recursive --no-timeouts --exit --require ts-node/register --enable-source-maps tests/**/*.test.ts",
73
+ "dev:test": "nodemon -e ts,js -w ./tests -w ./lib --exec npm run test",
74
+ "dev:source": "NODE_ENV=development nodemon --enable-source-maps -e tsx,ts,json,css -w ./lib -w ./www source.js",
75
+ "build": "node source.js",
76
+ "coverage": "nyc report --reporter=lcov",
77
+ "commit": "git add . && git-cz",
78
+ "release": "release-it --verbose",
79
+ "release-test": "release-it --dry-run --verbose"
80
+ },
81
+ "dependencies": {
82
+ "ts-node": "^10.9.1",
83
+ "tsc-prog": "^2.2.1",
84
+ "tslib": "^2.5.0",
85
+ "typescript": "^5.0.2"
86
+ },
87
+ "devDependencies": {
88
+ "@release-it/conventional-changelog": "^5.1.1",
89
+ "@typescript-eslint/eslint-plugin": "^5.56.0",
90
+ "@typescript-eslint/parser": "^5.56.0",
91
+ "cz-conventional-changelog": "^3.3.0",
92
+ "esbuild": "^0.17.12",
93
+ "eslint": "^8.36.0",
94
+ "eslint-plugin-sonarjs": "^0.19.0",
95
+ "expect": "^29.5.0",
96
+ "json-schema-test-suite": "github:json-schema-org/JSON-Schema-Test-Suite",
97
+ "mocha": "^10.2.0",
98
+ "nodemon": "^2.0.22",
99
+ "nyc": "^15.1.0",
100
+ "release-it": "^15.9.3",
101
+ "terser": "^5.16.6"
102
+ },
103
+ "nyc": {
104
+ "exclude": [
105
+ "test",
106
+ "register.js"
107
+ ]
108
+ },
109
+ "config": {
110
+ "commitizen": {
111
+ "path": "./node_modules/cz-conventional-changelog"
112
+ }
113
+ },
114
+ "release-it": {
115
+ "plugins": {
116
+ "@release-it/conventional-changelog": {
117
+ "infile": "CHANGELOG.md",
118
+ "preset": {
119
+ "name": "conventionalcommits",
120
+ "types": [
121
+ {
122
+ "type": "feat",
123
+ "section": "Features"
124
+ },
125
+ {
126
+ "type": "feature",
127
+ "section": "Features"
128
+ },
129
+ {
130
+ "type": "fix",
131
+ "section": "Bug Fixes"
132
+ },
133
+ {
134
+ "type": "perf",
135
+ "section": "Performance Improvements"
136
+ },
137
+ {
138
+ "type": "revert",
139
+ "section": "Reverts"
140
+ },
141
+ {
142
+ "type": "docs",
143
+ "section": "Documentation"
144
+ },
145
+ {
146
+ "type": "style",
147
+ "section": "Styles"
148
+ },
149
+ {
150
+ "type": "chore",
151
+ "section": "Miscellaneous Chores"
152
+ },
153
+ {
154
+ "type": "refactor",
155
+ "section": "Code Refactoring"
156
+ },
157
+ {
158
+ "type": "test",
159
+ "section": "Tests"
160
+ },
161
+ {
162
+ "type": "build",
163
+ "section": "Build System"
164
+ },
165
+ {
166
+ "type": "ci",
167
+ "section": "Continuous Integration"
168
+ }
169
+ ]
170
+ }
171
+ }
172
+ },
173
+ "git": {
174
+ "requireCleanWorkingDir": false
175
+ },
176
+ "github": {
177
+ "release": true
178
+ },
179
+ "npm": {
180
+ "publish": true
181
+ },
182
+ "hooks": {
183
+ "before:init": [
184
+ "npm run build",
185
+ "npm run test"
186
+ ]
187
+ }
188
+ }
189
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "jsx": "preserve",
4
+ "target": "ES2021",
5
+ "moduleResolution": "nodenext",
6
+ "resolveJsonModule": true,
7
+ "allowSyntheticDefaultImports": true,
8
+ "allowJs": true,
9
+ "esModuleInterop": true
10
+ },
11
+ "include": ["lib/**/*"],
12
+ "exclude": ["**/*.spec.ts", "node_modules/**/*", "docs/**/*"],
13
+ "ts-node": {
14
+ "transpileOnly": true
15
+ }
16
+ }