Skip to content

Commit f59ca8f

Browse files
committed
refactor: validate constraint through gateway
1 parent 8631348 commit f59ca8f

File tree

6 files changed

+38
-78
lines changed

6 files changed

+38
-78
lines changed

package-lock.json

-56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@types/toastify-js": "^1.9.2",
4141
"@typescript-eslint/eslint-plugin": "^5.3.1",
4242
"@typescript-eslint/parser": "^5.3.1",
43-
"ajv": "^8.7.1",
4443
"bootstrap-icons": "^1.7.0",
4544
"cloc": "^2.8.0",
4645
"cookie": "^0.4.1",

src/components/constraint/Editor.svelte

+9-20
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@
22

33
<script lang="ts">
44
import { createEventDispatcher, onMount } from 'svelte';
5-
import Ajv from 'ajv';
6-
import type { ValidateFunction } from 'ajv';
75
import Field from '../form/Field.svelte';
86
import Label from '../form/Label.svelte';
97
import Card from '../ui/Card.svelte';
108
import CodeMirrorJsonEditor from '../ui/CodeMirrorJsonEditor.svelte';
119
import AlertError from '../ui/AlertError.svelte';
1210
import Panel from '../ui/Panel.svelte';
1311
import type { Constraint, CreateConstraint } from '../../types';
12+
import { reqValidateConstraint } from '../../utilities/requests';
1413
1514
const dispatch = createEventDispatcher();
1615
1716
export let constraint: Constraint | null = null;
1817
export let modelId: number;
1918
export let planId: number;
2019
21-
let ajv: Ajv;
20+
let debounce: NodeJS.Timeout;
2221
let definition: string = '';
2322
let definitionError: string | null = null;
2423
let description: string = '';
2524
let name: string = '';
2625
let summary: string = '';
2726
let type: string = 'model';
28-
let validateSchema: ValidateFunction<any>;
2927
3028
$: valid = definition !== '' && !definitionError && name !== '';
3129
@@ -43,30 +41,21 @@
4341
summary = '';
4442
type = 'model';
4543
}
46-
await initAjv();
4744
});
4845
49-
async function initAjv() {
50-
ajv = new Ajv();
51-
ajv.addKeyword('$anchor');
52-
const response = await fetch('/schemas/constraint.json');
53-
const constraintSchema = await response.json();
54-
validateSchema = ajv.compile<any>(constraintSchema);
55-
}
56-
57-
function onTextChanged(event: CustomEvent<string>) {
46+
async function onTextChanged(event: CustomEvent<string>) {
5847
const { detail: json } = event;
5948
try {
60-
const parsedJson = JSON.parse(json);
6149
definition = json;
6250
definitionError = null;
63-
if (validateSchema) {
64-
const schemaValid = validateSchema(parsedJson);
65-
if (!schemaValid) {
66-
console.log(validateSchema.errors);
51+
52+
clearTimeout(debounce);
53+
debounce = setTimeout(async () => {
54+
const { valid } = await reqValidateConstraint(json);
55+
if (!valid) {
6756
definitionError = 'Input is not a valid constraint';
6857
}
69-
}
58+
}, 200);
7059
} catch (e) {
7160
console.log(e);
7261
definitionError = e.message;

src/utilities/requests.ts

+29
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,32 @@ export async function reqValidateActivityArguments(
12521252
return { errors: [message], success: false };
12531253
}
12541254
}
1255+
1256+
export async function reqValidateConstraint(body: string) {
1257+
let response: Response;
1258+
let json: any;
1259+
try {
1260+
const GATEWAY_URL = gatewayUrl();
1261+
const options = {
1262+
body,
1263+
headers: {
1264+
'Content-Type': 'application/json',
1265+
},
1266+
method: 'POST',
1267+
};
1268+
1269+
response = await fetch(`${GATEWAY_URL}/constraint/validate`, options);
1270+
json = await response.json();
1271+
if (!response.ok) throw new Error(response.statusText);
1272+
1273+
return json;
1274+
} catch (e) {
1275+
console.log(e);
1276+
console.log(response);
1277+
console.log(json);
1278+
return {
1279+
errors: ['An unexpected error occurred'],
1280+
valid: false,
1281+
};
1282+
}
1283+
}

static/.gitkeep

Whitespace-only changes.

static/schemas/constraint.json

-1
This file was deleted.

0 commit comments

Comments
 (0)