-
-
Save joepie91/5dcc85ba9374c6890c3794ea1b9e0051 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const required = require("@validatem/required"); | |
const either = require("@validatem/either"); | |
const anyProperty = require("@validatem/any-property"); | |
const arrayOf = require("@validatem/array-of"); | |
const isString = require("@validatem/is-string"); | |
const isInteger = require("@validatem/is-integer"); | |
const isBoolean = require("@validatem/is-boolean"); | |
const isPlainObject = require("@validatem/is-plain-object"); | |
const defaultTo = require("@validatem/default-to"); | |
const anything = require("@validatem/anything"); | |
let isRoomID = isString; // FIXME | |
let isMatrixID = isString; // FIXME | |
let isEventID = isString; // FIXME | |
function optionalObject(rules) { | |
return [ defaultTo({}), rules ]; | |
} | |
function optionalArray(rules) { | |
return [ defaultTo([]), rules ]; | |
} | |
let isEvent = { | |
type: [ required, isString ], | |
content: [ required, isPlainObject ] | |
}; | |
// NOTE: Unspecced, see https://github.com/matrix-org/matrix-doc/issues/2680 - this would normally just be `isEvent` as per the spec | |
let isPresenceEvent = { | |
... isEvent, | |
sender: [ required, isMatrixID ] | |
}; | |
let isTimelineEvent = { | |
... isEvent, | |
event_id: [ required, isString ], | |
sender: [ required, isMatrixID ], | |
origin_server_ts: [ required, isInteger ], | |
// room_id: [ required, isRoomID ], // Not present on /sync, but will need to be checked for event validation elsewhere | |
unsigned: optionalObject({ | |
age: isInteger, | |
redacted_because: isEventID, | |
transaction_id: isString, | |
// Spec omission: https://github.com/matrix-org/matrix-doc/issues/1167 | |
replaces_state: isEventID, | |
// Spec omission and/or Synapse bug: https://github.com/matrix-org/matrix-doc/issues/877 | |
prev_content: isPlainObject, | |
// Spec omission: https://github.com/matrix-org/matrix-doc/issues/684 | |
prev_sender: isMatrixID | |
}) | |
}; | |
let isStateEvent = { | |
... isTimelineEvent, | |
state_key: [ required, isString ], | |
prev_content: isPlainObject | |
}; | |
let isStrippedEvent = { | |
... isEvent, | |
state_key: [ required, isString ], | |
sender: [ required, isMatrixID ] | |
}; | |
let isDeviceEvent = { | |
... isEvent, | |
sender: [ required, isMatrixID ] | |
}; | |
let isStateList = arrayOf([ required, isStateEvent ]); | |
let isEventList = arrayOf([ required, isEvent ]); | |
let isPresenceEventList = arrayOf([ required, isPresenceEvent ]); | |
let isStrippedEventList = arrayOf([ required, isStrippedEvent ]); | |
let isDeviceEventList = arrayOf([ required, isDeviceEvent ]); | |
// The below permits state events as well, as these can be duplicated from the `state` section. See: https://github.com/matrix-org/synapse/issues/1597 | |
let isTimelineList = arrayOf([ required, either([ isTimelineEvent, isStateEvent ]) ]); | |
module.exports = { | |
next_batch: [ required, isString ], | |
rooms: { | |
join: optionalObject(anyProperty({ | |
key: [ required, isRoomID ], | |
value: { | |
summary: optionalObject({ | |
"m.heroes": arrayOf([ required, isString ]), | |
"m.joined_member_count": isInteger, | |
"m.invited_member_count": isInteger, | |
}), | |
state: optionalObject({ | |
events: optionalArray(isStateList) | |
}), | |
timeline: optionalObject({ | |
events: optionalArray(isTimelineList), | |
limited: isBoolean, | |
prev_batch: isString | |
}), | |
ephemeral: optionalObject({ | |
events: optionalArray(isEventList) | |
}), | |
account_data: optionalObject({ | |
events: optionalArray(isEventList) | |
}), | |
unread_notifications: { | |
highlight_count: isInteger, | |
notification_count: isInteger | |
} | |
} | |
})), | |
invite: optionalObject(anyProperty({ | |
key: [ required, isRoomID ], | |
value: { | |
invite_state: optionalObject({ | |
events: optionalArray(isStrippedEventList) | |
}) | |
} | |
})), | |
leave: optionalObject(anyProperty({ | |
key: [ required, isRoomID ], | |
value: { | |
state: optionalObject({ | |
events: optionalArray(isStateList) | |
}), | |
timeline: optionalObject({ | |
events: optionalArray(isTimelineList), | |
limited: isBoolean, | |
prev_batch: isString | |
}), | |
account_data: optionalObject({ | |
events: optionalArray(isEventList) | |
}), | |
} | |
})) | |
}, | |
presence: optionalObject({ | |
events: optionalArray(isPresenceEventList) | |
}), | |
account_data: optionalObject({ | |
events: optionalArray(isEventList) | |
}), | |
to_device: optionalObject({ | |
events: optionalArray(isDeviceEventList) | |
}), | |
device_lists: optionalObject({ | |
changed: arrayOf([ required, isMatrixID ]), | |
left: arrayOf([ required, isMatrixID ]) | |
}), | |
device_one_time_keys_count: optionalObject(anyProperty({ | |
key: [ required, isString ], // algorithm name | |
value: [ required, isInteger ] // key count | |
})), | |
groups: anything // NOTE: Non-standard | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment