Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | 5x 5x 5x 5x 5x 86x 86x 2x 2x 84x 84x 84x 84x 109x 109x 24x 109x 94x 94x 94x 94x 1x 1x 109x 84x 84x 8x 84x 14x 14x 2x 84x 84x 77x 84x 77x 84x 78x 84x 76x 76x 76x 84x 84x 14x 84x 13x 84x 14x 84x 14x 84x 14x 13x 13x 13x 84x 73x 102x 102x 106x 81x 49x 49x | import Ajv from 'ajv'
import ajvErrors from 'ajv-errors'
import md5 from 'md5'
import {logger} from './logger'
import stringify from 'fast-json-stable-stringify'
const SCHEMA = {
type: 'object',
properties: {
type: {
type: 'string',
pattern: '^([A-Z][a-zA-Z]*)+$',
maxLength: 50,
minLength: 1,
errorMessage: {
type:
'Entity type must be a string that starts with a capital letter like [C]ar or [U]ser',
maxLength:
'Entity type must be a non-empty string of 50 characters or less',
minLength:
'Entity type must be a non-empty string of 50 characters or less'
}
},
isGroup: {
type: 'boolean',
errorMessage: {
type: '`isGroup` must be a boolean'
}
},
id: {
type: ['string', 'integer'],
pattern: '^[ -~]+$',
maxLength: 250,
minLength: 1,
errorMessage: {
type:
"An entity's `id` must be a string or integer. An integer `id` will be cast into a string.",
maxLength:
"An entity's `id` must be a non-empty string of 250 characters or less.",
minLength:
"An entity's `id` must be a non-empty string of 250 characters or less."
}
},
displayName: {
type: 'string',
maxLength: 250,
minLength: 1,
errorMessage: {
type: "An entity's `displayName` must be a string",
maxLength:
"An entity's `displayName` must be a non-empty string of 250 characters or less.",
minLength:
"An entity's `displayName` must be a non-empty string of 250 characters or less."
}
},
attributes: {
type: 'object',
patternProperties: {
'^[a-zA-Z]$|^[a-zA-Z_]{0,48}[a-zA-Z]$': {
oneOf: [
{
type: 'string',
maxLength: 3000,
errorMessage: {
type:
"An entity's attribute value must be a string, boolean, or number.",
maxLength:
"An entity's string attribute must be 3000 characters or less."
}
},
{
type: 'boolean',
errorMessage: {
type:
"An entity's attribute value must be a string, boolean, or number."
}
},
{
type: 'number',
errorMessage: {
type:
"An entity's attribute value must be a string, boolean, or number."
}
}
],
errorMessage: {
oneOf:
"An entity's attribute value must be a string, boolean, or number."
}
}
},
maxProperties: 100,
additionalProperties: false,
errorMessage: {
maxProperties: 'There can only be up to 100 attributes for an entity.',
additionalProperties:
'Each attribute must begin and end with an alphabet letter (a-z, A-Z). In between, allowed characters are a-z, A-Z, and "_". For example: isStudent or is_student. Preceding or trailing underscore is not allowed (i.e., _is_student or is_student_). Each attribute value must be a number, string or boolean.'
}
},
group: {
type: ['object', 'null'],
properties: {
type: {
type: 'string',
pattern: '^([A-Z][a-zA-Z]*)+$',
maxLength: 50,
minLength: 1,
errorMessage: {
type:
'Group type must be a string that starts with a capital letter like [C]arGroup or [U]serGroup. Camel casing is used to separate words.',
maxLength:
'Group type must be a non-empty string of 50 characters or less',
minLength:
'Group type must be a non-empty string of 50 characters or less'
}
},
isGroup: {
type: 'boolean',
enum: [true],
errorMessage: {
type: '`isGroup` must be a boolean',
enum: '`isGroup` must always be true for an group'
}
},
id: {
type: ['string', 'integer'],
pattern: '^[ -~]+$',
maxLength: 250,
minLength: 1,
errorMessage: {
type:
"A group's `id` must be a string or integer. An integer `id` will be cast into a string.",
maxLength:
"A group's `id` must be a non-empty string of 250 characters or less.",
minLength:
"A group's `id` must be a non-empty string of 250 characters or less."
}
},
displayName: {
type: 'string',
maxLength: 250,
minLength: 1,
errorMessage: {
type: "A group's `displayName` must be a string",
maxLength:
"A group's `displayName` must be a non-empty string of 250 characters or less.",
minLength:
"A group's `displayName` must be a non-empty string of 250 characters or less."
}
},
attributes: {
type: 'object',
patternProperties: {
'^[a-zA-Z]$|^[a-zA-Z_]{0,48}[a-zA-Z]$': {
oneOf: [
{
type: 'string',
maxLength: 3000,
errorMessage: {
type:
"A group's attribute value must be a string, boolean, or number.",
maxLength:
"A group's string attribute must be 3000 characters or less."
}
},
{
type: 'boolean',
errorMessage: {
type:
"A group's attribute value must be a string, boolean, or number."
}
},
{
type: 'number',
errorMessage: {
type:
"A group's attribute value must be a string, boolean, or number."
}
}
],
errorMessage: {
oneOf:
"A group's attribute value must be a string, boolean, or number."
}
}
},
maxProperties: 100,
additionalProperties: false,
errorMessage: {
additionalProperties:
'Each attribute must begin and end with an alphabet letter (a-z, A-Z). In between, allowed characters are a-z, A-Z, and "_". For example: isStudent or is_student. Preceding or trailing underscore is not allowed (i.e., _is_student or is_student_). Each attribute value must be a number, string or boolean.'
}
}
},
required: ['id'],
additionalProperties: false,
errorMessage: {
required: 'Each group must have an `id` field.',
additionalProperties:
'Allowed properties for a group are: `type`, `id`, `displayName`, `attributes`, and `isGroup`. `id` is required, rest are optional.'
}
}
},
required: ['id'],
additionalProperties: false,
errorMessage: {
type: 'An entity is represented using a dictionary',
required: 'Each entity must have an `id` field.',
additionalProperties:
'Allowed properties for an entity are: `type`, `id`, `displayName`, `attributes`, `group`, and `isGroup`. `id` is required, rest are optional.'
}
}
const ajv = Ajv({allErrors: true, jsonPointers: true})
ajvErrors(ajv)
const validate = ajv.compile(SCHEMA)
export const DEFAULT_ENTITY_TYPE = 'User'
export default class AirshipObject {
constructor(obj) {
let isValid = AirshipObject.isValidObject(obj)
if (!isValid) {
this.object = null
return
}
obj = AirshipObject._cloneObject(obj)
isValid = AirshipObject._fillInFields(obj)
Iif (!isValid) {
this.object = null
return
}
this.object = obj
}
static isValidObject(obj) {
let isValid = validate(obj)
if (!isValid) {
logger(validate.errors.map(e => e.message))
}
if (isValid) {
const isGroup = obj.isGroup !== undefined ? obj.isGroup : false
const type = obj.type !== undefined ? obj.type : DEFAULT_ENTITY_TYPE
const groupIndex = type.lastIndexOf('Group')
if (
groupIndex !== -1 &&
groupIndex === type.length - 'Group'.length &&
!isGroup
) {
logger(
"An entity's type that ends with `Group` must be a group entity and therefore has to have an explicit `isGroup: true` property"
)
isValid = false
}
}
return isValid
}
static _cloneObject(obj) {
const clone = Object.assign({}, obj)
if (obj.attributes !== undefined) {
clone.attributes = Object.assign({}, obj.attributes)
}
if (obj.group !== undefined) {
clone.group = Object.assign({}, obj.group)
if (obj.group.attributes !== undefined) {
clone.group.attributes = Object.assign({}, obj.group.attributes)
}
}
return clone
}
static _fillInFields(obj) {
if (obj.type === undefined) {
obj.type = DEFAULT_ENTITY_TYPE
}
if (obj.displayName === undefined) {
obj.displayName = '' + obj.id
}
if (obj.isGroup === undefined) {
obj.isGroup = false
}
if (Number.isInteger(obj.id)) {
const idStr = '' + obj.id
Iif (idStr.length > 250) {
logger('Integer id must have 250 digits or less')
return false
}
obj.id = idStr
}
let group = null
if (obj.group !== undefined) {
group = obj.group
}
if (group !== null && group.displayName === undefined) {
group.displayName = '' + group.id
}
if (group !== null && group.type === undefined) {
group.type = obj.type + 'Group'
}
if (group !== null) {
group.isGroup = true
}
if (group !== null) {
if (Number.isInteger(group.id)) {
const idStr = '' + group.id
Iif (idStr.length > 250) {
logger('Integer id must have 250 digits or less')
return false
}
group.id = idStr
}
}
return true
}
getHash() {
return md5(
stringify({
...this.object,
attributes: this.object.attributes || {},
group: {
...this.object.group,
attributes: (this.object.group && this.object.group.attributes) || {}
}
})
)
}
getId() {
const obj = this.object
return `${obj.type}_${obj.id}`
}
isValid() {
return this.object !== null
}
getRawObject() {
return this.object
}
getObject() {
const obj = this.object
const clone = Object.assign({}, obj)
delete clone.group
return new AirshipObject(clone)
}
getGroup() {
const group = this.object.group || null
return group && new AirshipObject(group)
}
}
|