Skip to content

Commit 378f077

Browse files
authored
update examples (#693)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent 9c5df1f commit 378f077

File tree

6 files changed

+65
-23
lines changed

6 files changed

+65
-23
lines changed

.github/workflows/nodejs.yml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ jobs:
251251
name: dist.node
252252
path: dist.node
253253
- name: setup library
254-
run: npm ci --ignore-scripts --omit=dev --include=optional --loglevel=silly
254+
run: |
255+
set -ex
256+
npm ci --ignore-scripts --omit=dev --include=optional --loglevel=silly
257+
## rebuild deps for which scripts were ignored, or partially installed - since "ignore-scripts" was used
258+
npm rebuild --loglevel=silly libxmljs2 || npm uninstall --no-save libxmljs2
255259
- name: setup example project
256260
run: npm i --no-save --loglevel=silly
257261
working-directory: ${{ env.EXAMPLE_DIR }}
@@ -300,12 +304,18 @@ jobs:
300304
name: dist.node
301305
path: dist.node
302306
- name: setup library
303-
run: npm ci --ignore-scripts --omit=dev --include=optional --loglevel=silly
307+
run: |
308+
set -ex
309+
npm ci --ignore-scripts --omit=dev --include=optional --loglevel=silly
310+
## rebuild deps for which scripts were ignored, or partially installed - since "ignore-scripts" was used
311+
npm rebuild --loglevel=silly libxmljs2 || npm uninstall --no-save libxmljs2
304312
- name: setup example project
305313
run: npm i --no-save --loglevel=silly 'typescript@${{ matrix.typescript-version }}'
314+
working-directory: ${{ env.EXAMPLE_DIR }}
306315
- name: get TS-version
307316
id: ts_version
308317
run: node -p '`v=ts${require("typescript").version.split(".",2).join(".")}`' >> "$GITHUB_OUTPUT"
318+
working-directory: ${{ env.EXAMPLE_DIR }}
309319
- name: adjust @types/node version
310320
run: npm i --no-save --loglevel=silly '@types/node@${{ matrix.nodeTypes-version || steps.ts_version.outputs.v}}'
311321
working-directory: ${{ env.EXAMPLE_DIR }}

examples/node-javascript/example.cjs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ console.log(serializedJson)
4343
const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
4444
jsonValidator.validate(serializedJson)
4545
.then(validationErrors => {
46-
if (validationErrors !== null) {
46+
if (validationErrors === null) {
47+
console.info('JSON valid')
48+
} else {
4749
throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
4850
}
4951
})
5052
.catch(err => {
51-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
53+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
54+
console.info('JSON validation skipped:', err)
55+
} else {
5256
throw err
5357
}
5458
})
@@ -60,12 +64,16 @@ console.log(serializedXML)
6064
const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
6165
xmlValidator.validate(serializedXML)
6266
.then(validationErrors => {
63-
if (validationErrors !== null) {
67+
if (validationErrors === null) {
68+
console.info('XML valid')
69+
} else {
6470
throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
6571
}
6672
})
6773
.catch(err => {
68-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
74+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
75+
console.info('XML validation skipped:', err)
76+
} else {
6977
throw err
7078
}
7179
})

examples/node-javascript/example.mjs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ console.log(serializedJson)
4343
const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
4444
try {
4545
const validationErrors = await jsonValidator.validate(serializedJson)
46-
if (validationErrors !== null) {
47-
throw new Error('ValidationError:\n' + JSON.stringify(validationErrors))
46+
if (validationErrors === null) {
47+
console.info('JSON valid')
48+
} else {
49+
throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
4850
}
4951
} catch (err) {
50-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
52+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
53+
console.info('JSON validation skipped:', err)
54+
} else {
5155
throw err
5256
}
5357
}
@@ -59,11 +63,15 @@ console.log(serializedXML)
5963
const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
6064
try {
6165
const validationErrors = await xmlValidator.validate(serializedXML)
62-
if (validationErrors !== null) {
63-
throw new Error('ValidationError:\n' + JSON.stringify(validationErrors))
66+
if (validationErrors === null) {
67+
console.info('XML valid')
68+
} else {
69+
throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
6470
}
6571
} catch (err) {
66-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
72+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
73+
console.info('XML validation skipped:', err)
74+
} else {
6775
throw err
6876
}
6977
}

examples/node-typescript/example.cjs/src/example.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,16 @@ console.log(serializedJson)
4343
const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
4444
jsonValidator.validate(serializedJson)
4545
.then(validationErrors => {
46-
if (validationErrors !== null) {
46+
if (validationErrors === null) {
47+
console.info('JSON valid')
48+
} else {
4749
throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
4850
}
4951
})
5052
.catch(err => {
51-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
53+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
54+
console.info('JSON validation skipped:', err)
55+
} else {
5256
throw err
5357
}
5458
})
@@ -60,12 +64,16 @@ console.log(serializedXML)
6064
const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
6165
xmlValidator.validate(serializedXML)
6266
.then(validationErrors => {
63-
if (validationErrors !== null) {
67+
if (validationErrors === null) {
68+
console.info('XML valid')
69+
} else {
6470
throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
6571
}
6672
})
6773
.catch(err => {
68-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
74+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
75+
console.info('XML validation skipped:', err)
76+
} else {
6977
throw err
7078
}
7179
})

examples/node-typescript/example.mjs/src/example.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ console.log(serializedJson)
4343
const jsonValidator = new CDX.Validation.JsonStrictValidator(serializeSpec.version)
4444
try {
4545
const validationErrors = await jsonValidator.validate(serializedJson)
46-
if (validationErrors !== null) {
47-
throw new Error('ValidationError:\n' + JSON.stringify(validationErrors))
46+
if (validationErrors === null) {
47+
console.info('JSON valid')
48+
} else {
49+
throw new Error('JSON ValidationError:\n' + JSON.stringify(validationErrors))
4850
}
4951
} catch (err) {
50-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
52+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
53+
console.info('JSON validation skipped:', err)
54+
} else {
5155
throw err
5256
}
5357
}
@@ -59,11 +63,15 @@ console.log(serializedXML)
5963
const xmlValidator = new CDX.Validation.XmlValidator(serializeSpec.version)
6064
try {
6165
const validationErrors = await xmlValidator.validate(serializedXML)
62-
if (validationErrors !== null) {
63-
throw new Error('ValidationError:\n' + JSON.stringify(validationErrors))
66+
if (validationErrors === null) {
67+
console.info('XML valid')
68+
} else {
69+
throw new Error('XML ValidationError:\n' + JSON.stringify(validationErrors))
6470
}
6571
} catch (err) {
66-
if (!(err instanceof CDX.Validation.MissingOptionalDependencyError)) {
72+
if (err instanceof CDX.Validation.MissingOptionalDependencyError) {
73+
console.info('XML validation skipped:', err)
74+
} else {
6775
throw err
6876
}
6977
}

src/validation/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ SPDX-License-Identifier: Apache-2.0
1717
Copyright (c) OWASP Foundation. All Rights Reserved.
1818
*/
1919

20-
import { type Version } from '../spec'
20+
import type { Version } from '../spec'
2121

2222
export class NotImplementedError extends Error {
2323
constructor (version: Version) {

0 commit comments

Comments
 (0)