Skip to content

Commit

Permalink
feat: add color to preset (#183)
Browse files Browse the repository at this point in the history
* add color to preset

* add pattern for validating a hex color (#rrggbb)

* only allow for #rrggbb not #rgb

* add validation for protobuf, add testing of wrong validation

* remove capture group on color schema pattern

Co-authored-by: Evan Hahn <me@evanhahn.com>

* validate color by using the same pattern on the schema instead of an

external library

---------

Co-authored-by: Tomás Ciccola <tciccola@digital-democracy.com>
Co-authored-by: Evan Hahn <me@evanhahn.com>
  • Loading branch information
3 people authored May 15, 2024
1 parent 32cb667 commit 2e9744f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 13 deletions.
1 change: 1 addition & 0 deletions proto/preset/v2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ message Preset_2 {
repeated bytes fieldIds = 10;
optional bytes iconId = 11;
repeated string terms = 12;
string color = 13;
}
30 changes: 18 additions & 12 deletions schema/preset/v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,23 @@
"items": {
"type": "string"
}
},
"color": {
"description": "string representation of a color in 24 bit (#rrggbb)",
"type": "string",
"pattern": "^#[a-fA-F0-9]{6}$"
}
},
"required": [
"name",
"geometry",
"tags",
"addTags",
"removeTags",
"fieldIds",
"schemaName",
"terms"
],
"additionalProperties": false
},
"required": [
"name",
"geometry",
"tags",
"addTags",
"removeTags",
"fieldIds",
"schemaName",
"terms",
"color"
],
"additionalProperties": false
}
11 changes: 10 additions & 1 deletion src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
type Observation_5_Attachment,
} from '../proto/observation/v5.js'
import { ExhaustivenessError, parseVersionId } from './utils.js'
import { CoreOwnership, type Observation, type Track } from '../index.js'
import {
CoreOwnership,
valueSchemas,
type Observation,
type Track,
} from '../index.js'

/** 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 +71,10 @@ export const convertField: ConvertFunction<'field'> = (mapeoDoc) => {
}

export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
const colorRegex = valueSchemas.preset.properties.color.pattern
if (!mapeoDoc.color.match(colorRegex)) {
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',
},
},
]
1 change: 1 addition & 0 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const goodDocsCompleted = [
},
fieldIds: cachedValues.fieldIds,
iconId: cachedValues.iconId,
color: '#ff00ff',
terms: ['imastring'],
deleted: false,
},
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const goodDocsMinimal = [
fieldIds: [],
terms: [],
deleted: false,
color: '#ff00ff',
},
expected: {},
},
Expand Down
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 2e9744f

Please sign in to comment.