git ssb

0+

mixmix / ssb-gathering-schema



Commit e4eac9ccb82c20dcc8e3e9ea749f3557e168ec56

basic schemas for all types

mixmix committed on 9/7/2018, 4:12:34 AM
Parent: 246f45cd2e6b9fd110fa31cb5dc36e6bd7f9353b

Files changed

README.mdchanged
package-lock.jsonchanged
package.jsonchanged
attendee/schema.jsadded
attendee/validator.jsadded
attendee/validator.test.jsadded
gathering-update/schema.jsadded
gathering-update/validator.jsadded
gathering-update/validator.test.jsadded
gathering/schema.jsadded
gathering/validator.jsadded
lib/build-validator.jsadded
README.mdView
@@ -3,17 +3,60 @@
33 ## Usage
44
55
66 ```js
7-var { isGathering, isGatheringUpdate, isAttendance } = require('ssb-gathering-schema')
7 +var { isGathering, isGatheringUpdate, isAttendee } = require('ssb-gathering-schema')
88
99 isGathering(msg)
1010 // => true
1111 ```
1212
1313 ## Schemas
1414
15 +schemas describe the `content` section of messages
16 +
1517 ### gathering
1618
19 +```js
20 +{
21 + type: gathering
22 +}
23 +```
24 +
1725 ### gathering-update (type: `about`)
1826
19-### attendance (type: `about`)
27 +Updates to the details of a gathering are about messages with some / all of the following attributes.
28 +(everything but `type` and `about` are optional)
29 +
30 +```js
31 +{
32 + type: 'about'
33 + about: MessageId, // gathering
34 + title: String, // (optional)
35 + description: String, // (optional)
36 + location: String, // (optional)
37 + startDateTime: { // (optional)
38 + epoch: Integer, // unix time
39 + tz: TimeZoneString, // *
40 + bias: Integer // * ?
41 + silent: Boolean // * ?
42 + },
43 + image: { // (optional)
44 + link: BlobId,
45 + name: String,
46 + size: Integer,
47 + type: MimeType
48 + }
49 +}
50 +```
51 +
52 +### attendee (type: `about`)
53 +
54 +```js
55 +{
56 + type: 'about',
57 + about: MessageId, // gathering
58 + attendee: {
59 + link: FeedId // can be about another person!
60 + remove: true // (optional)
61 + }
62 +}
package-lock.jsonView
The diff is too large to show. Use a local git client to view these changes.
Old file size: 154513 bytes
New file size: 204977 bytes
package.jsonView
@@ -5,9 +5,9 @@
55 "main": "index.js",
66 "scripts": {
77 "lint": "standard --fix",
88 "test": "npm run test:js && npm run test:lint",
9- "test:js": "tape test/**/*.test.js | tap-diff",
9 + "test:js": "tape **/*.test.js | tap-diff",
1010 "test:lint": "standard"
1111 },
1212 "repository": {
1313 "type": "git",
@@ -32,9 +32,9 @@
3232 "ssb-schema-definitions": "^1.0.6"
3333 },
3434 "devDependencies": {
3535 "scuttle-testbot": "^1.1.6",
36 + "standard": "^12.0.1",
3637 "tap-diff": "^0.1.1",
37- "tape": "^4.9.1",
38- "tape-plus": "^1.0.0"
38 + "tape": "^4.9.1"
3939 }
4040 }
attendee/schema.jsView
@@ -1,0 +1,23 @@
1 +const definitions = require('ssb-schema-definitions')
2 +
3 +module.exports = {
4 + $schema: 'http://json-schema.org/schema#',
5 + type: 'object',
6 + required: ['type', 'about', 'attendee'],
7 + properties: {
8 + type: {
9 + type: 'string',
10 + pattern: '^about$'
11 + },
12 + about: { $ref: '#/definitions/messageId' },
13 + attendee: {
14 + type: 'object',
15 + required: 'link',
16 + properties: {
17 + link: { $ref: '#/definitions/feedId' },
18 + remove: { type: 'boolean' }
19 + }
20 + }
21 + },
22 + definitions: definitions
23 +}
attendee/validator.jsView
@@ -1,0 +1,4 @@
1 +var Validator = require('../lib/build-validator')
2 +var schema = require('./schema')
3 +
4 +module.exports = Validator(schema)
attendee/validator.test.jsView
@@ -1,0 +1,31 @@
1 +const test = require('tape')
2 +const valid = require('./validator')
3 +
4 +const Attendee = () => {
5 + return {
6 + type: 'about',
7 + about: '%WcE/QeRq1DQn5L+xP696fLq6qfIvRS4DBt4QXicas0A=.sha256',
8 + attendee: {
9 + link: '@ye+QM09iPcDJD6YvQYjoQc7sLF/IFhmNbEqgdzQo3lQ=.ed25519',
10 + remove: true
11 + }
12 + }
13 +}
14 +
15 +test('is-attendee', t => {
16 + t.true(valid(Attendee()), 'remove attendee')
17 +
18 + const withoutRemove = Attendee()
19 + delete withoutRemove.attendee.remove
20 + t.true(valid(withoutRemove), 'add attendee')
21 +
22 + const withoutAttendee = Attendee()
23 + delete withoutAttendee.attendee
24 + t.false(valid(withoutAttendee), 'must have attendee prop')
25 +
26 + const incorrectAttendee = Attendee()
27 + incorrectAttendee.attendee.link = 'mix'
28 + t.false(valid(incorrectAttendee), 'attendee.link must be a feedId')
29 +
30 + t.end()
31 +})
gathering-update/schema.jsView
@@ -1,0 +1,39 @@
1 +const definitions = require('ssb-schema-definitions')
2 +
3 +module.exports = {
4 + $schema: 'http://json-schema.org/schema#',
5 + type: 'object',
6 + required: ['type', 'about'],
7 + properties: {
8 + type: {
9 + type: 'string',
10 + pattern: '^about$'
11 + },
12 + about: { $ref: '#/definitions/messageId' },
13 + title: { type: 'string' },
14 + description: { type: 'string' },
15 + location: { type: 'string' },
16 + startDateTime: {
17 + type: 'object',
18 + required: ['epoch'],
19 + properties: {
20 + epoch: { type: 'integer' },
21 + tz: { type: 'string' }
22 + }
23 + },
24 + image: {
25 + type: 'object',
26 + required: ['link'],
27 + properties: {
28 + link: { $ref: '#/definitions/blobId' },
29 + name: { type: 'string' },
30 + size: { type: 'integer' },
31 + type: {
32 + type: 'string',
33 + pattern: '^image/\\w+$'
34 + }
35 + }
36 + }
37 + },
38 + definitions: definitions
39 +}
gathering-update/validator.jsView
@@ -1,0 +1,4 @@
1 +var Validator = require('../lib/build-validator')
2 +var schema = require('./schema')
3 +
4 +module.exports = Validator(schema)
gathering-update/validator.test.jsView
@@ -1,0 +1,66 @@
1 +const test = require('tape')
2 +const valid = require('./validator')
3 +
4 +const GatheringUpdate = () => {
5 + return {
6 + type: 'about',
7 + about: '%WcE/QeRq1DQn5L+xP696fLq6qfIvRS4DBt4QXicas0A=.sha256',
8 + title: 'ziva\'s birthday',
9 + description: 'come celebrate 1 year of life with ziva',
10 + location: 'our place in mirimar',
11 + startDateTime: {
12 + epoch: Date.now() + 5e6,
13 + silent: true,
14 + tz: 'Pacific/Auckland',
15 + valid: true
16 + },
17 + image: {
18 + link: '&GGEZJfKVD5NLd2l+YT8/mXbljNFM05D6iBxK+gen4+o=.sha256',
19 + name: 'simone.jpg',
20 + size: 47904,
21 + type: 'image/jpeg'
22 + }
23 + }
24 +}
25 +
26 +test('is-gathering-update', t => {
27 + t.true(valid(GatheringUpdate()))
28 + if (valid.errors) console.log(valid.errors)
29 +
30 + // misc keys
31 + const missingStrings = GatheringUpdate()
32 + delete missingStrings.title
33 + delete missingStrings.description
34 + delete missingStrings.location
35 + t.true(valid(missingStrings), 'missing title/ description/ location')
36 +
37 + // startDateTime
38 + const missingStart = GatheringUpdate()
39 + delete missingStart.startDateTime
40 + t.true(valid(missingStart), 'missing startDateTime')
41 +
42 + const bareStart = GatheringUpdate()
43 + bareStart.startDateTime = { epoch: Date.now() }
44 + t.true(valid(bareStart), 'minimal startDateTime')
45 +
46 + const brokenStart = GatheringUpdate()
47 + delete brokenStart.startDateTime.epoch
48 + t.false(valid(brokenStart), 'startDateTime missing epoch')
49 +
50 + // image
51 + const missingImage = GatheringUpdate()
52 + delete missingImage.image
53 + t.true(valid(missingImage), 'missing image')
54 +
55 + const minimalImage = GatheringUpdate()
56 + minimalImage.image = {
57 + link: '&GGEZJfKVD5NLd2l+YT8/mXbljNFM05D6iBxK+gen4+o=.sha256'
58 + }
59 + t.true(valid(minimalImage), 'minimal image')
60 +
61 + const brokenImage = GatheringUpdate()
62 + delete brokenImage.image.link
63 + t.false(valid(brokenImage), 'broken image')
64 +
65 + t.end()
66 +})
gathering/schema.jsView
@@ -1,0 +1,11 @@
1 +module.exports = {
2 + $schema: 'http://json-schema.org/schema#',
3 + type: 'object',
4 + required: ['type'],
5 + properties: {
6 + type: {
7 + type: 'string',
8 + pattern: '^gathering$'
9 + }
10 + }
11 +}
gathering/validator.jsView
@@ -1,0 +1,4 @@
1 +var Validator = require('../lib/build-validator')
2 +var schema = require('./schema')
3 +
4 +module.exports = Validator(schema)
lib/build-validator.jsView
@@ -1,0 +1,16 @@
1 +const Validator = require('is-my-json-valid')
2 +const getContent = require('ssb-msg-content')
3 +
4 +module.exports = function buildValidator (schema) {
5 + const validator = Validator(schema, { verbose: true })
6 +
7 + return function validatorWithErrors (obj, opts = {}) {
8 + const result = validator(getContent(obj))
9 +
10 + // exposes error messages provided by is-my-json-valid
11 + validatorWithErrors.errors = validator.errors
12 + if (opts.attachErrors) obj.errors = validator.errors
13 +
14 + return result
15 + }
16 +}

Built with git-ssb-web