@ekzo-dev/bootstrap-addons 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -0
- package/package.json +59 -0
- package/src/forms/json-input/index.ts +1 -0
- package/src/forms/json-input/json-input.html +12 -0
- package/src/forms/json-input/json-input.scss +13 -0
- package/src/forms/json-input/json-input.stories.ts +59 -0
- package/src/forms/json-input/json-input.ts +178 -0
- package/src/index.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# `ui-utils`
|
|
2
|
+
|
|
3
|
+
This project is bootstrapped by [aurelia-cli](https://github.com/aurelia/cli).
|
|
4
|
+
|
|
5
|
+
This Aurelia plugin project has a built-in dev app (with CLI built-in bundler and RequireJS) to simplify development.
|
|
6
|
+
|
|
7
|
+
1. The local `src/` folder, is the source code for the plugin.
|
|
8
|
+
2. The local `dev-app/` folder, is the code for the dev app, just like a normal app bootstrapped by aurelia-cli.
|
|
9
|
+
3. You can use normal `au run` and `au test` in development just like developing an app.
|
|
10
|
+
4. You can use aurelia-testing to test your plugin, just like developing an app.
|
|
11
|
+
5. To ensure compatibility to other apps, always use `PLATFORM.moduleName()` wrapper in files inside `src/`. You don't need to use the wrapper in `dev-app/` folder as CLI built-in bundler supports module name without the wrapper.
|
|
12
|
+
|
|
13
|
+
Note aurelia-cli doesn't provide a plugin skeleton with Webpack setup (not yet), but this plugin can be consumed by any app using Webpack, or CLI built-in bundler, or jspm.
|
|
14
|
+
|
|
15
|
+
## How to write an Aurelia plugin
|
|
16
|
+
|
|
17
|
+
For a full length tutorial, visit [Aurelia plugin guide](https://aurelia.io/docs/plugins/write-new-plugin).
|
|
18
|
+
|
|
19
|
+
Here is some basics. You can create new custom element, custom attribute, value converter or binding behavior manually, or use command `au generate` to help.
|
|
20
|
+
|
|
21
|
+
```shell
|
|
22
|
+
au generate element some-name
|
|
23
|
+
au generate attribute some-name
|
|
24
|
+
au generate value-converter some-name
|
|
25
|
+
au generate binding-behavior some-name
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
By default, the cli generates command generates files in following folders:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
src/elements
|
|
32
|
+
src/attributes
|
|
33
|
+
src/value-converters
|
|
34
|
+
src/binding-behaviors
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Note the folder structure is only to help you organising the files, it's not a requirement of Aurelia. You can manually create new element (or other thing) anywhere in `src/`.
|
|
38
|
+
|
|
39
|
+
After you added some new file, you need to register it in `src/index.ts`. Like this:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
config.globalResources([
|
|
43
|
+
// ...
|
|
44
|
+
PLATFORM.moduleName('./path/to/new-file-without-ext'),
|
|
45
|
+
]);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The usage of `PLATFORM.moduleName` wrapper is mandatory. It's needed for your plugin to be consumed by any app using webpack, CLI built-in bundler, or jspm.
|
|
49
|
+
|
|
50
|
+
## Resource import within the dev app
|
|
51
|
+
|
|
52
|
+
In dev app, when you need to import something from the inner plugin (for example, importing a class for dependency injection), use special name `"resources"` to reference the inner plugin.
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import {autoinject} from 'aurelia-framework';
|
|
56
|
+
// "resources" refers the inner plugin src/index.ts
|
|
57
|
+
import {MyService} from 'resources';
|
|
58
|
+
|
|
59
|
+
@autoinject()
|
|
60
|
+
export class App {
|
|
61
|
+
constructor(myService: MyService) {}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Manage dependencies
|
|
66
|
+
|
|
67
|
+
By default, this plugin has no "dependencies" in package.json. Theoretically this plugin depends on at least `aurelia-pal` because `src/index.ts` imports it. It could also depends on more core Aurelia package like `aurelia-binding` or `aurelia-templating` if you build advanced components that reference them.
|
|
68
|
+
|
|
69
|
+
Ideally you need to carefully add those `aurelia-pal` (`aurelia-binding`...) to "dependencies" in package.json. But in practice you don't have to. Because every app that consumes this plugin will have full Aurelia core packages installed.
|
|
70
|
+
|
|
71
|
+
Furthermore, there are two benefits by leaving those dependencies out of plugin's package.json.
|
|
72
|
+
|
|
73
|
+
1. ensure this plugin doesn't bring in a duplicated Aurelia core package to consumers' app. This is mainly for app built with webpack. We had been hit with `aurelia-binding` v1 and v2 conflicts due to 3rd party plugin asks for `aurelia-binding` v1.
|
|
74
|
+
2. reduce the burden for npm/yarn when installing this plugin.
|
|
75
|
+
|
|
76
|
+
If you are a perfectionist who could not stand leaving out dependencies, I recommend you to add `aurelia-pal` (`aurelia-binding`...) to "peerDependencies" in package.json. So at least it could not cause a duplicated Aurelia core package.
|
|
77
|
+
|
|
78
|
+
If your plugin depends on other npm package, like `lodash` or `jquery`, **you have to add them to "dependencies" in package.json**.
|
|
79
|
+
|
|
80
|
+
## Build Plugin
|
|
81
|
+
|
|
82
|
+
Run `au build-plugin`. This will transpile all files from `src/` folder to `dist/native-modules/` and `dist/commonjs/`.
|
|
83
|
+
|
|
84
|
+
For example, `src/index.ts` will become `dist/native-modules/index.js` and `dist/commonjs/index.js`.
|
|
85
|
+
|
|
86
|
+
Note all other files in `dev-app/` folder are for the dev app, they would not appear in the published npm package.
|
|
87
|
+
|
|
88
|
+
## Consume Plugin
|
|
89
|
+
|
|
90
|
+
By default, the `dist/` folder is not committed to git. (We have `/dist` in `.gitignore`). But that would not prevent you from consuming this plugin through direct git reference.
|
|
91
|
+
|
|
92
|
+
You can consume this plugin directly by:
|
|
93
|
+
|
|
94
|
+
```shell
|
|
95
|
+
npm i github:your_github_username/ui-utils
|
|
96
|
+
# or if you use bitbucket
|
|
97
|
+
npm i bitbucket:your_github_username/ui-utils
|
|
98
|
+
# or if you use gitlab
|
|
99
|
+
npm i gitlab:your_github_username/ui-utils
|
|
100
|
+
# or plain url
|
|
101
|
+
npm i https:/github.com/your_github_username/ui-utils.git
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Then load the plugin in app's `main.ts` like this.
|
|
105
|
+
|
|
106
|
+
```js
|
|
107
|
+
aurelia.use.plugin('ui-utils');
|
|
108
|
+
// for webpack user, use PLATFORM.moduleName wrapper
|
|
109
|
+
aurelia.use.plugin(PLATFORM.moduleName('ui-utils'));
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The missing `dist/` files will be filled up by npm through `"prepare": "npm run build"` (in `"scripts"` section of package.json).
|
|
113
|
+
|
|
114
|
+
Yarn has a [bug](https://github.com/yarnpkg/yarn/issues/5235) that ignores `"prepare"` script. If you want to use yarn to consume your plugin through direct git reference, remove `/dist` from `.gitignore` and commit all the files. Note you don't need to commit `dist/` files if you only use yarn to consume this plugin through published npm package (`npm i ui-utils`).
|
|
115
|
+
|
|
116
|
+
## Publish npm package
|
|
117
|
+
|
|
118
|
+
By default, `"private"` field in package.json has been turned on, this prevents you from accidentally publish a private plugin to npm.
|
|
119
|
+
|
|
120
|
+
To publish the plugin to npm for public consumption:
|
|
121
|
+
|
|
122
|
+
1. Remove `"private": true,` from package.json.
|
|
123
|
+
2. Pump up project version. This will run through `au test` (in "preversion" in package.json) first.
|
|
124
|
+
|
|
125
|
+
```shell
|
|
126
|
+
npm version patch # or minor or major
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
3. Push up changes to your git server
|
|
130
|
+
|
|
131
|
+
```shell
|
|
132
|
+
git push && git push --tags
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
4. Then publish to npm, you need to have your npm account logged in.
|
|
136
|
+
|
|
137
|
+
```shell
|
|
138
|
+
npm publish
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Automate changelog, git push, and npm publish
|
|
142
|
+
|
|
143
|
+
You can enable `npm version patch # or minor or major` to automatically update changelog, push commits and version tag to the git server, and publish to npm.
|
|
144
|
+
|
|
145
|
+
Here is one simple setup.
|
|
146
|
+
|
|
147
|
+
1. `npm i -D standard-changelog`. We use [`standard-changelog`](https://github.com/conventional-changelog/conventional-changelog) as a minimum example to support conventional changelog.
|
|
148
|
+
|
|
149
|
+
- Alternatively you can use high level [standard-version](https://github.com/conventional-changelog/standard-version).
|
|
150
|
+
|
|
151
|
+
2. Add two commands to `"scripts"` section of package.json.
|
|
152
|
+
|
|
153
|
+
```
|
|
154
|
+
"scripts": {
|
|
155
|
+
// ...
|
|
156
|
+
"version": "standard-changelog && git add CHANGELOG.md",
|
|
157
|
+
"postversion": "git push && git push --tags && npm publish"
|
|
158
|
+
},
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
3. you can remove `&& npm publish` if your project is private
|
|
162
|
+
|
|
163
|
+
For more information, go to https://aurelia.io/docs/cli/cli-bundler
|
|
164
|
+
|
|
165
|
+
## Run dev app
|
|
166
|
+
|
|
167
|
+
Run `au run`, then open `http://localhost:9000`
|
|
168
|
+
|
|
169
|
+
To open browser automatically, do `au run --open`.
|
|
170
|
+
|
|
171
|
+
To change dev server port, do `au run --port 8888`.
|
|
172
|
+
|
|
173
|
+
To change dev server host, do `au run --host 127.0.0.1`
|
|
174
|
+
|
|
175
|
+
**PS:** You could mix all the flags as well, `au run --host 127.0.0.1 --port 7070 --open`
|
|
176
|
+
|
|
177
|
+
## Unit tests
|
|
178
|
+
|
|
179
|
+
Run `au test` (or `au jest`).
|
|
180
|
+
|
|
181
|
+
To run in watch mode, `au test --watch` or `au jest --watch`.
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ekzo-dev/bootstrap-addons",
|
|
3
|
+
"description": "Aurelia Bootstrap additional component",
|
|
4
|
+
"version": "5.2.0",
|
|
5
|
+
"homepage": "https://github.com/ekzo-dev/aurelia-components/tree/main/packages/bootstrap-addons",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/ekzo-dev/aurelia-components.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@ekzo-dev/bootstrap": "^5.2.11",
|
|
13
|
+
"@ekzo-dev/vanilla-jsoneditor": "^0.23.6",
|
|
14
|
+
"@ekzo-dev/toolkit": "^1.2.4",
|
|
15
|
+
"@fortawesome/free-solid-svg-icons": "^6.5.2",
|
|
16
|
+
"@types/json-schema": "^7.0.14",
|
|
17
|
+
"ajv": "^8.17.1",
|
|
18
|
+
"ajv-formats": "^3.0.1",
|
|
19
|
+
"vanilla-jsoneditor": "~0.23.0",
|
|
20
|
+
"immutable-json-patch": "^5.1.3"
|
|
21
|
+
},
|
|
22
|
+
"main": "src/index.ts",
|
|
23
|
+
"files": [
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"lint:js": "eslint src --ext .js,.ts",
|
|
28
|
+
"lint:css": "stylelint \"**/*.*css\" --allow-empty-input",
|
|
29
|
+
"lint:html": "prettier \"**/*.html\" --no-error-on-unmatched-pattern",
|
|
30
|
+
"lint:all": "npm run lint:js && npm run lint:html && npm run lint:css",
|
|
31
|
+
"start": "webpack serve",
|
|
32
|
+
"build": "rimraf dist && webpack --env production",
|
|
33
|
+
"analyze": "rimraf dist && webpack --env production --analyze",
|
|
34
|
+
"test": "jest"
|
|
35
|
+
},
|
|
36
|
+
"jest": {
|
|
37
|
+
"testMatch": [
|
|
38
|
+
"<rootDir>/test/**/*.spec.ts"
|
|
39
|
+
],
|
|
40
|
+
"testEnvironment": "jsdom",
|
|
41
|
+
"transform": {
|
|
42
|
+
"\\.(css|less|sass|scss|styl|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "jest-transform-stub",
|
|
43
|
+
"\\.(ts|html)$": "@aurelia/ts-jest"
|
|
44
|
+
},
|
|
45
|
+
"collectCoverage": true,
|
|
46
|
+
"collectCoverageFrom": [
|
|
47
|
+
"src/**/*.ts",
|
|
48
|
+
"!src/**/*.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"globals": {
|
|
51
|
+
"ts-jest": {
|
|
52
|
+
"isolatedModules": true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './json-input';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<input ref="input" required.bind="inputRequired" />
|
|
3
|
+
<json-editor
|
|
4
|
+
ref="editor"
|
|
5
|
+
json.bind="value"
|
|
6
|
+
validator.bind="validator"
|
|
7
|
+
read-only.bind="disabled"
|
|
8
|
+
on-render-value.one-time="onRenderValue"
|
|
9
|
+
on-render-menu.one-time="onRenderMenu"
|
|
10
|
+
...$bindables="jsonEditorOptions"
|
|
11
|
+
></json-editor>
|
|
12
|
+
</template>
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Meta, Story, StoryFnAureliaReturnType } from '@storybook/aurelia';
|
|
2
|
+
|
|
3
|
+
import { BsJsonInput } from '.';
|
|
4
|
+
|
|
5
|
+
const meta: Meta = {
|
|
6
|
+
title: 'Ekzo / Bootstrap Addons / Forms / Json input',
|
|
7
|
+
component: BsJsonInput,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default meta;
|
|
11
|
+
|
|
12
|
+
const Overview: Story = (args): StoryFnAureliaReturnType => ({
|
|
13
|
+
props: args,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
Overview.args = {
|
|
17
|
+
jsonSchema: {
|
|
18
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: {
|
|
21
|
+
enum: {
|
|
22
|
+
type: 'string',
|
|
23
|
+
enum: ['1', '2', '3'],
|
|
24
|
+
},
|
|
25
|
+
boolean: {
|
|
26
|
+
type: 'boolean',
|
|
27
|
+
},
|
|
28
|
+
email: {
|
|
29
|
+
type: 'string',
|
|
30
|
+
format: 'email',
|
|
31
|
+
},
|
|
32
|
+
number: {
|
|
33
|
+
type: 'number',
|
|
34
|
+
multipleOf: 0.0001,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
required: ['enum'],
|
|
38
|
+
},
|
|
39
|
+
value: {
|
|
40
|
+
number: 1.11,
|
|
41
|
+
enum: '1',
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const JsonSchemaEditor: Story = (args): StoryFnAureliaReturnType => ({
|
|
46
|
+
props: args,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
JsonSchemaEditor.args = {
|
|
50
|
+
jsonSchema: true,
|
|
51
|
+
value: {
|
|
52
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
53
|
+
type: 'object',
|
|
54
|
+
properties: {},
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// eslint-disable-next-line
|
|
59
|
+
export { Overview, JsonSchemaEditor };
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import template from './json-input.html';
|
|
2
|
+
|
|
3
|
+
import './json-input.scss';
|
|
4
|
+
|
|
5
|
+
import { coerceBoolean } from '@ekzo-dev/toolkit';
|
|
6
|
+
import { JsonEditor } from '@ekzo-dev/vanilla-jsoneditor';
|
|
7
|
+
import { faUpRightAndDownLeftFromCenter } from '@fortawesome/free-solid-svg-icons/faUpRightAndDownLeftFromCenter';
|
|
8
|
+
import Ajv, { ErrorObject, Options } from 'ajv';
|
|
9
|
+
import Ajv2019 from 'ajv/dist/2019';
|
|
10
|
+
import Ajv2020 from 'ajv/dist/2020';
|
|
11
|
+
import addFormats from 'ajv-formats';
|
|
12
|
+
import { bindable, customElement } from 'aurelia';
|
|
13
|
+
import { JSONValue, parsePath } from 'immutable-json-patch';
|
|
14
|
+
import {
|
|
15
|
+
JSONEditorPropsOptional,
|
|
16
|
+
JSONSchema,
|
|
17
|
+
JSONSchemaDefinitions,
|
|
18
|
+
MenuItem,
|
|
19
|
+
RenderValueComponentDescription,
|
|
20
|
+
RenderValueProps,
|
|
21
|
+
ValidationError,
|
|
22
|
+
ValidationSeverity,
|
|
23
|
+
Validator,
|
|
24
|
+
} from 'vanilla-jsoneditor';
|
|
25
|
+
|
|
26
|
+
@customElement({
|
|
27
|
+
name: 'bs-json-input',
|
|
28
|
+
template,
|
|
29
|
+
dependencies: [JsonEditor],
|
|
30
|
+
})
|
|
31
|
+
export class BsJsonInput {
|
|
32
|
+
@bindable()
|
|
33
|
+
value: unknown;
|
|
34
|
+
|
|
35
|
+
@bindable(coerceBoolean)
|
|
36
|
+
required: boolean = false;
|
|
37
|
+
|
|
38
|
+
@bindable(coerceBoolean)
|
|
39
|
+
disabled: boolean = false;
|
|
40
|
+
|
|
41
|
+
@bindable()
|
|
42
|
+
jsonSchema?: JSONSchema | boolean;
|
|
43
|
+
|
|
44
|
+
@bindable()
|
|
45
|
+
ajvOptions: Options = {};
|
|
46
|
+
|
|
47
|
+
@bindable()
|
|
48
|
+
jsonEditorOptions: JSONEditorPropsOptional = {};
|
|
49
|
+
|
|
50
|
+
editorModule?: typeof import('vanilla-jsoneditor');
|
|
51
|
+
|
|
52
|
+
schemaVersion?: string;
|
|
53
|
+
|
|
54
|
+
readonly input!: HTMLInputElement;
|
|
55
|
+
|
|
56
|
+
readonly editor!: HTMLElement;
|
|
57
|
+
|
|
58
|
+
async binding() {
|
|
59
|
+
this.schemaVersion = this.#getSchemaVersion(this.value);
|
|
60
|
+
this.editorModule = await import('vanilla-jsoneditor');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
valueChanged(value: unknown) {
|
|
64
|
+
this.schemaVersion = this.jsonSchema === true ? this.#getSchemaVersion(value) : undefined;
|
|
65
|
+
|
|
66
|
+
if (value == null || value === '') {
|
|
67
|
+
this.input.setCustomValidity('');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onRenderValue = (props: RenderValueProps): RenderValueComponentDescription[] => {
|
|
72
|
+
let result: RenderValueComponentDescription[] | null;
|
|
73
|
+
const { jsonSchema } = this;
|
|
74
|
+
|
|
75
|
+
if (jsonSchema && typeof jsonSchema === 'object') {
|
|
76
|
+
result = this.editorModule.renderJSONSchemaEnum(props, jsonSchema, this.#getSchemaDefinitions(jsonSchema));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return result ?? this.editorModule.renderValue(props);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
onRenderMenu = (items: MenuItem[]): MenuItem[] | undefined => {
|
|
83
|
+
return [
|
|
84
|
+
...items,
|
|
85
|
+
{
|
|
86
|
+
type: 'space',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
type: 'button',
|
|
90
|
+
onClick: () => {
|
|
91
|
+
if (document.fullscreenElement) {
|
|
92
|
+
void document.exitFullscreen();
|
|
93
|
+
} else {
|
|
94
|
+
void this.editor.requestFullscreen();
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
icon: faUpRightAndDownLeftFromCenter,
|
|
98
|
+
title: 'Toggle full screen',
|
|
99
|
+
},
|
|
100
|
+
];
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
get inputRequired(): boolean {
|
|
104
|
+
return this.required && this.value != null && this.value !== '';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
get validator(): Validator | undefined {
|
|
108
|
+
const { jsonSchema, schemaVersion, disabled } = this;
|
|
109
|
+
const rawThis = this['__raw__'] as BsJsonInput;
|
|
110
|
+
|
|
111
|
+
if (jsonSchema && typeof jsonSchema === 'object' && !disabled) {
|
|
112
|
+
const ajv = rawThis.#initAjv(jsonSchema.$schema as string);
|
|
113
|
+
|
|
114
|
+
addFormats(ajv);
|
|
115
|
+
const validate = ajv.compile(jsonSchema);
|
|
116
|
+
|
|
117
|
+
if (validate.errors) {
|
|
118
|
+
throw validate.errors[0];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return (json: unknown): ValidationError[] => {
|
|
122
|
+
validate(json);
|
|
123
|
+
|
|
124
|
+
return rawThis.#processErrors(validate.errors, json);
|
|
125
|
+
};
|
|
126
|
+
} else if (schemaVersion != null && !disabled) {
|
|
127
|
+
const ajv = rawThis.#initAjv(schemaVersion);
|
|
128
|
+
|
|
129
|
+
return (json: unknown): ValidationError[] => {
|
|
130
|
+
void ajv.validateSchema(json);
|
|
131
|
+
|
|
132
|
+
return rawThis.#processErrors(ajv.errors, json);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
#initAjv($schema: string): Ajv {
|
|
138
|
+
const options: Options = {
|
|
139
|
+
strict: false,
|
|
140
|
+
multipleOfPrecision: 2,
|
|
141
|
+
...this.ajvOptions,
|
|
142
|
+
};
|
|
143
|
+
let ajv: Ajv;
|
|
144
|
+
|
|
145
|
+
switch ($schema) {
|
|
146
|
+
case 'https://json-schema.org/draft/2019-09/schema':
|
|
147
|
+
ajv = new Ajv2019(options);
|
|
148
|
+
break;
|
|
149
|
+
|
|
150
|
+
case 'https://json-schema.org/draft/2020-12/schema':
|
|
151
|
+
ajv = new Ajv2020(options);
|
|
152
|
+
break;
|
|
153
|
+
|
|
154
|
+
default:
|
|
155
|
+
ajv = new Ajv(options);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return ajv;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
#getSchemaDefinitions(schema: JSONSchema): JSONSchemaDefinitions {
|
|
162
|
+
return (schema.$defs ?? schema.definitions) as JSONSchemaDefinitions;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#getSchemaVersion(value: unknown): string {
|
|
166
|
+
return value === Object(value) && value['$schema'] ? (value['$schema'] as string) : '';
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#processErrors(errors: ErrorObject[] | null, json: unknown): ValidationError[] {
|
|
170
|
+
this.input.setCustomValidity(errors?.length ? "JSON doesn't match schema" : '');
|
|
171
|
+
|
|
172
|
+
return (errors || []).map((error) => ({
|
|
173
|
+
path: parsePath(json as JSONValue, error.instancePath),
|
|
174
|
+
message: error.message || 'Unknown error',
|
|
175
|
+
severity: 'warning' as ValidationSeverity,
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './forms/json-input';
|