Skip to content

Commit 65ebfdb

Browse files
authored
Merge pull request #133 from hed-standard/value-blacklist
Refactor interim value validation
2 parents 18443fc + 57552e3 commit 65ebfdb

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

parser/main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ const delimiters = new Set([','])
1414
*/
1515
const substituteCharacters = function (hedString) {
1616
const issues = []
17-
const illegalCharacterMap = { '\0': ['ASCII NUL', ' '] }
18-
const flaggedCharacters = /[^\w\d./$ :-]/g
17+
const illegalCharacterMap = { '\0': ['ASCII NUL', ' '], '\t': ['Tab', ' '] }
1918
const replaceFunction = function (match, offset) {
2019
if (match in illegalCharacterMap) {
2120
const [name, replacement] = illegalCharacterMap[match]
@@ -31,7 +30,7 @@ const substituteCharacters = function (hedString) {
3130
return match
3231
}
3332
}
34-
const fixedString = hedString.replace(flaggedCharacters, replaceFunction)
33+
const fixedString = hedString.replace(/./g, replaceFunction)
3534

3635
return [fixedString, issues]
3736
}

parser/parsedHedTag.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ export class ParsedHed3Tag extends ParsedHedTag {
233233
* @param {string} schemaName The label of this tag's schema in the dataset's schema spec.
234234
*/
235235
_convertTag(hedString, hedSchemas, schemaName) {
236+
const hed3ValidCharacters = /^[^{}[\]()~,\0\t]+$/
237+
if (!hed3ValidCharacters.test(this.originalTag)) {
238+
throw new Error('The parser failed to properly remove an illegal or special character.')
239+
}
240+
236241
if (hedSchemas.isSyntaxOnly) {
237242
this.canonicalTag = this.originalTag
238243
this.conversionIssues = []

parser/splitHedString.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,6 @@ const checkTagForInvalidCharacters = function (hedString, tagSpec, tag, invalidS
390390
for (let i = 0; i < tag.length; i++) {
391391
const character = tag.charAt(i)
392392
if (invalidSet.has(character)) {
393-
tagSpec.invalidCharacter = true
394393
issues.push(
395394
generateIssue('invalidCharacter', {
396395
character: character,

tests/event.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ describe('HED string and event validation', () => {
235235
it('should substitute and warn for certain illegal characters', () => {
236236
const testStrings = {
237237
nul: '/Attribute/Object side/Left,/Participant/Effect/Body part/Arm\0',
238+
tab: '/Attribute/Object side/Left,/Participant/Effect/Body part/Arm\t',
238239
}
239240
const expectedIssues = {
240241
nul: [
@@ -244,6 +245,13 @@ describe('HED string and event validation', () => {
244245
string: testStrings.nul,
245246
}),
246247
],
248+
tab: [
249+
generateIssue('invalidCharacter', {
250+
character: 'Tab',
251+
index: 61,
252+
string: testStrings.tab,
253+
}),
254+
],
247255
}
248256
// No-op function as this check is done during the parsing stage.
249257
// eslint-disable-next-line no-unused-vars

validator/event/hed3.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ const topLevelTagGroupType = 'topLevelTagGroup'
1919
export class Hed3Validator extends HedValidator {
2020
/**
2121
* The parsed definitions.
22+
*
2223
* @type {Map<string, ParsedHedGroup>}
2324
*/
2425
definitions
2526

2627
/**
2728
* Constructor.
29+
*
2830
* @param {ParsedHedString} parsedString The parsed HED string to be validated.
2931
* @param {Schemas} hedSchemas The collection of HED schemas.
3032
* @param {Map<string, ParsedHedGroup>} definitions The parsed definitions.
@@ -126,6 +128,7 @@ export class Hed3Validator extends HedValidator {
126128

127129
/**
128130
* Check that the unit is valid for the tag's unit class.
131+
*
129132
* @param {ParsedHed3Tag} tag A HED tag.
130133
*/
131134
checkIfTagUnitClassUnitsAreValid(tag) {
@@ -281,6 +284,7 @@ export class Hed3Validator extends HedValidator {
281284

282285
/**
283286
* Validate a unit and strip it from the value.
287+
*
284288
* @param {ParsedHed3Tag} tag A HED tag.
285289
* @returns {[boolean, boolean, string]} Whether a unit was found, whether it was valid, and the stripped value.
286290
*/
@@ -337,6 +341,8 @@ export class Hed3Validator extends HedValidator {
337341
*
338342
* @param {string} value The stripped value.
339343
* @param {boolean} isNumeric Whether the tag is numeric.
344+
* @returns {boolean} Whether the stripped value is valid.
345+
* @todo This function is a placeholder until support for value classes is implemented.
340346
*/
341347
validateValue(value, isNumeric) {
342348
if (value === '#') {
@@ -346,8 +352,8 @@ export class Hed3Validator extends HedValidator {
346352
if (isNumeric) {
347353
return isNumber(value)
348354
}
349-
const hed3ValidValueCharacters = /^[-a-zA-Z0-9.$%^+_; ]+$/
350-
return hed3ValidValueCharacters.test(value)
355+
// TODO: Placeholder.
356+
return true
351357
}
352358

353359
/**

0 commit comments

Comments
 (0)