mapcomplete/Models/ThemeConfig/Conversion/Validation.ts

427 lines
17 KiB
TypeScript
Raw Normal View History

2022-02-04 01:05:35 +01:00
import {DesugaringStep, Fuse, OnEvery} from "./Conversion";
import {LayerConfigJson} from "../Json/LayerConfigJson";
import LayerConfig from "../LayerConfig";
import {Utils} from "../../../Utils";
import Constants from "../../Constants";
import {Translation} from "../../../UI/i18n/Translation";
import {LayoutConfigJson} from "../Json/LayoutConfigJson";
import LayoutConfig from "../LayoutConfig";
import {TagRenderingConfigJson} from "../Json/TagRenderingConfigJson";
import {TagUtils} from "../../../Logic/Tags/TagUtils";
import {ExtractImages} from "./FixImages";
2022-02-10 23:16:14 +01:00
import ScriptUtils from "../../../scripts/ScriptUtils";
import {And} from "../../../Logic/Tags/And";
2022-02-17 23:54:14 +01:00
import Translations from "../../../UI/i18n/Translations";
class ValidateLanguageCompleteness extends DesugaringStep<any> {
private readonly _languages: string[];
constructor(...languages: string[]) {
super("Checks that the given object is fully translated in the specified languages", [], "ValidateLanguageCompleteness");
this._languages = languages;
}
2022-02-04 01:05:35 +01:00
convert(obj: any, context: string): { result: LayerConfig; errors: string[] } {
const errors = []
const translations = Translation.ExtractAllTranslationsFrom(
obj
)
for (const neededLanguage of this._languages ?? ["en"]) {
translations
.filter(t => t.tr.translations[neededLanguage] === undefined && t.tr.translations["*"] === undefined)
.forEach(missing => {
errors.push(context + "A theme should be translation-complete for " + neededLanguage + ", but it lacks a translation for " + missing.context + ".\n\tThe known translation is " + missing.tr.textFor('en'))
})
}
return {
result: obj,
2022-02-04 01:05:35 +01:00
errors
};
}
}
class ValidateTheme extends DesugaringStep<LayoutConfigJson> {
/**
* The paths where this layer is originally saved. Triggers some extra checks
* @private
*/
private readonly _path?: string;
private readonly knownImagePaths: Set<string>;
private readonly _isBuiltin: boolean;
constructor(knownImagePaths: Set<string>, path: string, isBuiltin: boolean) {
2022-02-17 23:54:14 +01:00
super("Doesn't change anything, but emits warnings and errors", [], "ValidateTheme");
this.knownImagePaths = knownImagePaths;
this._path = path;
this._isBuiltin = isBuiltin;
}
2022-02-10 23:16:14 +01:00
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors: string[], warnings: string[], information: string[] } {
const errors = []
const warnings = []
2022-02-10 23:16:14 +01:00
const information = []
2022-02-17 23:54:14 +01:00
const theme = new LayoutConfig(json, true, "test")
{
// Legacy format checks
if (this._isBuiltin) {
if (json["units"] !== undefined) {
errors.push("The theme " + json.id + " has units defined - these should be defined on the layer instead. (Hint: use overrideAll: { '+units': ... }) ")
}
if (json["roamingRenderings"] !== undefined) {
errors.push("Theme " + json.id + " contains an old 'roamingRenderings'. Use an 'overrideAll' instead")
}
}
}
{
2022-02-10 23:16:14 +01:00
// Check images: are they local, are the licenses there, is the theme icon square, ...
const images = new ExtractImages(this._isBuiltin).convertStrict(json, "validation")
const remoteImages = images.filter(img => img.indexOf("http") == 0)
for (const remoteImage of remoteImages) {
errors.push("Found a remote image: " + remoteImage + " in theme " + json.id + ", please download it.")
}
for (const image of images) {
if (image.indexOf("{") >= 0) {
2022-02-10 23:16:14 +01:00
information.push("Ignoring image with { in the path: " + image)
continue
}
2022-02-10 23:16:14 +01:00
if (image === "assets/SocialImage.png") {
continue
}
2022-02-10 23:16:14 +01:00
if (image.match(/[a-z]*/)) {
// This is a builtin img, e.g. 'checkmark' or 'crosshair'
continue;
}
if (this.knownImagePaths !== undefined && !this.knownImagePaths.has(image)) {
const ctx = context === undefined ? "" : ` in a layer defined in the theme ${context}`
errors.push(`Image with path ${image} not found or not attributed; it is used in ${json.id}${ctx}`)
}
}
2022-02-10 23:16:14 +01:00
if (json.icon.endsWith(".svg")) {
try {
ScriptUtils.ReadSvgSync(json.icon, svg => {
const width: string = svg.$.width;
const height: string = svg.$.height;
if (width !== height) {
2022-02-17 23:54:14 +01:00
const e = `the icon for theme ${json.id} is not square. Please square the icon at ${json.icon}` +
2022-02-10 23:16:14 +01:00
` Width = ${width} height = ${height}`;
(json.hideFromOverview ? warnings : errors).push(e)
}
})
} catch (e) {
console.error("Could not read " + json.icon + " due to " + e)
}
}
}
2022-02-17 23:54:14 +01:00
try {
if (theme.id !== theme.id.toLowerCase()) {
errors.push("Theme ids should be in lowercase, but it is " + theme.id)
}
const filename = this._path.substring(this._path.lastIndexOf("/") + 1, this._path.length - 5)
if (theme.id !== filename) {
errors.push("Theme ids should be the same as the name.json, but we got id: " + theme.id + " and filename " + filename + " (" + this._path + ")")
}
if (!this.knownImagePaths.has(theme.icon)) {
errors.push("The theme image " + theme.icon + " is not attributed or not saved locally")
}
const dups = Utils.Dupiclates(json.layers.map(layer => layer["id"]))
if (dups.length > 0) {
errors.push(`The theme ${json.id} defines multiple layers with id ${dups.join(", ")}`)
}
if (json["mustHaveLanguage"] !== undefined) {
const checked = new ValidateLanguageCompleteness(...json["mustHaveLanguage"])
2022-02-04 01:05:35 +01:00
.convert(theme, theme.id)
errors.push(...checked.errors)
}
2022-02-17 23:54:14 +01:00
if (!json.hideFromOverview && theme.id !== "personal") {
// Official, public themes must have a full english translation
const checked = new ValidateLanguageCompleteness("en")
.convert(theme, theme.id)
errors.push(...checked.errors)
}
} catch (e) {
errors.push(e)
}
return {
result: json,
errors,
2022-02-10 23:16:14 +01:00
warnings,
information
};
}
}
export class ValidateThemeAndLayers extends Fuse<LayoutConfigJson> {
constructor(knownImagePaths: Set<string>, path: string, isBuiltin: boolean) {
super("Validates a theme and the contained layers",
new ValidateTheme(knownImagePaths, path, isBuiltin),
new OnEvery("layers", new ValidateLayer(knownImagePaths, undefined, false))
);
}
}
2022-02-10 23:16:14 +01:00
class OverrideShadowingCheck extends DesugaringStep<LayoutConfigJson> {
constructor() {
2022-02-17 23:54:14 +01:00
super("Checks that an 'overrideAll' does not override a single override", [], "OverrideShadowingCheck");
}
2022-02-04 01:05:35 +01:00
convert(json: LayoutConfigJson, context: string): { result: LayoutConfigJson; errors?: string[]; warnings?: string[] } {
2022-02-10 23:16:14 +01:00
const overrideAll = json.overrideAll;
2022-02-10 23:16:14 +01:00
if (overrideAll === undefined) {
return {result: json}
}
2022-02-10 23:16:14 +01:00
const errors = []
2022-02-10 23:16:14 +01:00
const withOverride = json.layers.filter(l => l["override"] !== undefined)
for (const layer of withOverride) {
for (const key in overrideAll) {
2022-02-10 23:16:14 +01:00
if (layer["override"][key] !== undefined || layer["override"]["=" + key] !== undefined) {
const w = "The override of layer " + JSON.stringify(layer["builtin"]) + " has a shadowed property: " + key + " is overriden by overrideAll of the theme";
errors.push(w)
}
}
}
2022-02-10 23:16:14 +01:00
return {result: json, errors}
}
2022-02-10 23:16:14 +01:00
}
2022-02-10 23:16:14 +01:00
export class PrevalidateTheme extends Fuse<LayoutConfigJson> {
constructor() {
super("Various consistency checks on the raw JSON",
new OverrideShadowingCheck()
2022-02-10 23:16:14 +01:00
);
}
}
2022-02-10 23:16:14 +01:00
export class DetectShadowedMappings extends DesugaringStep<TagRenderingConfigJson> {
constructor() {
2022-02-17 23:54:14 +01:00
super("Checks that the mappings don't shadow each other", [], "DetectShadowedMappings");
}
2022-02-10 23:16:14 +01:00
2022-02-04 01:05:35 +01:00
convert(json: TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson; errors?: string[]; warnings?: string[] } {
const errors = []
2022-02-17 23:54:14 +01:00
const warnings = []
2022-02-10 23:16:14 +01:00
if (json.mappings === undefined || json.mappings.length === 0) {
return {result: json}
}
const parsedConditions = json.mappings.map(m => TagUtils.Tag(m.if))
2022-02-10 23:16:14 +01:00
for (let i = 0; i < json.mappings.length; i++) {
2022-02-18 00:12:32 +01:00
if(json.mappings[i].hideInAnswer === true){
continue
}
const keyValues = parsedConditions[i].asChange({});
const properties = []
keyValues.forEach(({k, v}) => {
properties[k] = v
})
2022-02-10 23:16:14 +01:00
for (let j = 0; j < i; j++) {
2022-02-17 23:54:14 +01:00
if(json.mappings[j].hideInAnswer === true){
continue
}
const doesMatch = parsedConditions[j].matchesProperties(properties)
2022-02-10 23:16:14 +01:00
if (doesMatch) {
// The current mapping is shadowed!
2022-02-18 00:12:32 +01:00
errors.push(`At ${context}: Mapping ${i} is shadowed by mapping ${j} and will thus never be shown:
2022-02-10 23:16:14 +01:00
The mapping ${parsedConditions[i].asHumanString(false, false, {})} is fully matched by a previous mapping, which matches:
${parsedConditions[j].asHumanString(false, false, {})}.
Move the mapping up to fix this problem
`)
}
}
}
2022-02-10 23:16:14 +01:00
return {
errors,
2022-02-17 23:54:14 +01:00
warnings,
result: json
};
}
}
2022-02-17 23:54:14 +01:00
export class DetectMappingsWithImages extends DesugaringStep<TagRenderingConfigJson> {
constructor() {
super("Checks that 'then'clauses in mappings don't have images, but use 'icon' instead", [], "DetectMappingsWithImages");
}
convert(json: TagRenderingConfigJson, context: string): { result: TagRenderingConfigJson; errors?: string[]; warnings?: string[] } {
const warnings = []
if (json.mappings === undefined || json.mappings.length === 0) {
return {result: json}
}
for (let i = 0; i < json.mappings.length; i++) {
const mapping = json.mappings[i]
const images = Utils.Dedup(Translations.T(mapping.then).ExtractImages())
if (images.length > 0) {
warnings.push(context + ".mappings[" + i + "]: A mapping has an image in the 'then'-clause. Remove the image there and use `\"icon\": <your-image>` instead. The images found are "+images.join(", "))
}
}
return {
warnings,
result: json
};
}
}
export class ValidateTagRenderings extends Fuse<TagRenderingConfigJson> {
constructor() {
super("Various validation on tagRenderingConfigs",
// TODO enable these checks again
// new DetectShadowedMappings(),
new DetectMappingsWithImages()
2022-02-17 23:54:14 +01:00
);
}
}
export class ValidateLayer extends DesugaringStep<LayerConfigJson> {
/**
* The paths where this layer is originally saved. Triggers some extra checks
* @private
*/
private readonly _path?: string;
private readonly knownImagePaths?: Set<string>;
private readonly _isBuiltin: boolean;
constructor(knownImagePaths: Set<string>, path: string, isBuiltin: boolean) {
2022-02-17 23:54:14 +01:00
super("Doesn't change anything, but emits warnings and errors", [], "ValidateLayer");
this.knownImagePaths = knownImagePaths;
this._path = path;
this._isBuiltin = isBuiltin;
}
2022-02-17 23:54:14 +01:00
convert(json: LayerConfigJson, context: string): { result: LayerConfigJson; errors: string[]; warnings?: string[], information?: string[] } {
const errors = []
const warnings = []
2022-02-17 23:54:14 +01:00
const information = []
if (typeof json === "string") {
errors.push(context + ": This layer hasn't been expanded: " + json)
return {
result: null,
errors
}
}
if (json["builtin"] !== undefined) {
errors.push(context + ": This layer hasn't been expanded: " + json)
return {
result: null,
errors
}
}
try {
{
// Some checks for legacy elements
if (json["overpassTags"] !== undefined) {
errors.push("Layer " + json.id + "still uses the old 'overpassTags'-format. Please use \"source\": {\"osmTags\": <tags>}' instead of \"overpassTags\": <tags> (note: this isn't your fault, the custom theme generator still spits out the old format)")
}
const forbiddenTopLevel = ["icon", "wayHandling", "roamingRenderings", "roamingRendering", "label", "width", "color", "colour", "iconOverlays"]
for (const forbiddenKey of forbiddenTopLevel) {
if (json[forbiddenKey] !== undefined)
errors.push(context + ": layer " + json.id + " still has a forbidden key " + forbiddenKey)
}
if (json["hideUnderlayingFeaturesMinPercentage"] !== undefined) {
errors.push(context + ": layer " + json.id + " contains an old 'hideUnderlayingFeaturesMinPercentage'")
}
}
{
// CHeck location of layer file
const expected: string = `assets/layers/${json.id}/${json.id}.json`
if (this._path != undefined && this._path.indexOf(expected) < 0) {
errors.push("Layer is in an incorrect place. The path is " + this._path + ", but expected " + expected)
}
}
if (this._isBuiltin) {
// Check for correct IDs
if (json.tagRenderings?.some(tr => tr["id"] === "")) {
const emptyIndexes: number[] = []
for (let i = 0; i < json.tagRenderings.length; i++) {
const tagRendering = json.tagRenderings[i];
if (tagRendering["id"] === "") {
emptyIndexes.push(i)
}
}
errors.push(`Some tagrendering-ids are empty or have an emtpy string; this is not allowed (at ${context}.tagRenderings.[${emptyIndexes.join(",")}])`)
}
const duplicateIds = Utils.Dupiclates((json.tagRenderings ?? [])?.map(f => f["id"]).filter(id => id !== "questions"))
if (duplicateIds.length > 0 && !Utils.runningFromConsole) {
errors.push(`Some tagRenderings have a duplicate id: ${duplicateIds} (at ${context}.tagRenderings)`)
}
if (json.description === undefined) {
if (Constants.priviliged_layers.indexOf(json.id) >= 0) {
errors.push(
context + ": A priviliged layer must have a description"
)
} else {
warnings.push(
context + ": A builtin layer should have a description"
)
}
}
}
2022-02-10 23:16:14 +01:00
if (json.tagRenderings !== undefined) {
2022-02-17 23:54:14 +01:00
const r = new OnEvery("tagRenderings", new ValidateTagRenderings()).convert(json, context)
warnings.push(...(r.warnings??[]))
errors.push(...(r.errors??[]))
information.push(...(r.information??[]))
}
2022-02-10 23:16:14 +01:00
2022-02-17 23:54:14 +01:00
if (json.presets !== undefined) {
// Check that a preset will be picked up by the layer itself
2022-02-17 23:54:14 +01:00
const baseTags = TagUtils.Tag(json.source.osmTags)
for (let i = 0; i < json.presets.length; i++) {
const preset = json.presets[i];
2022-02-17 23:54:14 +01:00
const tags: { k: string, v: string }[] = new And(preset.tags.map(t => TagUtils.Tag(t))).asChange({id: "node/-1"})
const properties = {}
for (const tag of tags) {
properties[tag.k] = tag.v
}
const doMatch = baseTags.matchesProperties(properties)
2022-02-17 23:54:14 +01:00
if (!doMatch) {
errors.push(context + ".presets[" + i + "]: This preset does not match the required tags of this layer. This implies that a newly added point will not show up.\n A newly created point will have properties: " + JSON.stringify(properties) + "\n The required tags are: " + baseTags.asHumanString(false, false, {}))
}
}
}
} catch (e) {
errors.push(e)
}
2022-02-10 23:16:14 +01:00
return {
result: json,
errors,
2022-02-17 23:54:14 +01:00
warnings,
information
};
}
}