diff options
author | 2021-11-16 03:00:45 +0100 | |
---|---|---|
committer | 2021-11-16 03:11:00 +0100 | |
commit | 5810a7eb3b19ef9868db170c9214686bfc613eee (patch) | |
tree | da122997d4ad58f4104d0f84b06a00fe14e7ad02 /language-web/src/main/js/xtext/xtextMessages.ts | |
parent | feat(lang): basic formatting (diff) | |
download | refinery-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.ts | 78 |
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 @@ | |||
1 | export interface IXtextWebRequest { | 1 | import { z } from 'zod'; |
2 | id: string; | ||
3 | 2 | ||
4 | request: unknown; | 3 | export const xtextWebRequest = z.object({ |
5 | } | 4 | id: z.string().nonempty(), |
5 | request: z.unknown(), | ||
6 | }); | ||
6 | 7 | ||
7 | export interface IXtextWebOkResponse { | 8 | export type XtextWebRequest = z.infer<typeof xtextWebRequest>; |
8 | id: string; | ||
9 | 9 | ||
10 | response: unknown; | 10 | export const xtextWebOkResponse = z.object({ |
11 | } | 11 | id: z.string().nonempty(), |
12 | response: z.unknown(), | ||
13 | }); | ||
12 | 14 | ||
13 | export function isOkResponse(response: unknown): response is IXtextWebOkResponse { | 15 | export 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 | ||
20 | export const VALID_XTEXT_WEB_ERROR_KINDS = ['request', 'server'] as const; | 17 | export const xtextWebErrorKind = z.enum(['request', 'server']); |
21 | 18 | ||
22 | export type XtextWebErrorKind = typeof VALID_XTEXT_WEB_ERROR_KINDS[number]; | 19 | export type XtextWebErrorKind = z.infer<typeof xtextWebErrorKind>; |
23 | 20 | ||
24 | export function isXtextWebErrorKind(value: unknown): value is XtextWebErrorKind { | 21 | export 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 | ||
29 | export interface IXtextWebErrorResponse { | 27 | export type XtextWebErrorResponse = z.infer<typeof xtextWebErrorResponse>; |
30 | id: string; | ||
31 | 28 | ||
32 | error: XtextWebErrorKind; | 29 | export const xtextWebPushService = z.enum(['highlight', 'validate']); |
33 | 30 | ||
34 | message: string; | 31 | export type XtextWebPushService = z.infer<typeof xtextWebPushService>; |
35 | } | ||
36 | 32 | ||
37 | export function isErrorResponse(response: unknown): response is IXtextWebErrorResponse { | 33 | export 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 | ||
45 | export interface IXtextWebPushMessage { | 40 | export type XtextWebPushMessage = z.infer<typeof xtextWebPushMessage>; |
46 | resource: string; | ||
47 | |||
48 | stateId: string; | ||
49 | |||
50 | service: string; | ||
51 | |||
52 | push: unknown; | ||
53 | } | ||
54 | |||
55 | export 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 | } | ||