Better context check
This commit is contained in:
parent
1b684e2f2b
commit
c9b3c9bc41
|
@ -4,6 +4,8 @@ const Endpoints = require('./endpoint');
|
||||||
// deps
|
// deps
|
||||||
const { isString, isObject } = require('lodash');
|
const { isString, isObject } = require('lodash');
|
||||||
|
|
||||||
|
const Context = '@context';
|
||||||
|
|
||||||
module.exports = class ActivityPubObject {
|
module.exports = class ActivityPubObject {
|
||||||
constructor(obj, withContext = [ActivityStreamsContext]) {
|
constructor(obj, withContext = [ActivityStreamsContext]) {
|
||||||
if (withContext) {
|
if (withContext) {
|
||||||
|
@ -39,16 +41,31 @@ module.exports = class ActivityPubObject {
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid() {
|
isValid() {
|
||||||
const nonEmpty = s => isString(s) && s.length > 1;
|
//
|
||||||
// :TODO: Additional validation
|
// If @context is present, it must be valid;
|
||||||
if (
|
// child objects generally inherit, so they may not have one
|
||||||
(this['@context'] === ActivityStreamsContext ||
|
//
|
||||||
this['@context'][0] === ActivityStreamsContext) &&
|
if (this[Context]) {
|
||||||
nonEmpty(this.id) &&
|
if (!this.isContextValid()) {
|
||||||
nonEmpty(this.type)
|
return false;
|
||||||
) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const checkString = s => isString(s) && s.length > 1;
|
||||||
|
return checkString(this.id) && checkString(this.type);
|
||||||
|
}
|
||||||
|
|
||||||
|
isContextValid() {
|
||||||
|
if (Array.isArray(this[Context])) {
|
||||||
|
if (this[Context][0] === ActivityStreamsContext) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
} else if (isString(this[Context])) {
|
||||||
|
if (ActivityStreamsContext === this[Context]) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue