Add selection validator and fix outline and vector exports

This commit is contained in:
Mitchell McCaffrey 2021-07-23 15:40:01 +10:00
parent add88abb95
commit 1b248c16a5
3 changed files with 102 additions and 2 deletions

View File

@ -77,4 +77,4 @@ export const OutlineSchema = {
},
};
export const isColor = ajv.compile<Outline>(OutlineSchema);
export const isOutline = ajv.compile<Outline>(OutlineSchema);

100
src/validators/Selection.ts Normal file
View File

@ -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<Selection>(SelectionSchema);

View File

@ -17,4 +17,4 @@ export const Vector2Schema: JSONSchemaType<Vector2> = {
type: "object",
};
export const isColor = ajv.compile<Vector2>(Vector2Schema);
export const isVector2 = ajv.compile<Vector2>(Vector2Schema);