Skip to content

Commit 42e905d

Browse files
committed
feat: add JSON Schema validations for the OpenSSF Scorecard results
1 parent 6b06409 commit 42e905d

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

__tests__/schemas.test.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { sampleGithubOrg, sampleGithubListOrgRepos, sampleGithubRepository } = require('../__fixtures__')
2-
const { validateGithubOrg, validateGithubListOrgRepos, validateGithubRepository } = require('../src/schemas')
1+
const { sampleGithubOrg, sampleGithubListOrgRepos, sampleGithubRepository, sampleOSSFScorecardResult } = require('../__fixtures__')
2+
const { validateGithubOrg, validateGithubListOrgRepos, validateGithubRepository, validateOSSFResult } = require('../src/schemas')
33

44
describe('schemas', () => {
55
describe('validateGithubOrg', () => {
@@ -53,4 +53,19 @@ describe('schemas', () => {
5353
expect(() => validateGithubRepository(invalidData)).toThrow()
5454
})
5555
})
56+
describe('validateOSSFResult', () => {
57+
test('Should not throw an error with valid data', () => {
58+
expect(() => validateOSSFResult(sampleOSSFScorecardResult)).not.toThrow()
59+
})
60+
61+
test('Should not throw an error with additional data', () => {
62+
const additionalData = { ...sampleOSSFScorecardResult, additionalKey: 'value' }
63+
expect(() => validateOSSFResult(additionalData)).not.toThrow()
64+
})
65+
66+
test('Should throw an error with invalid data', () => {
67+
const invalidData = { ...sampleOSSFScorecardResult, score: '123' }
68+
expect(() => validateOSSFResult(invalidData)).toThrow()
69+
})
70+
})
5671
})

src/schemas/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const addFormats = require('ajv-formats')
33
const githubOrganizationSchema = require('./githubOrganization.json')
44
const githubListOrgReposSchema = require('./githubListOrgRepos.json')
55
const githubRepositorySchema = require('./githubRepository.json')
6+
const ossfScorecardResultSchema = require('./ossfScorecardResult.json')
67

78
const ajv = new Ajv()
89
addFormats(ajv)
@@ -39,8 +40,19 @@ const validateGithubRepository = (data) => {
3940
return null
4041
}
4142

43+
const validateOSSFResult = (data) => {
44+
const validate = ajv.compile(ossfScorecardResultSchema)
45+
const valid = validate(data)
46+
if (!valid) {
47+
const readableErrors = getReadableErrors(validate)
48+
throw new Error(`Error when validating the OSSF Scorecard result: ${readableErrors}`)
49+
}
50+
return null
51+
}
52+
4253
module.exports = {
4354
validateGithubOrg,
4455
validateGithubListOrgRepos,
45-
validateGithubRepository
56+
validateGithubRepository,
57+
validateOSSFResult
4658
}

src/schemas/ossfScorecardResult.json

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"title": "OpenSSF Scorecard Result",
3+
"description": "OpenSSF Scorecard Result for a project",
4+
"type": "object",
5+
"properties": {
6+
"date": {
7+
"type": "string",
8+
"format": "date-time",
9+
"examples": ["2024-12-11T23:55:17Z"]
10+
},
11+
"repo": {
12+
"type": "object",
13+
"properties": {
14+
"name": {
15+
"type": "string",
16+
"examples": ["github.com/octocat/Hello-World"]
17+
},
18+
"commit": {
19+
"type": "string",
20+
"examples": ["e739f419e56442b754e4fea6dbcf98c1c8d00dda"]
21+
}
22+
}
23+
},
24+
"scorecard": {
25+
"type": "object",
26+
"properties": {
27+
"version": {
28+
"type": "string",
29+
"examples": ["v5.0.0"]
30+
},
31+
"commit": {
32+
"type": "string",
33+
"examples": ["ea7e27ed41b76ab879c862fa0ca4cc9c61764ee4"]
34+
}
35+
}
36+
},
37+
"score": {
38+
"type": "number",
39+
"examples": [6]
40+
},
41+
"checks": {
42+
"type": ["array", "null"],
43+
"items": {
44+
"type": "object",
45+
"properties": {
46+
"details": {
47+
"type": ["string", "null"],
48+
"examples": [null]
49+
},
50+
"score": {
51+
"type": "number",
52+
"examples": [10]
53+
},
54+
"reason": {
55+
"type": "string",
56+
"examples": ["no binaries found in the repo"]
57+
},
58+
"name": {
59+
"type": "string",
60+
"examples": ["Binary-Artifacts"]
61+
},
62+
"documentation": {
63+
"type": "object",
64+
"properties": {
65+
"url": {
66+
"type": "string",
67+
"format": "uri",
68+
"examples": [
69+
"https://github.com/ossf/scorecard/blob/ea7e27ed41b76ab879c862fa0ca4cc9c61764ee4/docs/checks.md#binary-artifacts"
70+
]
71+
},
72+
"short": {
73+
"type": "string",
74+
"examples": [
75+
"Determines if the project has generated executable (binary) artifacts in the source repository."
76+
]
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+

0 commit comments

Comments
 (0)