diff --git a/packages/core/examples/tds/invalid/typoCheckWithTypos.json b/packages/core/examples/tds/typo/typoCheckWithTypos.json similarity index 88% rename from packages/core/examples/tds/invalid/typoCheckWithTypos.json rename to packages/core/examples/tds/typo/typoCheckWithTypos.json index 2c4bcb857..18ea25854 100644 --- a/packages/core/examples/tds/invalid/typoCheckWithTypos.json +++ b/packages/core/examples/tds/typo/typoCheckWithTypos.json @@ -1,9 +1,9 @@ { "id2": "urn:typoCheck", - "x@context":"https://www.w3.org/2022/wot/td/v1.1", + "@context":"https://www.w3.org/2022/wot/td/v1.1", "title": "MyLampThing", "description":"Valid TD demonstrating how to use security in interaction level by overwriting the root level security", - "securityDefinitins": { + "securityDefinitions": { "basic_sc": { "scheme": "basic", "in": "header" @@ -15,7 +15,7 @@ "scheme": "nosec" } }, - "securityuu": ["basic_sc"], + "security": ["basic_sc"], "propeRties": { "status" : { "type": "string", @@ -42,5 +42,6 @@ "security": ["nosec_sc"] }] } - } + }, + "typoCount": 4 } \ No newline at end of file diff --git a/packages/core/examples/tds/valid/typoCheckWithoutTypos.json b/packages/core/examples/tds/typo/typoCheckWithoutTypos.json similarity index 97% rename from packages/core/examples/tds/valid/typoCheckWithoutTypos.json rename to packages/core/examples/tds/typo/typoCheckWithoutTypos.json index 83719a4e6..58d56a7e3 100644 --- a/packages/core/examples/tds/valid/typoCheckWithoutTypos.json +++ b/packages/core/examples/tds/typo/typoCheckWithoutTypos.json @@ -42,5 +42,6 @@ "security": ["nosec_sc"] }] } - } + }, + "typoCount": 0 } \ No newline at end of file diff --git a/packages/core/index.js b/packages/core/index.js index d8b3593fc..c81f4d19d 100644 --- a/packages/core/index.js +++ b/packages/core/index.js @@ -953,13 +953,12 @@ const PROPERTIES = "properties" const ADDITONAL_PROPERTIES = "additional_properties" const DATA_SCHEMA = "dataSchema" const PATH = "#/" -const JSON_SCHEMA = require('./td-schema.json') -const TYPO_LOOKUP_TABLE = createSchemaLookupTable(JSON_SCHEMA) +const TYPO_LOOKUP_TABLE = createSchemaLookupTable(tdSchema) /** * Checks possible typos in a TD * @param {object} td The TD to apply typo check on - * @returns List of possible typos + * @returns List of possible typos where the typo consists of string value of typo itself and the message, another string value, to be prompted to the user for the fix */ function checkTypos(td) { const typos = [] @@ -1061,7 +1060,7 @@ function findPathsInSchema(lookupTable, schema, path) { path = 'r' + path } - findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, schema[REF]), path) + findPathsInSchema(getRefObjectOfSchema(tdSchema, schema[REF]), path) return } @@ -1078,7 +1077,7 @@ function findPathsInSchema(lookupTable, schema, path) { path = 'r' + path } - findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, properties[key]), path) + findPathsInSchema(getRefObjectOfSchema(tdSchema, properties[key]), path) return } else { findPathsInSchema(properties[key], `${path}${key}/`) @@ -1099,7 +1098,7 @@ function findPathsInSchema(lookupTable, schema, path) { path = 'r' + path } - findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, additionalProperties[key]), `${path}*/`) + findPathsInSchema(getRefObjectOfSchema(tdSchema, additionalProperties[key]), `${path}*/`) return } } @@ -1122,7 +1121,7 @@ function findPathsInSchema(lookupTable, schema, path) { path = 'r' + path } - findPathsInSchema(getRefObjectOfSchema(JSON_SCHEMA, items[item]), path) + findPathsInSchema(getRefObjectOfSchema(tdSchema, items[item]), path) return } } @@ -1187,8 +1186,11 @@ function getRefObjectOfSchema(schema, ref) { return result } +// Minimum similarity value to be able to say that two words are similar const SIMILARITY_THRESHOLD = 0.85 -const LENGTH_DIFFERENCE_THRESHOLD = 2 + +// Maximum value of length difference between two words +const MAX_LENGTH_DIFFERENCE = 2 /** * Checks whether typo exists or not by comparing similarity of the two words @@ -1197,7 +1199,7 @@ const LENGTH_DIFFERENCE_THRESHOLD = 2 * @returns Boolean value that tell whether typo exists or not */ function doesTypoExist(actual, desired) { - if (Math.abs(actual.length - desired.length) > LENGTH_DIFFERENCE_THRESHOLD) { + if (Math.abs(actual.length - desired.length) > MAX_LENGTH_DIFFERENCE) { return false } diff --git a/packages/core/tests/examples-typo.test.js b/packages/core/tests/examples-typo.test.js new file mode 100644 index 000000000..86fb012c4 --- /dev/null +++ b/packages/core/tests/examples-typo.test.js @@ -0,0 +1,43 @@ +const fs = require("fs") +const path = require("path") +const { checkTypos } = require("../index") +const tdValidator = require("../index").tdValidator + +const rootDir = path.join("./", "examples", "tds") + +const dirPath = path.join(rootDir, "typo") +const fileNames = fs.readdirSync(dirPath) +const refResult = { + report: { + json: 'passed', + schema: 'passed', + defaults: expect.stringMatching(/warning|passed/), + jsonld: 'passed', + additional: expect.stringMatching(/warning|passed/) + }, + details: { + enumConst: expect.stringMatching(/failed|warning|passed/), + propItems: expect.stringMatching(/failed|warning|passed/), + security: expect.stringMatching(/failed|warning|passed/), + propUniqueness: expect.stringMatching(/failed|warning|passed/), + multiLangConsistency: expect.stringMatching(/failed|warning|passed/), + linksRelTypeCount: expect.stringMatching(/failed|warning|passed/), + readWriteOnly: expect.stringMatching(/failed|warning|passed/), + uriVariableSecurity: expect.stringMatching(/failed|warning|passed/) + }, + detailComments: expect.any(Object) +} +fileNames.forEach( fileName => { + test(fileName, done => { + fs.readFile(path.join(dirPath, fileName), "utf-8", (err, tdToTest) => { + if (err) {done(err)} + tdValidator(tdToTest, ()=>{},{}).then( result => { + expect(result).toEqual(refResult) + const tdJson = JSON.parse(tdToTest) + const typoCount = tdJson['typoCount'] + expect(checkTypos(tdToTest).length).toEqual(typoCount) + done() + }, errTwo => {done(errTwo)}) + }) + }) +}) diff --git a/packages/web/util.js b/packages/web/util.js index f442efb3b..b2e7aac08 100644 --- a/packages/web/util.js +++ b/packages/web/util.js @@ -321,12 +321,12 @@ export function getExamplesList(docType){ "type": "invalid" }, "TypoCheckWithoutTypos": { - "addr": "./node_modules/@thing-description-playground/core/examples/tds/valid/typoCheckWithoutTypos.json", + "addr": "./node_modules/@thing-description-playground/core/examples/tds/typo/typoCheckWithoutTypos.json", "type": "valid" }, "TypoCheckWithTypos": { - "addr": "./node_modules/@thing-description-playground/core/examples/tds/invalid/typoCheckWithTypos.json", - "type": "warning" + "addr": "./node_modules/@thing-description-playground/core/examples/tds/typo/typoCheckWithTypos.json", + "type": "valid" } } : {