aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/xtext/xtextMessages.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-11-16 03:00:45 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-11-16 03:11:00 +0100
commit5810a7eb3b19ef9868db170c9214686bfc613eee (patch)
treeda122997d4ad58f4104d0f84b06a00fe14e7ad02 /language-web/src/main/js/xtext/xtextMessages.ts
parentfeat(lang): basic formatting (diff)
downloadrefinery-5810a7eb3b19ef9868db170c9214686bfc613eee.tar.gz
refinery-5810a7eb3b19ef9868db170c9214686bfc613eee.tar.zst
refinery-5810a7eb3b19ef9868db170c9214686bfc613eee.zip
chore(web): json validation with zod
Use the zod library instead of manually written type assertions for validating json messages from the server. This makes it easier to add and handle new messages.
Diffstat (limited to 'language-web/src/main/js/xtext/xtextMessages.ts')
-rw-r--r--language-web/src/main/js/xtext/xtextMessages.ts78
1 files changed, 28 insertions, 50 deletions
diff --git a/language-web/src/main/js/xtext/xtextMessages.ts b/language-web/src/main/js/xtext/xtextMessages.ts
index 68737958..c4305fcf 100644
--- a/language-web/src/main/js/xtext/xtextMessages.ts
+++ b/language-web/src/main/js/xtext/xtextMessages.ts
@@ -1,62 +1,40 @@
1export interface IXtextWebRequest { 1import { z } from 'zod';
2 id: string;
3 2
4 request: unknown; 3export const xtextWebRequest = z.object({
5} 4 id: z.string().nonempty(),
5 request: z.unknown(),
6});
6 7
7export interface IXtextWebOkResponse { 8export type XtextWebRequest = z.infer<typeof xtextWebRequest>;
8 id: string;
9 9
10 response: unknown; 10export const xtextWebOkResponse = z.object({
11} 11 id: z.string().nonempty(),
12 response: z.unknown(),
13});
12 14
13export function isOkResponse(response: unknown): response is IXtextWebOkResponse { 15export type XtextWebOkResponse = z.infer<typeof xtextWebOkResponse>;
14 const okResponse = response as IXtextWebOkResponse;
15 return typeof okResponse === 'object'
16 && typeof okResponse.id === 'string'
17 && typeof okResponse.response !== 'undefined';
18}
19 16
20export const VALID_XTEXT_WEB_ERROR_KINDS = ['request', 'server'] as const; 17export const xtextWebErrorKind = z.enum(['request', 'server']);
21 18
22export type XtextWebErrorKind = typeof VALID_XTEXT_WEB_ERROR_KINDS[number]; 19export type XtextWebErrorKind = z.infer<typeof xtextWebErrorKind>;
23 20
24export function isXtextWebErrorKind(value: unknown): value is XtextWebErrorKind { 21export const xtextWebErrorResponse = z.object({
25 return typeof value === 'string' 22 id: z.string().nonempty(),
26 && VALID_XTEXT_WEB_ERROR_KINDS.includes(value as XtextWebErrorKind); 23 error: xtextWebErrorKind,
27} 24 message: z.string(),
25});
28 26
29export interface IXtextWebErrorResponse { 27export type XtextWebErrorResponse = z.infer<typeof xtextWebErrorResponse>;
30 id: string;
31 28
32 error: XtextWebErrorKind; 29export const xtextWebPushService = z.enum(['highlight', 'validate']);
33 30
34 message: string; 31export type XtextWebPushService = z.infer<typeof xtextWebPushService>;
35}
36 32
37export function isErrorResponse(response: unknown): response is IXtextWebErrorResponse { 33export const xtextWebPushMessage = z.object({
38 const errorResponse = response as IXtextWebErrorResponse; 34 resource: z.string().nonempty(),
39 return typeof errorResponse === 'object' 35 stateId: z.string().nonempty(),
40 && typeof errorResponse.id === 'string' 36 service: xtextWebPushService,
41 && isXtextWebErrorKind(errorResponse.error) 37 push: z.unknown(),
42 && typeof errorResponse.message === 'string'; 38});
43}
44 39
45export interface IXtextWebPushMessage { 40export type XtextWebPushMessage = z.infer<typeof xtextWebPushMessage>;
46 resource: string;
47
48 stateId: string;
49
50 service: string;
51
52 push: unknown;
53}
54
55export function isPushMessage(response: unknown): response is IXtextWebPushMessage {
56 const pushMessage = response as IXtextWebPushMessage;
57 return typeof pushMessage === 'object'
58 && typeof pushMessage.resource === 'string'
59 && typeof pushMessage.stateId === 'string'
60 && typeof pushMessage.service === 'string'
61 && typeof pushMessage.push !== 'undefined';
62}