Skip to content

Commit 1c8d40b

Browse files
bcomnesvoxpelli
andauthored
Add defaults for all values (#4)
* Return 0 value defaults and default values This makes it easier downstream where you can assume these things are iterable. * Strengthen types * Tweak `githubApp` schema default Co-authored-by: Pelle Wessman <[email protected]>
1 parent ebbf55a commit 1c8d40b

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

index.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ const { socketYmlSchemaV1 } = require('./lib/v1')
1010

1111
/**
1212
* @typedef SocketYmlGitHub
13-
* @property {boolean} [enabled] enable/disable the Socket.dev GitHub app entirely
14-
* @property {boolean} [projectReportsEnabled] enable/disable Github app project report checks
15-
* @property {boolean} [pullRequestAlertsEnabled] enable/disable GitHub app pull request alert checks
13+
* @property {boolean} enabled enable/disable the Socket.dev GitHub app entirely
14+
* @property {boolean} projectReportsEnabled enable/disable Github app project report checks
15+
* @property {boolean} pullRequestAlertsEnabled enable/disable GitHub app pull request alert checks
1616
*/
1717

1818
/**
1919
* @typedef SocketYml
2020
* @property {2} version
21-
* @property {string[]} [projectIgnorePaths]
22-
* @property {{ [issueName: string]: boolean }} [issueRules]
23-
* @property {SocketYmlGitHub} [githubApp]
21+
* @property {string[]} projectIgnorePaths
22+
* @property {{ [issueName: string]: boolean }} issueRules
23+
* @property {SocketYmlGitHub} githubApp
2424
*/
2525

2626
/** @type {import('ajv').JSONSchemaType<SocketYml>} */
@@ -32,24 +32,24 @@ const socketYmlSchema = {
3232
projectIgnorePaths: {
3333
type: 'array',
3434
items: { type: 'string' },
35-
nullable: true
35+
default: []
3636
},
3737
issueRules: {
3838
type: 'object',
39-
nullable: true,
4039
required: [],
41-
additionalProperties: { type: 'boolean' }
40+
additionalProperties: { type: 'boolean' },
41+
default: {}
4242
},
4343
githubApp: {
4444
type: 'object',
45-
nullable: true,
4645
properties: {
47-
enabled: { type: 'boolean', nullable: true, default: true },
48-
projectReportsEnabled: { type: 'boolean', nullable: true, default: true },
49-
pullRequestAlertsEnabled: { type: 'boolean', nullable: true, default: true },
46+
enabled: { type: 'boolean', default: true },
47+
projectReportsEnabled: { type: 'boolean', default: true },
48+
pullRequestAlertsEnabled: { type: 'boolean', default: true },
5049
},
5150
required: [],
5251
additionalProperties: false,
52+
default: { enabled: true, projectReportsEnabled: true, pullRequestAlertsEnabled: true }
5353
},
5454
},
5555
required: ['version'],
@@ -180,9 +180,9 @@ async function parseV1SocketConfig (parsedV1Content) {
180180
projectIgnorePaths: parsedV1Content?.ignore ?? [],
181181
issueRules: parsedV1Content?.issues ?? {},
182182
githubApp: {
183-
enabled: parsedV1Content?.enabled,
184-
pullRequestAlertsEnabled: parsedV1Content?.pullRequestAlertsEnabled,
185-
projectReportsEnabled: parsedV1Content?.projectReportsEnabled
183+
enabled: Boolean(parsedV1Content?.enabled),
184+
pullRequestAlertsEnabled: Boolean(parsedV1Content?.pullRequestAlertsEnabled),
185+
projectReportsEnabled: Boolean(parsedV1Content?.projectReportsEnabled)
186186
}
187187
}
188188
return v2

test/parse.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ const {
1414
chai.use(chaiAsPromised)
1515
chai.should()
1616

17+
const defaults = {
18+
'githubApp': {
19+
'enabled': true,
20+
'projectReportsEnabled': true,
21+
'pullRequestAlertsEnabled': true,
22+
},
23+
'issueRules': {},
24+
'projectIgnorePaths': [],
25+
}
26+
1727
describe('parseSocketConfig()', () => {
1828
it('should read and parse socket.yml', async () => {
1929
const fileContent = await readFile(path.resolve(__dirname, 'sample.yml'), 'utf8')
@@ -72,7 +82,7 @@ bar: {{ def }} {{ efg }}
7282
version: 2
7383
foo: true
7484
`)
75-
.should.eventually.become({ version: 2 })
85+
.should.eventually.become({ version: 2, ...defaults })
7686
})
7787

7888
it('should coerce types', async () => {
@@ -82,6 +92,7 @@ projectIgnorePaths: foobar
8292
`)
8393
.should.eventually.become({
8494
version: 2,
95+
...defaults,
8596
projectIgnorePaths: ['foobar'],
8697
})
8798
})

0 commit comments

Comments
 (0)