From 1b248c16a56a3868b602ee53709cafb9865ce9d8 Mon Sep 17 00:00:00 2001 From: Mitchell McCaffrey Date: Fri, 23 Jul 2021 15:40:01 +1000 Subject: [PATCH] Add selection validator and fix outline and vector exports --- src/validators/Outline.ts | 2 +- src/validators/Selection.ts | 100 ++++++++++++++++++++++++++++++++++++ src/validators/Vector2.ts | 2 +- 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/validators/Selection.ts diff --git a/src/validators/Outline.ts b/src/validators/Outline.ts index e137337..8f7559c 100644 --- a/src/validators/Outline.ts +++ b/src/validators/Outline.ts @@ -77,4 +77,4 @@ export const OutlineSchema = { }, }; -export const isColor = ajv.compile(OutlineSchema); +export const isOutline = ajv.compile(OutlineSchema); diff --git a/src/validators/Selection.ts b/src/validators/Selection.ts new file mode 100644 index 0000000..f40537e --- /dev/null +++ b/src/validators/Selection.ts @@ -0,0 +1,100 @@ +import Ajv from "ajv"; +import { Selection } from "../types/Select"; + +import { DrawingSchema } from "./Drawing"; +import { Vector2Schema } from "./Vector2"; +import { ColorSchema } from "./Color"; + +export const SelectionSchema = { + $id: "https://www.owlbear.rodeo/schemas/selection.json", + anyOf: [ + { + $ref: "#/definitions/RectSelection", + }, + { + $ref: "#/definitions/PathSelection", + }, + ], + definitions: { + SelectionItemType: { + enum: ["token", "note"], + type: "string", + }, + SelectionItem: { + properties: { + type: { + $ref: "#/definitions/SelectionItemType", + }, + id: { + type: "string", + }, + }, + required: ["type", "id"], + type: "object", + }, + BaseSelection: { + properties: { + items: { + items: { + $ref: "#/definitions/SelectionItem", + }, + type: "array", + }, + x: { + type: "number", + }, + y: { + type: "number", + }, + }, + required: ["items", "x", "y"], + type: "object", + }, + RectSelection: { + allOf: [ + { + $ref: "#/definitions/BaseSelection", + }, + { + properties: { + data: { + $ref: "drawing.json#/definitions/RectData", + }, + type: { + enum: ["rectangle"], + type: "string", + }, + }, + required: ["data", "type"], + type: "object", + }, + ], + }, + PathSelection: { + allOf: [ + { + $ref: "#/definitions/BaseSelection", + }, + { + properties: { + data: { + $ref: "drawing.json#/definitions/PointsData", + }, + type: { + enum: ["path"], + type: "string", + }, + }, + required: ["data", "type"], + type: "object", + }, + ], + }, + }, +}; + +const ajv = new Ajv({ + schemas: [SelectionSchema, DrawingSchema, Vector2Schema, ColorSchema], +}); + +export const isSelection = ajv.compile(SelectionSchema); diff --git a/src/validators/Vector2.ts b/src/validators/Vector2.ts index 40d01cb..8d92895 100644 --- a/src/validators/Vector2.ts +++ b/src/validators/Vector2.ts @@ -17,4 +17,4 @@ export const Vector2Schema: JSONSchemaType = { type: "object", }; -export const isColor = ajv.compile(Vector2Schema); +export const isVector2 = ajv.compile(Vector2Schema);