The filter config now refuses regexTags except case-invariant regextags

This commit is contained in:
Pieter Vander Vennet 2022-06-07 03:38:13 +02:00
parent 0a81dd7e10
commit de6d2d71ac

View file

@ -9,6 +9,7 @@ import {UIEventSource} from "../../Logic/UIEventSource";
import {FilterState} from "../FilteredLayer"; import {FilterState} from "../FilteredLayer";
import {QueryParameters} from "../../Logic/Web/QueryParameters"; import {QueryParameters} from "../../Logic/Web/QueryParameters";
import {Utils} from "../../Utils"; import {Utils} from "../../Utils";
import {RegexTag} from "../../Logic/Tags/RegexTag";
export default class FilterConfig { export default class FilterConfig {
public readonly id: string public readonly id: string
@ -29,7 +30,6 @@ export default class FilterConfig {
} }
if (json.id.match(/^[a-zA-Z0-9_-]*$/) === null) { if (json.id.match(/^[a-zA-Z0-9_-]*$/) === null) {
throw `A filter with invalid id was found at ${context}. Ids should only contain letters, numbers or - _` throw `A filter with invalid id was found at ${context}. Ids should only contain letters, numbers or - _`
} }
if (json.options.map === undefined) { if (json.options.map === undefined) {
@ -43,12 +43,13 @@ export default class FilterConfig {
option.question, option.question,
`${ctx}.question` `${ctx}.question`
); );
let osmTags = undefined; let osmTags: undefined | TagsFilter = undefined;
if ((option.fields?.length ?? 0) == 0 && option.osmTags !== undefined) { if ((option.fields?.length ?? 0) == 0 && option.osmTags !== undefined) {
osmTags = TagUtils.Tag( osmTags = TagUtils.Tag(
option.osmTags, option.osmTags,
`${ctx}.osmTags` `${ctx}.osmTags`
); );
FilterConfig.validateSearch(osmTags, ctx)
} }
if (question === undefined) { if (question === undefined) {
throw `Invalid filter: no question given at ${ctx}` throw `Invalid filter: no question given at ${ctx}`
@ -85,6 +86,10 @@ export default class FilterConfig {
} }
} }
if(option.osmTags !== undefined){
FilterConfig.validateSearch(TagUtils.Tag(option.osmTags), ctx)
}
return {question: question, osmTags: osmTags, fields, originalTagsSpec: option.osmTags}; return {question: question, osmTags: osmTags, fields, originalTagsSpec: option.osmTags};
}); });
@ -101,6 +106,26 @@ export default class FilterConfig {
} }
private static validateSearch(osmTags: TagsFilter, ctx: string){
osmTags.visit(t => {
if (!(t instanceof RegexTag)) {
return;
}
if(typeof t.value == "string"){
return;
}
if(t.value.source == '^..*$' || t.value.source == '^[\\s\\S][\\s\\S]*$' /*Compiled regex with 'm'*/){
return
}
if(!t.value.ignoreCase) {
throw `At ${ctx}: The filter for key '${t.key}' uses a regex '${t.value}', but you should use a case invariant regex with ~i~ instead, as search should be case insensitive`
}
})
}
public initState(): UIEventSource<FilterState> { public initState(): UIEventSource<FilterState> {
function reset(state: FilterState): string { function reset(state: FilterState): string {