|
| 1 | +import { ajv } from "@classmodel/class/validate"; |
| 2 | +import { type DefinedError, type JSONSchemaType, ValidationError } from "ajv"; |
| 3 | + |
| 4 | +export interface BaseAnalysis { |
| 5 | + id: string; |
| 6 | + description: string; |
| 7 | + type: string; |
| 8 | + name: string; |
| 9 | +} |
| 10 | + |
| 11 | +export type TimeseriesAnalysis = BaseAnalysis & { |
| 12 | + xVariable: string; |
| 13 | + yVariable: string; |
| 14 | +}; |
| 15 | + |
| 16 | +export type ProfilesAnalysis = BaseAnalysis & { |
| 17 | + variable: string; |
| 18 | + time: number; |
| 19 | +}; |
| 20 | + |
| 21 | +export type SkewTAnalysis = BaseAnalysis & { |
| 22 | + time: number; |
| 23 | +}; |
| 24 | + |
| 25 | +export type Analysis = TimeseriesAnalysis | ProfilesAnalysis | SkewTAnalysis; |
| 26 | +export const analysisNames = [ |
| 27 | + "Vertical profiles", |
| 28 | + "Timeseries", |
| 29 | + "Thermodynamic diagram", |
| 30 | +]; |
| 31 | + |
| 32 | +export function parseAnalysis(raw: unknown): Analysis { |
| 33 | + const schema = { |
| 34 | + oneOf: [ |
| 35 | + { |
| 36 | + type: "object", |
| 37 | + required: [ |
| 38 | + "id", |
| 39 | + "description", |
| 40 | + "type", |
| 41 | + "name", |
| 42 | + "xVariable", |
| 43 | + "yVariable", |
| 44 | + ], |
| 45 | + properties: { |
| 46 | + id: { type: "string" }, |
| 47 | + description: { type: "string" }, |
| 48 | + type: { const: "timeseries" }, |
| 49 | + name: { type: "string" }, |
| 50 | + xVariable: { type: "string" }, |
| 51 | + yVariable: { type: "string" }, |
| 52 | + }, |
| 53 | + additionalProperties: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + type: "object", |
| 57 | + required: ["id", "description", "type", "name", "variable", "time"], |
| 58 | + properties: { |
| 59 | + id: { type: "string" }, |
| 60 | + description: { type: "string" }, |
| 61 | + type: { const: "profiles" }, |
| 62 | + name: { type: "string" }, |
| 63 | + variable: { type: "string" }, |
| 64 | + time: { type: "number" }, |
| 65 | + }, |
| 66 | + additionalProperties: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + type: "object", |
| 70 | + required: ["id", "description", "type", "name", "time"], |
| 71 | + properties: { |
| 72 | + id: { type: "string" }, |
| 73 | + description: { type: "string" }, |
| 74 | + type: { const: "skewT" }, |
| 75 | + name: { type: "string" }, |
| 76 | + time: { type: "number" }, |
| 77 | + }, |
| 78 | + additionalProperties: false, |
| 79 | + }, |
| 80 | + ], |
| 81 | + } as unknown as JSONSchemaType<Analysis>; |
| 82 | + const validate = ajv.compile(schema); |
| 83 | + if (!validate(raw)) { |
| 84 | + throw new ValidationError(validate.errors as DefinedError[]); |
| 85 | + } |
| 86 | + return raw; |
| 87 | +} |
0 commit comments