Commit e602c7f5b93b901ece225fec5f2fc495eeaed73d
Standard
Piet Geursen committed on 2/28/2018, 11:22:39 PMParent: e5241baa17774c8f27f39e3c093cf2b43a8aaa2f
Files changed
index.js | changed |
poll.js | changed |
schema/poll.js | changed |
schema/pollDetails/chooseOne.js | changed |
schema/pollDetails/dot.js | changed |
schema/pollDetails/proposal.js | changed |
schema/pollDetails/score.js | changed |
schema/position.js | changed |
schema/positionTypes/chooseOne.js | changed |
stance.js | changed |
sync/isPoll.js | changed |
test/schema/poll.js | changed |
test/schema/pollDetails/chooseOne.js | changed |
index.js | ||
---|---|---|
@@ -1,16 +1,20 @@ | ||
1 | -const { | |
1 … | +const { | |
2 | 2 … | create: poll, |
3 | 3 … | schema: pollSchema, |
4 | 4 … | validate: isPoll |
5 | 5 … | } = require('./poll') |
6 | 6 … | |
7 | -const { | |
7 … | +const { | |
8 | 8 … | create: stance, |
9 | 9 … | schema: stanceSchema, |
10 | 10 … | validate: isStance |
11 | 11 … | } = require('./stance') |
12 | 12 … | |
13 | 13 … | module.exports = { |
14 | - poll, pollSchema, isPoll, | |
15 | - stance, stanceSchema, isStance, | |
14 … | + poll, | |
15 … | + pollSchema, | |
16 … | + isPoll, | |
17 … | + stance, | |
18 … | + stanceSchema, | |
19 … | + isStance | |
16 | 20 … | } |
poll.js | ||
---|---|---|
@@ -1,46 +1,38 @@ | ||
1 | 1 … | var Validate = require('is-my-json-valid') |
2 | 2 … | var { schema, validate } = require('./schema/poll.js') |
3 | -var { schema : chooseOneSchema, create: chooseOneCreate, isValidTypeString: isValidChooseOneTypeString } = require('./schema/pollDetails/chooseOne.js') | |
3 … | +var { schema: chooseOneSchema, create: chooseOneCreate, isValidTypeString: isValidChooseOneTypeString } = require('./schema/pollDetails/chooseOne.js') | |
4 | 4 … | var { link } = require('ssb-msg-schemas/util') |
5 | 5 … | |
6 | - | |
7 | - | |
8 | -function create({text, mentions, recps, channel, pollOptions, root, branch}){ | |
6 … | +function create ({text, mentions, recps, channel, pollOptions, root, branch}) { | |
9 | 7 … | var content = { type: 'poll', text, pollOptions} |
10 | 8 … | if (root) { |
11 | 9 … | root = link(root) |
12 | - if (!root) | |
13 | - throw new Error('root is not a valid link') | |
10 … | + if (!root) { throw new Error('root is not a valid link') } | |
14 | 11 … | content.root = root |
15 | 12 … | } |
16 | 13 … | if (branch) { |
17 | - if (!root) | |
18 | - throw new Error('root is not a valid link') | |
14 … | + if (!root) { throw new Error('root is not a valid link') } | |
19 | 15 … | branch = Array.isArray(branch) ? branch.map(link) : link(branch) |
20 | - if (!branch) | |
21 | - throw new Error('branch is not a valid link') | |
16 … | + if (!branch) { throw new Error('branch is not a valid link') } | |
22 | 17 … | content.branch = branch |
23 | 18 … | } |
24 | 19 … | if (mentions && (!Array.isArray(mentions) || mentions.length)) { |
25 | 20 … | mentions = links(mentions) |
26 | - if (!mentions || !mentions.length) | |
27 | - throw new Error('mentions are not valid links') | |
21 … | + if (!mentions || !mentions.length) { throw new Error('mentions are not valid links') } | |
28 | 22 … | content.mentions = mentions |
29 | 23 … | } |
30 | 24 … | if (recps && (!Array.isArray(recps) || recps.length)) { |
31 | 25 … | recps = links(recps) |
32 | - if (!recps || !recps.length) | |
33 | - throw new Error('recps are not valid links') | |
26 … | + if (!recps || !recps.length) { throw new Error('recps are not valid links') } | |
34 | 27 … | content.recps = recps |
35 | 28 … | } |
36 | 29 … | if (channel) { |
37 | - if (typeof channel !== 'string') | |
38 | - throw new Error('channel must be a string') | |
30 … | + if (typeof channel !== 'string') { throw new Error('channel must be a string') } | |
39 | 31 … | content.channel = channel |
40 | 32 … | } |
41 | 33 … | |
42 | - if(isValidChooseOneTypeString(pollOptions.type)){ | |
34 … | + if (isValidChooseOneTypeString(pollOptions.type)) { | |
43 | 35 … | content.pollOptions = chooseOneCreate({choices: pollOptions.choices}) |
44 | 36 … | } |
45 | 37 … | |
46 | 38 … | return content |
schema/poll.js | ||
---|---|---|
@@ -23,12 +23,12 @@ | ||
23 | 23 … | oneOf: [ |
24 | 24 … | { $ref: '#/definitions/pollDetails/dot'}, |
25 | 25 … | { $ref: '#/definitions/pollDetails/proposal'}, |
26 | 26 … | { $ref: '#/definitions/pollDetails/score'}, |
27 | - { $ref: '#/definitions/pollDetails/chooseOne'}, | |
28 | - //{ $ref: '#/definitions/pollDetails/rsvp'}, | |
29 | - //{ $ref: '#/definitions/pollDetails/meeting'}, | |
30 | - ] | |
27 … | + { $ref: '#/definitions/pollDetails/chooseOne'} | |
28 … | + // { $ref: '#/definitions/pollDetails/rsvp'}, | |
29 … | + // { $ref: '#/definitions/pollDetails/meeting'}, | |
30 … | + ] | |
31 | 31 … | }, |
32 | 32 … | text: { type: 'string' }, |
33 | 33 … | mentions: { |
34 | 34 … | oneOf: [ |
@@ -52,9 +52,9 @@ | ||
52 | 52 … | type: 'array', |
53 | 53 … | items: { |
54 | 54 … | oneOf: [ |
55 | 55 … | { $ref: '#/definitions/feedId' }, |
56 | - { $ref: '#/definitions/mentions/feed' }, | |
56 … | + { $ref: '#/definitions/mentions/feed' } | |
57 | 57 … | ] |
58 | 58 … | } |
59 | 59 … | } |
60 | 60 … | ] |
@@ -105,9 +105,9 @@ | ||
105 | 105 … | name: { type: 'string' } |
106 | 106 … | } |
107 | 107 … | } |
108 | 108 … | } |
109 | - }, | |
109 … | + } | |
110 | 110 … | } |
111 | 111 … | |
112 | 112 … | const validate = Validate(schema, { verbose: true }) |
113 | 113 … |
schema/pollDetails/chooseOne.js | ||
---|---|---|
@@ -1,18 +1,18 @@ | ||
1 | 1 … | const Validate = require('is-my-json-valid') |
2 | 2 … | |
3 | 3 … | const typeString = 'chooseOne' |
4 | -const typeStringPattern =`^${typeString}$` | |
5 | -const typeStringRegex = new RegExp(typeStringPattern) | |
4 … | +const typeStringPattern = `^${typeString}$` | |
5 … | +const typeStringRegex = new RegExp(typeStringPattern) | |
6 | 6 … | |
7 | -function isValidTypeString(string) { | |
8 | - return typeStringRegex.test(string) | |
7 … | +function isValidTypeString (string) { | |
8 … | + return typeStringRegex.test(string) | |
9 | 9 … | } |
10 | 10 … | |
11 | -function create({choices}) { | |
11 … | +function create ({choices}) { | |
12 | 12 … | return { |
13 | 13 … | choices, |
14 | - type: typeString, | |
14 … | + type: typeString | |
15 | 15 … | } |
16 | 16 … | } |
17 | 17 … | |
18 | 18 … | var schema = { |
@@ -23,9 +23,9 @@ | ||
23 | 23 … | type: 'string', |
24 | 24 … | pattern: typeStringPattern |
25 | 25 … | }, |
26 | 26 … | choices: { |
27 | - type: 'array', | |
27 … | + type: 'array' | |
28 | 28 … | } |
29 | 29 … | } |
30 | 30 … | } |
31 | 31 … |
schema/pollDetails/dot.js | ||
---|---|---|
@@ -10,11 +10,10 @@ | ||
10 | 10 … | type: 'integer', |
11 | 11 … | minimum: 1 |
12 | 12 … | }, |
13 | 13 … | choices: { |
14 | - type: 'array', | |
14 … | + type: 'array' | |
15 | 15 … | } |
16 | 16 … | } |
17 | 17 … | } |
18 | 18 … | |
19 | - | |
20 | -module.exports = schema; | |
19 … | +module.exports = schema |
schema/pollDetails/proposal.js | ||
---|---|---|
@@ -6,10 +6,10 @@ | ||
6 | 6 … | type: 'string', |
7 | 7 … | pattern: '^proposal$' |
8 | 8 … | }, |
9 | 9 … | proposal: { |
10 | - type: 'string', | |
10 … | + type: 'string' | |
11 | 11 … | } |
12 | 12 … | } |
13 | 13 … | } |
14 | 14 … | |
15 | -module.exports = schema; | |
15 … | +module.exports = schema |
schema/pollDetails/score.js | ||
---|---|---|
@@ -10,10 +10,10 @@ | ||
10 | 10 … | type: 'integer', |
11 | 11 … | minimum: 2 |
12 | 12 … | }, |
13 | 13 … | choices: { |
14 | - type: 'array', | |
14 … | + type: 'array' | |
15 | 15 … | } |
16 | 16 … | } |
17 | 17 … | } |
18 | 18 … | |
19 | -module.exports = schema; | |
19 … | +module.exports = schema |
schema/position.js | |||
---|---|---|---|
@@ -1,19 +1,19 @@ | |||
1 | 1 … | const Validate = require('is-my-json-valid') | |
2 | 2 … | const { msgIdRegex, feedIdRegex, blobIdRegex } = require('ssb-ref') | |
3 | -//what would a message look like? | ||
3 … | +// what would a message look like? | ||
4 | 4 … | // | |
5 | -//well for a chooseOne: | ||
6 | -//{ | ||
5 … | +// well for a chooseOne: | ||
6 … | +// { | ||
7 | 7 … | // pollId: msgId, | |
8 | 8 … | // choice: 'apple', //what if this is a key? Or a key and index number? I wonder if this is an argument for making each choice a separate message? | |
9 | 9 … | // reason: "I don't like doctorbs", | |
10 | -//} | ||
11 | -//When do the various validations happen? | ||
12 | -//- when we're counting votes. We need to check it's a valid position for the type of poll | ||
13 | -//- when we try and create the vote? | ||
10 … | +// } | ||
11 … | +// When do the various validations happen? | ||
12 … | +// - when we're counting votes. We need to check it's a valid position for the type of poll | ||
13 … | +// - when we try and create the vote? | ||
14 | 14 … | // | |
15 | -//Can a schema always capture the types we need. Max stance score is 1 for a chooseOne. | ||
15 … | +// Can a schema always capture the types we need. Max stance score is 1 for a chooseOne. | ||
16 | 16 … | ||
17 | 17 … | const schema = { | |
18 | 18 … | $schema: 'http://json-schema.org/schema#', | |
19 | 19 … | type: 'object', | |
@@ -50,9 +50,9 @@ | |||
50 | 50 … | type: 'array', | |
51 | 51 … | items: { | |
52 | 52 … | oneOf: [ | |
53 | 53 … | { $ref: '#/definitions/feedId' }, | |
54 | - { $ref: '#/definitions/mentions/feed' }, | ||
54 … | + { $ref: '#/definitions/mentions/feed' } | ||
55 | 55 … | ] | |
56 | 56 … | } | |
57 | 57 … | } | |
58 | 58 … | ] | |
@@ -115,9 +115,9 @@ | |||
115 | 115 … | name: { type: 'string' } | |
116 | 116 … | } | |
117 | 117 … | } | |
118 | 118 … | } | |
119 | - }, | ||
119 … | + } | ||
120 | 120 … | } | |
121 | 121 … | ||
122 | 122 … | const validate = Validate(schema, { verbose: true }) | |
123 | 123 … |
schema/positionTypes/chooseOne.js | ||
---|---|---|
@@ -1,20 +1,20 @@ | ||
1 | 1 … | const Validate = require('is-my-json-valid') |
2 | 2 … | |
3 | 3 … | const typeString = 'chooseOne' |
4 | -const typeStringPattern =`^${typeString}$` | |
5 | -const typeStringRegex = new RegExp(typeStringPattern) | |
4 … | +const typeStringPattern = `^${typeString}$` | |
5 … | +const typeStringRegex = new RegExp(typeStringPattern) | |
6 | 6 … | |
7 | -function isValidTypeString(string) { | |
8 | - return typeStringRegex.test(string) | |
7 … | +function isValidTypeString (string) { | |
8 … | + return typeStringRegex.test(string) | |
9 | 9 … | } |
10 | 10 … | |
11 | -function create({choice, reason}) { | |
11 … | +function create ({choice, reason}) { | |
12 | 12 … | return { |
13 | 13 … | choice, |
14 | 14 … | type: typeString, |
15 | 15 … | reason |
16 | - } | |
16 … | + } | |
17 | 17 … | } |
18 | 18 … | |
19 | 19 … | var schema = { |
20 | 20 … | type: 'object', |
@@ -24,9 +24,9 @@ | ||
24 | 24 … | type: 'string', |
25 | 25 … | pattern: typeStringPattern |
26 | 26 … | }, |
27 | 27 … | choice: { |
28 | - type: 'string', | |
28 … | + type: 'string' | |
29 | 29 … | } |
30 | 30 … | } |
31 | 31 … | } |
32 | 32 … | |
@@ -37,5 +37,4 @@ | ||
37 | 37 … | schema, |
38 | 38 … | validate, |
39 | 39 … | create |
40 | 40 … | } |
41 | - |
stance.js | ||
---|---|---|
@@ -1,8 +1,8 @@ | ||
1 | 1 … | var Validate = require('is-my-json-valid') |
2 | 2 … | const { msgIdRegex, feedIdRegex, blobIdRegex } = require('ssb-ref') |
3 | 3 … | |
4 | -function create(){ | |
4 … | +function create () { | |
5 | 5 … | |
6 | 6 … | } |
7 | 7 … | |
8 | 8 … | const schema = { |
sync/isPoll.js | ||
---|---|---|
@@ -18,6 +18,4 @@ | ||
18 | 18 … | if (obj.value && obj.value.content) return obj.value.content |
19 | 19 … | |
20 | 20 … | return obj |
21 | 21 … | } |
22 | - | |
23 | - |
test/schema/poll.js | ||
---|---|---|
@@ -1,15 +1,14 @@ | ||
1 | 1 … | var test = require('tape') |
2 | -var {poll, isPoll, response} = require('../..'); | |
2 … | +var {poll, isPoll, response} = require('../..') | |
3 | 3 … | |
4 | -test('test', function(t) { | |
4 … | +test('test', function (t) { | |
5 | 5 … | t.true(true) |
6 | 6 … | t.end() |
7 | 7 … | }) |
8 | 8 … | |
9 | -test('create and validate an invalid chooseOne poll', function(t) { | |
9 … | +test('create and validate an invalid chooseOne poll', function (t) { | |
10 | 10 … | var pollOptions = {type: 'chooseOne'} |
11 | - var myPoll = poll({text:"how many food", mentions: null, recps: null, channel: null, pollOptions}) | |
11 … | + var myPoll = poll({text: 'how many food', mentions: null, recps: null, channel: null, pollOptions}) | |
12 | 12 … | t.false(isPoll(myPoll)) |
13 | 13 … | t.end() |
14 | 14 … | }) |
15 | - |
test/schema/pollDetails/chooseOne.js | ||
---|---|---|
@@ -1,14 +1,14 @@ | ||
1 | 1 … | var test = require('tape') |
2 | -var {create, validate} = require('../../../schema/pollDetails/chooseOne'); | |
2 … | +var {create, validate} = require('../../../schema/pollDetails/chooseOne') | |
3 | 3 … | |
4 | -test('create and validate an invalid chooseOne poll', function(t) { | |
5 | - var myPoll = create({choices:"how"}) | |
4 … | +test('create and validate an invalid chooseOne poll', function (t) { | |
5 … | + var myPoll = create({choices: 'how'}) | |
6 | 6 … | t.false(validate(myPoll)) |
7 | 7 … | t.end() |
8 | 8 … | }) |
9 | 9 … | |
10 | -test('create and validate a valid chooseOne poll', function(t) { | |
11 | - var myPoll = create({choices:["how","many", "food"]}) | |
10 … | +test('create and validate a valid chooseOne poll', function (t) { | |
11 … | + var myPoll = create({choices: ['how', 'many', 'food']}) | |
12 | 12 … | t.true(validate(myPoll)) |
13 | 13 … | t.end() |
14 | 14 … | }) |
Built with git-ssb-web