Skip to content

Commit

Permalink
Make preset.color optional
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn committed Sep 12, 2024
1 parent 9ea53a1 commit e585e2d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion proto/preset/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ message Preset_1 {
repeated FieldRef fieldRefs = 10;
optional IconRef iconRef = 11;
repeated string terms = 12;
string color = 13 [(required) = true];
optional string color = 13;

enum Geometry {
geometry_unspecified = 0;
Expand Down
3 changes: 1 addition & 2 deletions schema/preset/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@
"removeTags",
"fieldRefs",
"schemaName",
"terms",
"color"
"terms"
],
"additionalProperties": false
}
13 changes: 11 additions & 2 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import {
type JsonTagValue,
type MapeoDocDecode,
} from '../types.js'
import type { Icon, Observation, Track } from '../index.js'
import {
type Icon,
type Observation,
type Track,
valueSchemas,
} from '../index.js'
import type {
Observation_1_Attachment,
Observation_1_Metadata,
Expand Down Expand Up @@ -153,7 +158,11 @@ export const convertPreset: ConvertFunction<'preset'> = (
const jsonSchemaCommon = convertCommon(common, versionObj)

ensure(name, 'preset', 'name')
ensure(color, 'preset', 'color')

const colorRegex = RegExp(valueSchemas.preset.properties.color.pattern)
if (color && !colorRegex.test(color)) {
throw new Error(`Invalid color string ${color}`)
}

const geometry = rest.geometry.filter(
(geomType): geomType is JsonSchemaPresetGeomItem =>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const convertField: ConvertFunction<'field'> = (mapeoDoc) => {
}

export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
const colorRegex = valueSchemas.preset.properties.color.pattern
if (!mapeoDoc.color.match(colorRegex)) {
const colorRegex = RegExp(valueSchemas.preset.properties.color.pattern)
if (mapeoDoc.color && !colorRegex.test(mapeoDoc.color)) {
throw new Error(`invalid color string ${mapeoDoc.color}`)
}

Expand Down
1 change: 0 additions & 1 deletion test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export const goodDocsMinimal = [
},
terms: [],
deleted: false,
color: '#ff00ff',
},
expected: {},
},
Expand Down

0 comments on commit e585e2d

Please sign in to comment.