Begin implementing Joi validation on the database models

This commit is contained in:
Oliver-Akins 2025-05-20 23:43:20 -06:00
parent 64029b9508
commit 76fe473cd1
4 changed files with 116 additions and 41 deletions

View file

@ -1,43 +1,60 @@
const { fields } = foundry.data;
import * as Joi from "joi";
// MARK: Buckets
const numberBucketSchema = Joi.object({
type: Joi.string().valid(`number`, `range`).required(),
min: Joi
.number()
.integer()
.when(`type`, {
is: Joi.string().valid(`range`),
then: Joi.required(),
otherwise: Joi.optional(),
}),
max: Joi
.number()
.integer()
.when(`type`, {
is: Joi.string().valid(`range`),
then: Joi.required(),
otherwise: Joi.optional(),
}),
step: Joi
.number()
.integer()
.when(`type`, {
is: Joi.string().valid(`range`),
then: Joi.required(),
otherwise: Joi.optional(),
}),
});
const stringBucketSchema = Joi.object({
type: Joi.string().valid(`string`).required(),
choices: Joi.array(Joi.string()).optional(),
});
// MARK: Graphs
const barGraphSchema = Joi.object({
type: Joi.string().valid(`bar`).required(),
stacked: Joi.boolean().required(),
});
// MARK: Table
export class Table extends foundry.abstract.DataModel {
static defineSchema() {
return {
name: new fields.StringField({
nullable: false,
required: true,
blank: false,
trim: true,
validate(value) {
return !value.match(/[^a-z0-9_\-:]/i);
},
}),
data: new fields.TypedObjectField(Row),
};
};
};
export const tableSchema = Joi.object({
name: Joi
.string()
.trim()
.required()
.pattern(/^[a-z \-_]+(\/[a-z \-_]+)?$/i),
buckets: Joi.alternatives([
numberBucketSchema,
stringBucketSchema,
]).match(`one`),
graph: Joi.alternatives([
barGraphSchema,
]).match(`one`),
});
// MARK: Row
export class Row extends foundry.abstract.DataModel {
static defineSchema() {
return {
id: new fields.StringField({
nullable: false,
required: true,
blank: false,
}),
timestamp: new fields.NumberField({
min: 0,
required: true,
nullable: false,
}),
value: new fields.AnyField(),
private: new fields.BooleanField({
initial: false,
required: true,
nullable: false,
}),
};
};
};
export const rowSchema = Joi.object({});