|
| 1 | +import { ModeledMethod } from "../modeled-method"; |
| 2 | +import { MethodSignature } from "../method"; |
| 3 | +import { assertNever } from "../../common/helpers-pure"; |
| 4 | + |
| 5 | +export type ModeledMethodValidationError = { |
| 6 | + title: string; |
| 7 | + message: string; |
| 8 | + actionText: string; |
| 9 | + index: number; |
| 10 | +}; |
| 11 | + |
| 12 | +/** |
| 13 | + * This method will reset any properties which are not used for the specific type of modeled method. |
| 14 | + * |
| 15 | + * It will also set the `provenance` to `manual` since multiple modelings of the same method with a |
| 16 | + * different provenance are not actually different. |
| 17 | + * |
| 18 | + * The returned canonical modeled method should only be used for comparisons. It should not be used |
| 19 | + * for display purposes, saving the model, or any other purpose which requires the original modeled |
| 20 | + * method to be preserved. |
| 21 | + * |
| 22 | + * @param modeledMethod The modeled method to canonicalize |
| 23 | + */ |
| 24 | +function canonicalizeModeledMethod( |
| 25 | + modeledMethod: ModeledMethod, |
| 26 | +): ModeledMethod { |
| 27 | + const methodSignature: MethodSignature = { |
| 28 | + signature: modeledMethod.signature, |
| 29 | + packageName: modeledMethod.packageName, |
| 30 | + typeName: modeledMethod.typeName, |
| 31 | + methodName: modeledMethod.methodName, |
| 32 | + methodParameters: modeledMethod.methodParameters, |
| 33 | + }; |
| 34 | + |
| 35 | + switch (modeledMethod.type) { |
| 36 | + case "none": |
| 37 | + return { |
| 38 | + ...methodSignature, |
| 39 | + type: "none", |
| 40 | + input: "", |
| 41 | + output: "", |
| 42 | + kind: "", |
| 43 | + provenance: "manual", |
| 44 | + }; |
| 45 | + case "source": |
| 46 | + return { |
| 47 | + ...methodSignature, |
| 48 | + type: "source", |
| 49 | + input: "", |
| 50 | + output: modeledMethod.output, |
| 51 | + kind: modeledMethod.kind, |
| 52 | + provenance: "manual", |
| 53 | + }; |
| 54 | + case "sink": |
| 55 | + return { |
| 56 | + ...methodSignature, |
| 57 | + type: "sink", |
| 58 | + input: modeledMethod.input, |
| 59 | + output: "", |
| 60 | + kind: modeledMethod.kind, |
| 61 | + provenance: "manual", |
| 62 | + }; |
| 63 | + case "summary": |
| 64 | + return { |
| 65 | + ...methodSignature, |
| 66 | + type: "summary", |
| 67 | + input: modeledMethod.input, |
| 68 | + output: modeledMethod.output, |
| 69 | + kind: modeledMethod.kind, |
| 70 | + provenance: "manual", |
| 71 | + }; |
| 72 | + case "neutral": |
| 73 | + return { |
| 74 | + ...methodSignature, |
| 75 | + type: "neutral", |
| 76 | + input: "", |
| 77 | + output: "", |
| 78 | + kind: "", |
| 79 | + provenance: "manual", |
| 80 | + }; |
| 81 | + default: |
| 82 | + assertNever(modeledMethod.type); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +export function validateModeledMethods( |
| 87 | + modeledMethods: ModeledMethod[], |
| 88 | +): ModeledMethodValidationError[] { |
| 89 | + // Anything that is not modeled will not be saved, so we don't need to validate it |
| 90 | + const consideredModeledMethods = modeledMethods.filter( |
| 91 | + (modeledMethod) => modeledMethod.type !== "none", |
| 92 | + ); |
| 93 | + |
| 94 | + const result: ModeledMethodValidationError[] = []; |
| 95 | + |
| 96 | + // If the same model is present multiple times, only the first one makes sense, so we should give |
| 97 | + // an error for any duplicates. |
| 98 | + const seenModeledMethods = new Set<string>(); |
| 99 | + for (const modeledMethod of consideredModeledMethods) { |
| 100 | + const canonicalModeledMethod = canonicalizeModeledMethod(modeledMethod); |
| 101 | + const key = JSON.stringify( |
| 102 | + canonicalModeledMethod, |
| 103 | + // This ensures the keys are always in the same order |
| 104 | + Object.keys(canonicalModeledMethod).sort(), |
| 105 | + ); |
| 106 | + |
| 107 | + if (seenModeledMethods.has(key)) { |
| 108 | + result.push({ |
| 109 | + title: "Duplicated classification", |
| 110 | + message: |
| 111 | + "This method has two identical or conflicting classifications.", |
| 112 | + actionText: "Modify or remove the duplicated classification.", |
| 113 | + index: modeledMethods.indexOf(modeledMethod), |
| 114 | + }); |
| 115 | + } else { |
| 116 | + seenModeledMethods.add(key); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const neutralModeledMethod = consideredModeledMethods.find( |
| 121 | + (modeledMethod) => modeledMethod.type === "neutral", |
| 122 | + ); |
| 123 | + const hasNonNeutralModeledMethod = consideredModeledMethods.some( |
| 124 | + (modeledMethod) => modeledMethod.type !== "neutral", |
| 125 | + ); |
| 126 | + |
| 127 | + // If there is a neutral model and any other model, that is an error |
| 128 | + if (neutralModeledMethod && hasNonNeutralModeledMethod) { |
| 129 | + // Another validation will validate that only one neutral method is present, so we only need |
| 130 | + // to return an error for the first one |
| 131 | + |
| 132 | + result.push({ |
| 133 | + title: "Conflicting classification", |
| 134 | + message: |
| 135 | + "This method has a neutral classification, which conflicts with other classifications.", |
| 136 | + actionText: "Modify or remove the neutral classification.", |
| 137 | + index: modeledMethods.indexOf(neutralModeledMethod), |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + // Sort by index so that the errors are always in the same order |
| 142 | + result.sort((a, b) => a.index - b.index); |
| 143 | + |
| 144 | + return result; |
| 145 | +} |
0 commit comments