This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
MatrixSyncResponseStrippedStateDTO.ts
101 lines (91 loc) · 3.84 KB
/
MatrixSyncResponseStrippedStateDTO.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2021. Sendanor <info@sendanor.fi>. All rights reserved.
import { MatrixSyncResponseUnsignedDataDTO, isMatrixSyncResponseUnsignedDataDTO } from "./MatrixSyncResponseUnsignedDataDTO";
import { isJsonObject, JsonObject } from "../../../../../core/Json";
import { isUndefined } from "../../../../../core/types/undefined";
import { isString, isStringOrUndefined } from "../../../../../core/types/String";
import { isNumberOrUndefined } from "../../../../../core/types/Number";
import { isRegularObject } from "../../../../../core/types/RegularObject";
import { hasNoOtherKeysInDevelopment } from "../../../../../core/types/OtherKeys";
import { keys } from "../../../../../core/functions/keys";
export interface MatrixSyncResponseStrippedStateDTO {
readonly content : JsonObject;
readonly state_key : string;
readonly type : string;
readonly sender : string;
readonly origin_server_ts ?: number;
readonly unsigned ?: MatrixSyncResponseUnsignedDataDTO;
readonly event_id ?: string;
}
export function isMatrixSyncResponseStrippedStateDTO (value: any): value is MatrixSyncResponseStrippedStateDTO {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, [
'content',
'state_key',
'type',
'sender',
'origin_server_ts',
'unsigned',
'event_id'
])
&& isJsonObject(value?.content)
&& isString(value?.state_key)
&& isString(value?.type)
&& isString(value?.sender)
&& isNumberOrUndefined(value?.origin_server_ts)
&& ( isUndefined(value?.unsigned) || isMatrixSyncResponseUnsignedDataDTO(value?.unsigned) )
&& isStringOrUndefined(value?.event_id)
);
}
export function assertMatrixSyncResponseStrippedStateDTO (value: any): void {
if(!( isRegularObject(value) )) {
throw new TypeError(`invalid: ${value}`);
}
if(!( hasNoOtherKeysInDevelopment(value, [
'content',
'state_key',
'type',
'sender',
'origin_server_ts',
'unsigned',
'event_id'
]) )) {
throw new TypeError(`one key is extra: ${keys(value)}`);
}
if(!( isJsonObject(value?.content) )) {
throw new TypeError(`Property "content" invalid: ${value?.content}`);
}
if(!( isString(value?.state_key) )) {
throw new TypeError(`Property "state_key" invalid: ${value?.state_key}`);
}
if(!( isString(value?.type) )) {
throw new TypeError(`Property "type" invalid: ${value?.type}`);
}
if(!( isString(value?.sender) )) {
throw new TypeError(`Property "sender" invalid: ${value?.sender}`);
}
if(!( isNumberOrUndefined(value?.origin_server_ts) )) {
throw new TypeError(`Property "origin_server_ts" invalid: ${value?.origin_server_ts}`);
}
if(!( ( isUndefined(value?.unsigned) || isMatrixSyncResponseUnsignedDataDTO(value?.unsigned) ) )) {
throw new TypeError(`Property "unsigned" invalid: ${value?.unsigned}`);
}
if(!( isStringOrUndefined(value?.event_id) )) {
throw new TypeError(`Property "event_id" invalid: ${value?.event_id}`);
}
}
export function explainMatrixSyncResponseStrippedStateDTO (value : any) : string {
try {
assertMatrixSyncResponseStrippedStateDTO(value);
return 'No errors detected';
} catch (err : any) {
return err?.message;
}
}
export function stringifyMatrixSyncResponseStrippedStateDTO (value: MatrixSyncResponseStrippedStateDTO): string {
return `MatrixSyncResponseStrippedStateDTO(${value})`;
}
export function parseMatrixSyncResponseStrippedStateDTO (value: any): MatrixSyncResponseStrippedStateDTO | undefined {
if ( isMatrixSyncResponseStrippedStateDTO(value) ) return value;
return undefined;
}