Skip to content

Commit

Permalink
add validation for protobuf, add testing of wrong validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomás Ciccola committed May 14, 2024
1 parent 67b9d8e commit c01dc25
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"protobufjs": "^7.2.5",
"type-fest": "^4.1.0",
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.4"
"typedoc-plugin-markdown": "^3.15.4",
"validate-color": "^2.2.4"
},
"eslintConfig": {
"env": {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '../proto/observation/v5.js'
import { ExhaustivenessError, parseVersionId } from './utils.js'
import { CoreOwnership, type Observation, type Track } from '../index.js'
import validateColor from 'validate-color'

/** Function type for converting a protobuf type of any version for a particular
* schema name, and returning the most recent JSONSchema type */
Expand Down Expand Up @@ -66,6 +67,9 @@ export const convertField: ConvertFunction<'field'> = (mapeoDoc) => {
}

export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
if (!validateColor.validateHTMLColorHex(mapeoDoc.color)) {
throw new Error(`invalid color string ${mapeoDoc.color}`)
}
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/bad-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,25 @@ export const badDocs = [
],
},
},
{
text: 'preset with invalid color string',
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
schemaName: 'preset',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
createdBy: cachedValues.createdBy,
links: [],
name: 'myPreset',
geometry: ['point'],
tags: {},
addTags: {},
removeTags: {},
fieldIds: [],
terms: [],
deleted: false,
color: '#ff00g1',
},
},
]
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ test('Bad docs throw when encoding', () => {
}
})

test(`Bad docs won't validate`, () => {
for (const { text, doc } of badDocs) {
assert.throws(() => {
// @ts-expect-error
validate(doc)
}, text)
}
})

test('validate bad docs', () => {
for (const schemaName of Object.keys(currentSchemaVersions)) {
assert(
Expand Down

0 comments on commit c01dc25

Please sign in to comment.