Skip to content

Commit

Permalink
validate color by using the same pattern on the schema instead of an
Browse files Browse the repository at this point in the history
external library
  • Loading branch information
Tomás Ciccola committed May 14, 2024
1 parent dc578fe commit e99cc47
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@
"protobufjs": "^7.2.5",
"type-fest": "^4.1.0",
"typedoc": "^0.24.8",
"typedoc-plugin-markdown": "^3.15.4",
"validate-color": "^2.2.4"
"typedoc-plugin-markdown": "^3.15.4"
},
"eslintConfig": {
"env": {
Expand Down
11 changes: 8 additions & 3 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +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 validateColor from 'validate-color'
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 @@ -67,7 +71,8 @@ export const convertField: ConvertFunction<'field'> = (mapeoDoc) => {
}

export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
if (!validateColor.validateHTMLColorHex(mapeoDoc.color)) {
const colorRegex = valueSchemas.preset.properties.color.pattern
if (!mapeoDoc.color.match(colorRegex)) {
throw new Error(`invalid color string ${mapeoDoc.color}`)
}
return {
Expand Down

0 comments on commit e99cc47

Please sign in to comment.