backend validation wip

This commit is contained in:
newtbot
2024-01-02 02:53:44 +08:00
parent a058b1ba70
commit 6342d5a4cb
13 changed files with 410 additions and 211 deletions

View File

@ -1,62 +1,125 @@
"use strict";
const { Sequelize, DataTypes } = require("sequelize");
const { sequelize } = require("../mySQL");
const {
isValidDateString,
} = require("../../Web-Server/functions/validateData");
//sequelize.sync();
const api_log_Model = sequelize.define("api-logs",{
const api_log_Model = sequelize.define(
"api-logs",
{
// Model attributes are defined here
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
validator: {
isNumeric: true,
},
},
ip:{
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
time: {
type: DataTypes.STRING,
allowNull: false,
length: 20,
},
method: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
host: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
statusCode: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
Responsesize: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
},
referrer: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
},
userAgent: {
type: DataTypes.STRING,
allowNull: false,
length: 100,
},
},
ip: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
validator: {
isIP: true,
},
},
time: {
type: DataTypes.STRING,
allowNull: false,
length: 20,
validator: {
//validate time: new Date().toUTCString(),
isValidDateString(value) {
if (!isValidDateString(value)) {
throw new Error("Time must be a valid date string");
}
},
notEmpty: { msg: "Time cannot be empty" },
len: [1, 20],
},
},
method: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validator: {
isIn: [["GET", "POST", "PUT", "DELETE"]],
},
},
host: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
validator: {
//http://localhost/api/test
//remember to add domain name to the list
isIn: [["localhost"]],
//isFqdn: true,
},
},
statusCode: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validator: {
isNumeric: true,
len: [1, 3],
},
},
Responsesize: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validator: {
isNumeric: true,
len: [1, 100],
},
},
referrer: {
type: DataTypes.STRING,
allowNull: false,
length: 45,
validator: {
isString(value) {
if (typeof value !== "string") {
throw new Error("Referrer must be a string");
}
},
notEmpty: { msg: "Referrer cannot be empty" },
len: [1, 45], // Length between 1 and 45 characters
},
},
userAgent: {
type: DataTypes.STRING,
allowNull: false,
length: 100,
validator: {
isString(value) {
if (typeof value !== "string") {
throw new Error("UserAgent must be a string");
}
},
notEmpty: { msg: "UserAgent cannot be empty" },
len: [1, 100], // Length between 1 and 100 characters
},
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
},
},
{
timestamps: false,
timestamps: true,
}
);
module.exports = { api_log_Model };

View File

@ -11,21 +11,60 @@ const locationModel = sequelize.define(
allowNull: true,
primaryKey: true,
autoIncrement: true,
validator: {
isNumeric: true,
},
},
name: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validator: {
notEmpty: { msg: "Name cannot be empty" },
len: [1, 20],
/*
//will not validate this and fail it
"hello world" (contains a space)
"hello@123" (contains a symbol)
"" (empty string)
*/
is: ["^[a-z0-9]+$", "i"]
},
},
added_by: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
validator: {
notEmpty: { msg: "Added by cannot be empty" },
len: [1, 20],
/*
//will not validate this and fail it
"hello world" (contains a space)
"hello@123" (contains a symbol)
"" (empty string)
*/
is: ["^[a-z0-9]+$", "i"]
},
},
description: {
type: DataTypes.STRING,
allowNull: true,
length: 100,
validator: {
notEmpty: { msg: "Description cannot be empty" },
len: [1, 100],
/*
//will not validate this and fail it
"hello world" (contains a space)
"hello@123" (contains a symbol)
"" (empty string)
*/
is: ["^[a-z0-9]+$", "i"]
},
},
createdAt: {
type: DataTypes.DATE,

View File

@ -5,38 +5,39 @@ const { locationModel } = require("./locationModel");
const { sensorModel } = require("./sensorModel");
//sequelize.sync();
const sensorDataModel = sequelize.define("sensorData",
const sensorDataModel = sequelize.define(
"sensorData",
{
id: {
type: DataTypes.INTEGER,
allowNull: true,
primaryKey: true,
autoIncrement: true,
autoIncrement: true,
},
id_sensor: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: sensorModel,
key: "id",
},
},
id_location: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: locationModel,
key: "id",
},
},
sensordata: {
type: DataTypes.JSON,
allowNull: false,
},
id_sensor: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: sensorModel,
key: 'id'
}
},
id_location: {
type: DataTypes.INTEGER,
allowNull: false,
length: 100,
//FK
references: {
model: locationModel,
key: 'id'
}
},
Sensordata: {
type: DataTypes.JSON,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,

View File

@ -12,7 +12,7 @@ const sensorModel = sequelize.define("sensors",
primaryKey: true,
autoIncrement: true,
},
sensortype: {
sensorname: {
type: DataTypes.STRING,
allowNull: false,
length: 10,
@ -22,6 +22,11 @@ const sensorModel = sequelize.define("sensors",
allowNull: false,
length: 10,
},
mac_address: {
type: DataTypes.STRING,
allowNull: false,
length: 12,
},
description: {
type: DataTypes.STRING,
allowNull: true,