{"record":{"id":"2abbef5da36b5394","repo":"gchq/CyberChef","slug":"incorrect-number-of-samples-perhaps-you-need-to-m","errorCode":null,"errorMessage":"Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?","messagePattern":"Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples\\?","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/Diff.mjs","lineNumber":86,"sourceCode":"     */\n    run(input, args) {\n        const [\n                sampleDelim,\n                diffBy,\n                showAdded,\n                showRemoved,\n                showSubtraction,\n                ignoreWhitespace\n            ] = args,\n            samples = input.split(sampleDelim);\n        let output = \"\",\n            diff;\n\n        // Node and Webpack load modules slightly differently\n        const jsdiff = JsDiff.default ? JsDiff.default : JsDiff;\n\n        if (!samples || samples.length !== 2) {\n            throw new OperationError(\"Incorrect number of samples, perhaps you need to modify the sample delimiter or add more samples?\");\n        }\n\n        switch (diffBy) {\n            case \"Character\":\n                diff = jsdiff.diffChars(samples[0], samples[1]);\n                break;\n            case \"Word\":\n                if (ignoreWhitespace) {\n                    diff = jsdiff.diffWords(samples[0], samples[1]);\n                } else {\n                    diff = jsdiff.diffWordsWithSpace(samples[0], samples[1]);\n                }\n                break;\n            case \"Line\":\n                if (ignoreWhitespace) {\n                    diff = jsdiff.diffTrimmedLines(samples[0], samples[1]);\n                } else {\n                    diff = jsdiff.diffLines(samples[0], samples[1]);","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/Diff.mjs#L68-L104","documentation":"Thrown by Diff run() when input.split(sampleDelim) does not yield exactly two samples. The operation compares two inputs; the default sample delimiter is '\\n\\n' (a literal backslash-n-backslash-n rendered from the binaryString default). If the input does not contain the delimiter, split returns a 1-element array; if it contains it more than once, the array is longer than 2.","triggerScenarios":"Input with zero occurrences of the delimiter (returns ['whole input'], length 1); input with 2+ occurrences (length 3+); delimiter set to a value that does not actually appear between the two samples (e.g. samples separated by a single newline while delimiter expects two).","commonSituations":"Pasting two text blocks separated by a single blank line when the delimiter expects '\\n\\n' rendered as actual newlines vs the literal characters; delimiter string not matching the input's separator; providing three or more samples; empty input.","solutions":["Ensure the input contains exactly one occurrence of the sample delimiter separating the two samples.","Adjust the Sample delimiter argument to match the actual separator in your input (e.g. a single newline '\\n', a comma, a tab).","If the delimiter is a control sequence, confirm whether the binaryString field interprets it literally or as an escape (the default '\\n\\n' is an escape).","Remove extra occurrences of the separator so only two samples result."],"exampleFix":"// before - samples joined by one newline, delimiter expects two\ninput: \"lineA\\nlineB\"\ndelimiter: \"\\n\\n\"  // split -> 1 sample -> error\n\n// after\ndelimiter: \"\\n\"      // split -> 2 samples","handlingStrategy":"validation","validationCode":"function hasExactlyTwoSamples(input, sampleDelim) {\n    return input.split(sampleDelim).length === 2;\n}","typeGuard":"/** @returns {boolean} */\nfunction hasTwoSamples(input, sampleDelim) {\n    return typeof input === \"string\"\n        && typeof sampleDelim === \"string\"\n        && sampleDelim.length > 0\n        && input.split(sampleDelim).length === 2;\n}","tryCatchPattern":"try {\n    out = diff.run(input, args);\n} catch (e) {\n    if (e instanceof OperationError && /Incorrect number of samples/.test(e.message)) {\n        // adjust sampleDelim to match the actual separator in input\n    } else throw e;\n}","preventionTips":["Ensure the input contains exactly one occurrence of the sample delimiter.","Match the Sample delimiter argument to the actual separator in the input.","Confirm how the binaryString field interprets escape sequences (\\n as newline vs literal).","Remove extra occurrences of the separator before running Diff."],"tags":["text","diff","validation","delimiter"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}