Add schema validators for map state

This commit is contained in:
Mitchell McCaffrey
2021-07-23 12:45:07 +10:00
parent ed64294855
commit e3a2cc766c
10 changed files with 744 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import Ajv, { JSONSchemaType } from "ajv";
import { Color } from "../helpers/colors";
const ajv = new Ajv();
export const ColorSchema: JSONSchemaType<Color> = {
$id: "https://www.owlbear.rodeo/schemas/color.json",
enum: [
"black",
"blue",
"darkGray",
"green",
"lightGray",
"orange",
"pink",
"primary",
"purple",
"red",
"teal",
"white",
"yellow",
],
type: "string",
};
export const isColor = ajv.compile<Color>(ColorSchema);
+225
View File
@@ -0,0 +1,225 @@
import Ajv from "ajv";
import { Drawing } from "../types/Drawing";
import { ColorSchema } from "./Color";
import { Vector2Schema } from "./Vector2";
export const DrawingSchema = {
$id: "https://www.owlbear.rodeo/schemas/drawing.json",
anyOf: [
{
$ref: "#/definitions/Shape",
},
{
$ref: "#/definitions/Path",
},
],
definitions: {
PointsData: {
properties: {
points: {
items: {
$ref: "vector2.json",
},
type: "array",
},
},
required: ["points"],
type: "object",
},
RectData: {
properties: {
height: {
type: "number",
},
width: {
type: "number",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: ["height", "width", "x", "y"],
type: "object",
},
CircleData: {
properties: {
radius: {
type: "number",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: ["radius", "x", "y"],
type: "object",
},
BaseDrawing: {
properties: {
blend: {
type: "boolean",
},
color: {
$ref: "color.json",
},
id: {
type: "string",
},
strokeWidth: {
type: "number",
},
},
required: ["blend", "color", "id", "strokeWidth"],
type: "object",
},
BaseShape: {
allOf: [
{
$ref: "#/definitions/BaseDrawing",
},
{
properties: {
type: {
enum: ["shape"],
type: "string",
},
},
required: ["type"],
type: "object",
},
],
},
Line: {
allOf: [
{
$ref: "#/definitions/BaseShape",
},
{
properties: {
data: {
$ref: "#/definitions/PointsData",
},
shapeType: {
enum: ["line"],
type: "string",
},
},
required: ["data", "shapeType"],
type: "object",
},
],
},
Rectangle: {
allOf: [
{
$ref: "#/definitions/BaseShape",
},
{
properties: {
data: {
$ref: "#/definitions/RectData",
},
shapeType: {
enum: ["rectangle"],
type: "string",
},
},
required: ["data", "shapeType"],
type: "object",
},
],
},
Circle: {
allOf: [
{
$ref: "#/definitions/BaseShape",
},
{
properties: {
data: {
$ref: "#/definitions/CircleData",
},
shapeType: {
enum: ["circle"],
type: "string",
},
},
required: ["data", "shapeType"],
type: "object",
},
],
},
Triangle: {
allOf: [
{
$ref: "#/definitions/BaseShape",
},
{
properties: {
data: {
$ref: "#/definitions/PointsData",
},
shapeType: {
enum: ["triangle"],
type: "string",
},
},
required: ["data", "shapeType"],
type: "object",
},
],
},
Shape: {
anyOf: [
{
$ref: "#/definitions/Line",
},
{
$ref: "#/definitions/Rectangle",
},
{
$ref: "#/definitions/Circle",
},
{
$ref: "#/definitions/Triangle",
},
],
},
Path: {
allOf: [
{
$ref: "#/definitions/BaseDrawing",
},
{
properties: {
data: {
$ref: "#/definitions/PointsData",
},
pathType: {
enum: ["fill", "stroke"],
type: "string",
},
type: {
enum: ["path"],
type: "string",
},
},
required: ["data", "pathType", "type"],
type: "object",
},
],
},
},
};
export const ajv = new Ajv({
schemas: [DrawingSchema, ColorSchema, Vector2Schema],
});
export const isDrawing = ajv.compile<Drawing>(DrawingSchema);
+59
View File
@@ -0,0 +1,59 @@
import Ajv from "ajv";
import { Fog } from "../types/Fog";
import { Vector2Schema } from "./Vector2";
import { ColorSchema } from "./Color";
export const FogSchema = {
$id: "https://www.owlbear.rodeo/schemas/fog.json",
properties: {
color: {
$ref: "color.json",
},
data: {
$ref: "#/definitions/FogData",
},
id: {
type: "string",
},
strokeWidth: {
type: "number",
},
type: {
enum: ["fog"],
type: "string",
},
visible: {
type: "boolean",
},
},
required: ["color", "data", "id", "strokeWidth", "type", "visible"],
type: "object",
definitions: {
FogData: {
properties: {
holes: {
items: {
items: {
$ref: "vector2.json",
},
type: "array",
},
type: "array",
},
points: {
items: {
$ref: "vector2.json",
},
type: "array",
},
},
required: ["holes", "points"],
type: "object",
},
},
};
const ajv = new Ajv({ schemas: [FogSchema, ColorSchema, Vector2Schema] });
export const isFog = ajv.compile<Fog>(FogSchema);
+104
View File
@@ -0,0 +1,104 @@
import Ajv from "ajv";
import { MapState } from "../types/MapState";
import { DrawingSchema } from "./Drawing";
import { FogSchema } from "./Fog";
import { NoteSchema } from "./Note";
import { TokenStateSchema } from "./TokenState";
import { Vector2Schema } from "./Vector2";
import { ColorSchema } from "./Color";
import { OutlineSchema } from "./Outline";
export const MapStateSchema: any = {
$id: "https://www.owlbear.rodeo/schemas/map-state.json",
properties: {
tokens: {
$ref: "#/definitions/TokenStates",
},
drawShapes: {
$ref: "#/definitions/DrawingState",
},
fogShapes: {
$ref: "#/definitions/FogState",
},
editFlags: {
items: {
enum: ["drawing", "fog", "notes", "tokens"],
type: "string",
},
type: "array",
},
notes: {
$ref: "#/definitions/Notes",
},
mapId: {
type: "string",
},
},
required: [
"drawShapes",
"editFlags",
"fogShapes",
"mapId",
"notes",
"tokens",
],
type: "object",
definitions: {
TokenStates: {
propertyNames: {
type: "string",
},
additionalProperties: {
$ref: "token-state.json",
},
required: [],
type: "object",
},
DrawingState: {
propertyNames: {
type: "string",
},
additionalProperties: {
$ref: "drawing.json",
},
required: [],
type: "object",
},
FogState: {
propertyNames: {
type: "string",
},
additionalProperties: {
$ref: "fog.json",
},
required: [],
type: "object",
},
Notes: {
propertyNames: {
type: "string",
},
additionalProperties: {
$ref: "note.json",
},
required: [],
type: "object",
},
},
};
export const ajv = new Ajv({
schemas: [
MapStateSchema,
DrawingSchema,
FogSchema,
NoteSchema,
TokenStateSchema,
Vector2Schema,
ColorSchema,
OutlineSchema,
],
});
export const isMapState = ajv.compile<MapState>(MapStateSchema);
+62
View File
@@ -0,0 +1,62 @@
import Ajv, { JSONSchemaType } from "ajv";
import { Note } from "../types/Note";
import { Vector2Schema } from "./Vector2";
import { ColorSchema } from "./Color";
export const NoteSchema: JSONSchemaType<Note> = {
$id: "https://www.owlbear.rodeo/schemas/note.json",
properties: {
color: {
$ref: "color.json",
},
id: {
type: "string",
},
lastModified: {
type: "number",
},
lastModifiedBy: {
type: "string",
},
locked: {
type: "boolean",
},
size: {
type: "number",
},
text: {
type: "string",
},
textOnly: {
type: "boolean",
},
visible: {
type: "boolean",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: [
"color",
"id",
"lastModified",
"lastModifiedBy",
"locked",
"size",
"text",
"textOnly",
"visible",
"x",
"y",
],
type: "object",
};
const ajv = new Ajv({ schemas: [NoteSchema, ColorSchema, Vector2Schema] });
export const isNote = ajv.compile<Note>(NoteSchema);
+80
View File
@@ -0,0 +1,80 @@
import Ajv from "ajv";
import { Outline } from "../types/Outline";
const ajv = new Ajv();
export const OutlineSchema = {
$id: "https://www.owlbear.rodeo/schemas/outline.json",
anyOf: [
{
$ref: "#/definitions/CircleOutline",
},
{
$ref: "#/definitions/RectOutline",
},
{
$ref: "#/definitions/PathOutline",
},
],
definitions: {
CircleOutline: {
properties: {
radius: {
type: "number",
},
type: {
enum: ["circle"],
type: "string",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: ["radius", "type", "x", "y"],
type: "object",
},
RectOutline: {
properties: {
height: {
type: "number",
},
type: {
enum: ["rect"],
type: "string",
},
width: {
type: "number",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: ["height", "type", "width", "x", "y"],
type: "object",
},
PathOutline: {
properties: {
points: {
items: {
type: "number",
},
type: "array",
},
type: {
enum: ["path"],
type: "string",
},
},
required: ["points", "type"],
type: "object",
},
},
};
export const isColor = ajv.compile<Outline>(OutlineSchema);
+147
View File
@@ -0,0 +1,147 @@
import Ajv from "ajv";
import { TokenState } from "../types/TokenState";
import { ColorSchema } from "./Color";
import { OutlineSchema } from "./Outline";
export const TokenStateSchema = {
$id: "https://www.owlbear.rodeo/schemas/token-state.json",
anyOf: [
{
$ref: "#/definitions/DefaultTokenState",
},
{
$ref: "#/definitions/FileTokenState",
},
],
definitions: {
TokenCategory: {
enum: ["character", "prop", "vehicle"],
type: "string",
},
BaseTokenState: {
properties: {
category: {
$ref: "#/definitions/TokenCategory",
},
height: {
type: "number",
},
id: {
type: "string",
},
label: {
type: "string",
},
lastModified: {
type: "number",
},
lastModifiedBy: {
type: "string",
},
locked: {
type: "boolean",
},
outline: {
$ref: "outline.json",
},
owner: {
type: "string",
},
rotation: {
type: "number",
},
size: {
type: "number",
},
statuses: {
items: {
$ref: "color.json",
},
type: "array",
},
tokenId: {
type: "string",
},
visible: {
type: "boolean",
},
width: {
type: "number",
},
x: {
type: "number",
},
y: {
type: "number",
},
},
required: [
"category",
"height",
"id",
"label",
"lastModified",
"lastModifiedBy",
"locked",
"outline",
"owner",
"rotation",
"size",
"statuses",
"tokenId",
"visible",
"width",
"x",
"y",
],
type: "object",
},
DefaultTokenState: {
allOf: [
{
$ref: "#/definitions/BaseTokenState",
},
{
properties: {
key: {
type: "string",
},
type: {
enum: ["default"],
type: "string",
},
},
required: ["key", "type"],
type: "object",
},
],
},
FileTokenState: {
allOf: [
{
$ref: "#/definitions/BaseTokenState",
},
{
properties: {
file: {
type: "string",
},
type: {
enum: ["file"],
type: "string",
},
},
required: ["file", "type"],
type: "object",
},
],
},
},
};
export const ajv = new Ajv({
schemas: [TokenStateSchema, ColorSchema, OutlineSchema],
});
export const isTokenState = ajv.compile<TokenState>(TokenStateSchema);
+20
View File
@@ -0,0 +1,20 @@
import Ajv, { JSONSchemaType } from "ajv";
import Vector2 from "../helpers/Vector2";
const ajv = new Ajv();
export const Vector2Schema: JSONSchemaType<Vector2> = {
$id: "https://www.owlbear.rodeo/schemas/vector2.json",
properties: {
x: {
type: "number",
},
y: {
type: "number",
},
},
required: ["x", "y"],
type: "object",
};
export const isColor = ajv.compile<Vector2>(Vector2Schema);