From 0374d7bccb79282e1079787d37aef4933a6325e9 Mon Sep 17 00:00:00 2001 From: "Gu://em_" Date: Thu, 24 Apr 2025 18:39:35 +0200 Subject: [PATCH] Added a sample implementation for validation schemas --- package.json | 1 + pnpm-lock.yaml | 34 ++++++++++++++++++++++++++++++++++ src/database/index.js | 2 +- src/schemas/auth.js | 21 +++++++++++++++++++++ src/schemas/mod.js | 21 +++++++++++++++++++++ src/schemas/modpack.js | 21 +++++++++++++++++++++ src/schemas/user.js | 23 +++++++++++++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/schemas/auth.js create mode 100644 src/schemas/mod.js create mode 100644 src/schemas/modpack.js create mode 100644 src/schemas/user.js diff --git a/package.json b/package.json index b761ae0..1facd76 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "license": "ISC", "packageManager": "pnpm@10.5.2", "dependencies": { + "ajv": "^8.17.1", "bcrypt": "^5.1.1", "better-sqlite3": "^11.9.1", "dompurify": "^3.2.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1859315..d1d6f0f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + ajv: + specifier: ^8.17.1 + version: 8.17.1 bcrypt: specifier: ^5.1.1 version: 5.1.1 @@ -92,6 +95,9 @@ packages: resolution: {integrity: sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==} engines: {node: '>= 14'} + ajv@8.17.1: + resolution: {integrity: sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==} + ansi-regex@5.0.1: resolution: {integrity: sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==} engines: {node: '>=8'} @@ -296,6 +302,12 @@ packages: resolution: {integrity: sha512-DT9ck5YIRU+8GYzzU5kT3eHGA5iL+1Zd0EutOmTE9Dtk+Tvuzd23VBU+ec7HPNSTxXYO55gPV/hq4pSBJDjFpA==} engines: {node: '>= 18'} + fast-deep-equal@3.1.3: + resolution: {integrity: sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==} + + fast-uri@3.0.6: + resolution: {integrity: sha512-Atfo14OibSv5wAp4VWNsFYE1AchQRTv9cBGWET4pZWHzYshFSS9NQI6I57rdKn9croWVMbYFbLhJ+yJvmZIIHw==} + file-uri-to-path@1.0.0: resolution: {integrity: sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==} @@ -458,6 +470,9 @@ packages: canvas: optional: true + json-schema-traverse@1.0.0: + resolution: {integrity: sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==} + jsonwebtoken@9.0.2: resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==} engines: {node: '>=12', npm: '>=6'} @@ -685,6 +700,10 @@ packages: resolution: {integrity: sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==} engines: {node: '>=8.10.0'} + require-from-string@2.0.2: + resolution: {integrity: sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==} + engines: {node: '>=0.10.0'} + rimraf@3.0.2: resolution: {integrity: sha512-JZkJMZkAGFFPP2YqXZXPbMlMBgsxzE8ILs4lMIX/2o0L9UBw9O/Y3o6wFw/i9YLapcUJWwqbi3kdxIPdC62TIA==} deprecated: Rimraf versions prior to v4 are no longer supported @@ -974,6 +993,13 @@ snapshots: agent-base@7.1.3: {} + ajv@8.17.1: + dependencies: + fast-deep-equal: 3.1.3 + fast-uri: 3.0.6 + json-schema-traverse: 1.0.0 + require-from-string: 2.0.2 + ansi-regex@5.0.1: {} anymatch@3.1.3: @@ -1197,6 +1223,10 @@ snapshots: transitivePeerDependencies: - supports-color + fast-deep-equal@3.1.3: {} + + fast-uri@3.0.6: {} + file-uri-to-path@1.0.0: {} fill-range@7.1.1: @@ -1387,6 +1417,8 @@ snapshots: - supports-color - utf-8-validate + json-schema-traverse@1.0.0: {} + jsonwebtoken@9.0.2: dependencies: jws: 3.2.2 @@ -1614,6 +1646,8 @@ snapshots: dependencies: picomatch: 2.3.1 + require-from-string@2.0.2: {} + rimraf@3.0.2: dependencies: glob: 7.2.3 diff --git a/src/database/index.js b/src/database/index.js index 116b623..fcc776a 100644 --- a/src/database/index.js +++ b/src/database/index.js @@ -1,6 +1,6 @@ -const { getConfig } = require("../utils/configManager"); const MySQLDatabase = require("./mysql"); const SQLiteDatabase = require("./sqlite"); +const { getConfig } = require("../utils/configManager"); let db; diff --git a/src/schemas/auth.js b/src/schemas/auth.js new file mode 100644 index 0000000..e57a75c --- /dev/null +++ b/src/schemas/auth.js @@ -0,0 +1,21 @@ +const Ajv = require("ajv"); +const ajv = new Ajv(); + +// --- Schemas --- + +const AuthUserSchema = { + type: 'object', + properties: { + email: { type: 'string', format: 'email' }, + password: { type: 'string', minLength: 3, maxLength: 30 }, + }, + required: ['email', 'password'], + additionalProperties: false +}; + +const validateAuthUserData = ajv.compile(AuthUserSchema); + + +// --- Exports --- + +module.exports = { validateAuthUserData, validateAuthNodeData }; \ No newline at end of file diff --git a/src/schemas/mod.js b/src/schemas/mod.js new file mode 100644 index 0000000..6d7dc9d --- /dev/null +++ b/src/schemas/mod.js @@ -0,0 +1,21 @@ +const Ajv = require("ajv"); +const ajv = new Ajv(); + +// --- Schemas --- +//TODO + +const newModSchema = { + type: 'object', + properties: { + name: { type: 'string'}, + }, + required: ['name'], + additionalProperties: false +}; + +const validateNewModData = ajv.compile(newModSchema); + + +// --- Exports --- + +module.exports = { validateNewModData }; \ No newline at end of file diff --git a/src/schemas/modpack.js b/src/schemas/modpack.js new file mode 100644 index 0000000..df4a49a --- /dev/null +++ b/src/schemas/modpack.js @@ -0,0 +1,21 @@ +const Ajv = require("ajv"); +const ajv = new Ajv(); + +// --- Schemas --- +//TODO + +const newModpackSchema = { + type: 'object', + properties: { + name: { type: 'string'}, + }, + required: ['name'], + additionalProperties: false +}; + +const validateNewModpackData = ajv.compile(newModpackSchema); + + +// --- Exports --- + +module.exports = { validateNewModpackData }; \ No newline at end of file diff --git a/src/schemas/user.js b/src/schemas/user.js new file mode 100644 index 0000000..0c009a1 --- /dev/null +++ b/src/schemas/user.js @@ -0,0 +1,23 @@ +const Ajv = require("ajv"); +const ajv = new Ajv(); + +// --- Schemas --- +//TODO + +const newUserSchema = { + type: 'object', + properties: { + email: { type: 'string', format: 'email' }, + name: { type: 'string' }, + password: { type: 'string', minLength: 3, maxLength: 30 }, + }, + required: ['name', 'email', 'password'], + additionalProperties: false +}; + +const validateNewUserData = ajv.compile(newUserSchema); + + +// --- Exports --- + +module.exports = { validateNewUserData }; \ No newline at end of file