Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

26 move json spell checker #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/ci-json-spell-checker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: JSON Spell Checker CI Pipeline

on:
push:
branches: [main]
paths:
- "node/json-spell-checker/**"
pull_request:
branches: [main]
paths:
- "node/json-spell-checker/**"
workflow_dispatch:

defaults:
run:
working-directory: node/json-spell-checker

jobs:
setup-and-test:
name: Tests (${{ matrix.os }}, Node ${{ matrix.node-version }})
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest]
node-version: [18.x, 20.x]

timeout-minutes: 30

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.os }} ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"

- name: Install Dependencies
run: npm ci

- name: Test
timeout-minutes: 10
run: npm run test:coverage

- name: Upload to codecov.io
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage/lcov.info
verbose: true
64 changes: 64 additions & 0 deletions node/json-spell-checker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Eclipse Thingweb - JSON Spell Checker

This package provides spell-checking support for JSON files when a JSON Schema is given.

Limitations:

- There is limited nested spell-checking. Some of the $ref definitions can be nested infinitely. Therefore, spell-checking stops when recursive nesting is first detected.
- This tool is mainly developed for Thing Descriptions. Even though it can be used for all JSON Schemas, it may not cover all schema rules. For example 'if, else, then' rule of Open API schema is not supported yet, but this functionality could be added in the future.

## License

Licensed under the MIT license, see [License](../../LICENSE.md).

## Script-based Spell Checking

JSON spell checker is a Node.js based tool.

You can use the functionality of this package by:

- Install the package with npm `npm install @thing-description-playground/json-spell-checker` (or clone the repo and install the package with `npm install`)
- Node.js or Browser

- Node.js: Require the package and use the validation function

```javascript
const jsonSpellChecker = require("@thing-description-playground/json-spell-checker");
```

- Browser: Import the `JsonSpellChecker` as a global by adding a script tag to your HTML.

```html
<script src="./node_modules/@thing-description-playground/json-spell-checker/dist/web-bundle.min.js"></script>
```

- First, configure your spell checker using (similarityThreshold should be between 0 and 1 and try to set maxLengthDifference low if you need quick typo evaluation):

```javascript
jsonSpellChecker.configure(yourSchema, similarityThreshold, maxLengthDifference);
```

- Or just set schema and use default values for similarityThreshold (0.85) and maxLengthDifference (2)

```javascript
jsonSpellChecker.configure(yourSchema);
```

- Then call checkTypos function with your stringified JSON file to find typos

```javascript
jsonSpellChecker.checkTypos(stringifiedJsonFile);
```

### Structure

The [index.js](./src/index.js) file contains the main validation functionality and exports the module's functionalities.

## Examples

- There are also examples provided in the [examples-scripts folder](./examples/scripts/) to demonstrate the usage of this package.

## Test Usage

Test Examples are located under the examples directory.
When preparing TD examples, you must fill in the 'typoCount' field corresponding to the number of typos in the TD.
48 changes: 48 additions & 0 deletions node/json-spell-checker/examples/json/open-api-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"ope2napi": "3.1.0",
"info": {
"ti2tle": "Webhook Example",
"versiomn": "1.0.0"
},
"webhoks": {
"newPet": {
"post": {
"requestBody": {
"description": "Information about a new pet in the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "Return a 200 status to indicate that the data was received successfully"
}
}
}
}
},
"components": {
"shemas": {
"Pet": {
"required": ["id", "name"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
},
"typoCount": 5
}
83 changes: 83 additions & 0 deletions node/json-spell-checker/examples/json/td-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"id2": "urn:formOpArrayWithDefaults",
"@co3ntext": "https://www.w3.org/2022/wot/td/v1.1",
"titlve": "MyLampThing",
"descbription": "Valid TD demonstrating how to use multiple forms with different op",
"securnityDefinitions": {
"basic_sc": {
"scheme": "basic",
"in": "header"
}
},
"securimty": ["basic_sc"],
"properties": {
"brightness": {
"ddescription": "The current brightness setting",
"tyfpe": "integer",
"mingimum": -64,
"maximum": 64,
"observable": true,
"readOnly": false,
"writeOnly": false,
"forms": [
{
"hresf": "http://example.org:9191/api/brightness",
"op": ["readproperty"],
"contentType": "application/json"
},
{
"href": "http://example.org:9191/api/brightness",
"op": ["writeproperty"],
"htv:methodName": "POST",
"contentType": "application/json"
},
{
"href": "http://example.org:9191/api/brightness/observe",
"op": ["observeproperty"],
"htv:methodName": "GET",
"subprotocol": "websub",
"contentType": "application/json"
},
{
"href": "http://example.org:9191/api/brightness/unobserve",
"op": ["unobserveproperty"],
"htv:methodName": "GET",
"subprotocol": "websub",
"contentType": "application/json"
}
]
}
},
"actions": {
"toggle": {
"safe": false,
"idempotent": false,
"forms": [
{
"href": "https://mylamp.example.com/toggle",
"op": ["invokeaction"],
"contentType": "application/json"
}
]
}
},
"events": {
"overheating": {
"data": {
"type": "string",
"readOnsly": true,
"writeOnly": false,
"observable": false
},
"forms": [
{
"href": "https://mylamp.example.com/oh",
"subprotocol": "longpoll",
"op": ["subscribeevent"],
"contentType": "application/json"
}
]
}
},
"typoCount": 15
}
Loading
Loading