@tsed/schema-formio 8.0.1 → 8.0.2
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/package.json +11 -10
- package/src/components/anyToComponent.ts +28 -0
- package/src/components/arrayToComponent.ts +43 -0
- package/src/components/booleanToComponent.ts +15 -0
- package/src/components/dateToComponent.ts +66 -0
- package/src/components/defaultToComponent.ts +17 -0
- package/src/components/enumToComponent.ts +33 -0
- package/src/components/mapToComponent.ts +17 -0
- package/src/components/nestedToComponent.ts +23 -0
- package/src/components/numberToComponent.ts +20 -0
- package/src/components/objectToEditGrid.ts +13 -0
- package/src/components/panelToComponent.ts +13 -0
- package/src/components/propertiesToComponents.ts +106 -0
- package/src/components/stringToComponent.ts +44 -0
- package/src/decorators/component.ts +34 -0
- package/src/decorators/conditional.spec.ts +73 -0
- package/src/decorators/conditional.ts +49 -0
- package/src/decorators/currency.spec.ts +40 -0
- package/src/decorators/currency.ts +19 -0
- package/src/decorators/customClass.spec.ts +35 -0
- package/src/decorators/customClass.ts +14 -0
- package/src/decorators/customConditional.spec.ts +95 -0
- package/src/decorators/customConditional.ts +85 -0
- package/src/decorators/dataSourceJson.spec.ts +88 -0
- package/src/decorators/dataSourceJson.ts +26 -0
- package/src/decorators/dataSourceUrl.spec.ts +111 -0
- package/src/decorators/dataSourceUrl.ts +19 -0
- package/src/decorators/errorLabel.spec.ts +35 -0
- package/src/decorators/errorLabel.ts +14 -0
- package/src/decorators/errorMessage.spec.ts +35 -0
- package/src/decorators/errorMessage.ts +13 -0
- package/src/decorators/form.ts +25 -0
- package/src/decorators/hidden.spec.ts +35 -0
- package/src/decorators/hidden.ts +18 -0
- package/src/decorators/inputTags.spec.ts +36 -0
- package/src/decorators/inputTags.ts +22 -0
- package/src/decorators/label.spec.ts +34 -0
- package/src/decorators/label.ts +21 -0
- package/src/decorators/mask.spec.ts +105 -0
- package/src/decorators/mask.ts +46 -0
- package/src/decorators/maxWords.spec.ts +35 -0
- package/src/decorators/maxWords.ts +13 -0
- package/src/decorators/minWords.spec.ts +35 -0
- package/src/decorators/minWords.ts +13 -0
- package/src/decorators/modalEdit.spec.ts +35 -0
- package/src/decorators/modalEdit.ts +14 -0
- package/src/decorators/multiple.spec.ts +35 -0
- package/src/decorators/multiple.ts +14 -0
- package/src/decorators/openWhenEmpty.ts +14 -0
- package/src/decorators/password.spec.ts +35 -0
- package/src/decorators/password.ts +20 -0
- package/src/decorators/placeholder.spec.ts +35 -0
- package/src/decorators/placeholder.ts +15 -0
- package/src/decorators/prefix.spec.ts +35 -0
- package/src/decorators/prefix.ts +14 -0
- package/src/decorators/select.spec.ts +82 -0
- package/src/decorators/select.ts +25 -0
- package/src/decorators/suffix.spec.ts +35 -0
- package/src/decorators/suffix.ts +14 -0
- package/src/decorators/tableView.spec.ts +35 -0
- package/src/decorators/tableView.ts +15 -0
- package/src/decorators/tabs.ts +18 -0
- package/src/decorators/textCase.spec.ts +35 -0
- package/src/decorators/textCase.ts +15 -0
- package/src/decorators/textarea.spec.ts +35 -0
- package/src/decorators/textarea.ts +21 -0
- package/src/decorators/tooltip.spec.ts +35 -0
- package/src/decorators/tooltip.ts +15 -0
- package/src/decorators/validate.ts +18 -0
- package/src/domain/FormioDataResolver.ts +5 -0
- package/src/index.ts +52 -0
- package/src/registries/FormioMappersContainer.ts +21 -0
- package/src/registries/FormsContainer.ts +4 -0
- package/src/utils/getFormioProps.ts +12 -0
- package/src/utils/getFormioSchema.spec.ts +84 -0
- package/src/utils/getFormioSchema.ts +62 -0
- package/src/utils/getRef.ts +8 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {Enum} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
4
|
+
import {Select} from "./select.js";
|
|
5
|
+
|
|
6
|
+
describe("Select", () => {
|
|
7
|
+
it("should declare a model with select field", async () => {
|
|
8
|
+
class Model {
|
|
9
|
+
@Select()
|
|
10
|
+
test: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
14
|
+
components: [
|
|
15
|
+
{
|
|
16
|
+
disabled: false,
|
|
17
|
+
input: true,
|
|
18
|
+
key: "test",
|
|
19
|
+
label: "Test",
|
|
20
|
+
selectThreshold: 0.3,
|
|
21
|
+
type: "select",
|
|
22
|
+
validate: {
|
|
23
|
+
required: false
|
|
24
|
+
},
|
|
25
|
+
widget: "choicesjs"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
display: "form",
|
|
29
|
+
machineName: "model",
|
|
30
|
+
name: "model",
|
|
31
|
+
title: "Model",
|
|
32
|
+
type: "form",
|
|
33
|
+
submissionAccess: [],
|
|
34
|
+
access: [],
|
|
35
|
+
tags: []
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
it("should with enum", async () => {
|
|
39
|
+
enum MyEnum {
|
|
40
|
+
TEST1 = "TEST1",
|
|
41
|
+
TEST2 = "TEST2"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class Model {
|
|
45
|
+
@Select()
|
|
46
|
+
@Enum(MyEnum)
|
|
47
|
+
test: MyEnum;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
51
|
+
access: [],
|
|
52
|
+
components: [
|
|
53
|
+
{
|
|
54
|
+
data: {
|
|
55
|
+
json: '[{"label":"TEST1","value":"TEST1"},{"label":"TEST2","value":"TEST2"}]'
|
|
56
|
+
},
|
|
57
|
+
dataSrc: "json",
|
|
58
|
+
disabled: false,
|
|
59
|
+
idPath: "value",
|
|
60
|
+
input: true,
|
|
61
|
+
key: "test",
|
|
62
|
+
label: "Test",
|
|
63
|
+
selectThreshold: 0.3,
|
|
64
|
+
template: "<span>{{ item.label }}</span>",
|
|
65
|
+
type: "select",
|
|
66
|
+
validate: {
|
|
67
|
+
required: false
|
|
68
|
+
},
|
|
69
|
+
valueProperty: "value",
|
|
70
|
+
widget: "choicesjs"
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
display: "form",
|
|
74
|
+
machineName: "model",
|
|
75
|
+
name: "model",
|
|
76
|
+
submissionAccess: [],
|
|
77
|
+
tags: [],
|
|
78
|
+
title: "Model",
|
|
79
|
+
type: "form"
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
export interface SelectOpts extends Record<string, any> {
|
|
4
|
+
idPath?: string;
|
|
5
|
+
valueProperty?: string;
|
|
6
|
+
template?: string;
|
|
7
|
+
multiple?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Configure the property as Select component.
|
|
12
|
+
* @decorator
|
|
13
|
+
* @formio
|
|
14
|
+
* @property
|
|
15
|
+
* @schema
|
|
16
|
+
*/
|
|
17
|
+
export function Select(props: SelectOpts = {}): PropertyDecorator {
|
|
18
|
+
return Component({
|
|
19
|
+
selectThreshold: 0.3,
|
|
20
|
+
input: true,
|
|
21
|
+
widget: "choicesjs",
|
|
22
|
+
...props,
|
|
23
|
+
type: "select"
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
2
|
+
import {Suffix} from "./suffix.js";
|
|
3
|
+
|
|
4
|
+
describe("@Suffix", () => {
|
|
5
|
+
it("should add a suffix on field", async () => {
|
|
6
|
+
class Model {
|
|
7
|
+
@Suffix("The suffix")
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
12
|
+
access: [],
|
|
13
|
+
components: [
|
|
14
|
+
{
|
|
15
|
+
disabled: false,
|
|
16
|
+
input: true,
|
|
17
|
+
key: "test",
|
|
18
|
+
label: "Test",
|
|
19
|
+
suffix: "The suffix",
|
|
20
|
+
type: "textfield",
|
|
21
|
+
validate: {
|
|
22
|
+
required: false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
display: "form",
|
|
27
|
+
machineName: "model",
|
|
28
|
+
name: "model",
|
|
29
|
+
submissionAccess: [],
|
|
30
|
+
tags: [],
|
|
31
|
+
title: "Model",
|
|
32
|
+
type: "form"
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
2
|
+
import {TableView} from "./tableView.js";
|
|
3
|
+
|
|
4
|
+
describe("@TableView", () => {
|
|
5
|
+
it("should change the table view option", async () => {
|
|
6
|
+
class Model {
|
|
7
|
+
@TableView(false)
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
12
|
+
access: [],
|
|
13
|
+
components: [
|
|
14
|
+
{
|
|
15
|
+
disabled: false,
|
|
16
|
+
input: true,
|
|
17
|
+
key: "test",
|
|
18
|
+
label: "Test",
|
|
19
|
+
type: "textfield",
|
|
20
|
+
tableView: false,
|
|
21
|
+
validate: {
|
|
22
|
+
required: false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
display: "form",
|
|
27
|
+
machineName: "model",
|
|
28
|
+
name: "model",
|
|
29
|
+
submissionAccess: [],
|
|
30
|
+
tags: [],
|
|
31
|
+
title: "Model",
|
|
32
|
+
type: "form"
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Change the visibility of the current property on a table.
|
|
5
|
+
* @param bool
|
|
6
|
+
* @decorator
|
|
7
|
+
* @formio
|
|
8
|
+
* @property
|
|
9
|
+
* @schema
|
|
10
|
+
*/
|
|
11
|
+
export function TableView(bool: boolean): PropertyDecorator {
|
|
12
|
+
return Component({
|
|
13
|
+
tableView: bool
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {JsonEntityFn, JsonEntityStore} from "@tsed/schema";
|
|
2
|
+
import {camelCase} from "change-case";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Group the current property in tabs components.
|
|
6
|
+
* @decorator
|
|
7
|
+
* @formio
|
|
8
|
+
* @property
|
|
9
|
+
* @schema
|
|
10
|
+
*/
|
|
11
|
+
export function Tabs(label: string) {
|
|
12
|
+
return JsonEntityFn((store: JsonEntityStore) => {
|
|
13
|
+
store.schema.customKey("x-formiotabs", {
|
|
14
|
+
key: camelCase(label),
|
|
15
|
+
label
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
2
|
+
import {TextCase} from "./textCase.js";
|
|
3
|
+
|
|
4
|
+
describe("@TextCase", () => {
|
|
5
|
+
it("should change the case", async () => {
|
|
6
|
+
class Model {
|
|
7
|
+
@TextCase("lowercase")
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
12
|
+
access: [],
|
|
13
|
+
components: [
|
|
14
|
+
{
|
|
15
|
+
disabled: false,
|
|
16
|
+
input: true,
|
|
17
|
+
key: "test",
|
|
18
|
+
label: "Test",
|
|
19
|
+
case: "lowercase",
|
|
20
|
+
type: "textfield",
|
|
21
|
+
validate: {
|
|
22
|
+
required: false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
display: "form",
|
|
27
|
+
machineName: "model",
|
|
28
|
+
name: "model",
|
|
29
|
+
submissionAccess: [],
|
|
30
|
+
tags: [],
|
|
31
|
+
title: "Model",
|
|
32
|
+
type: "form"
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Force the output of this field to be sanitized in a specific format.
|
|
5
|
+
* @decorator
|
|
6
|
+
* @formio
|
|
7
|
+
* @property
|
|
8
|
+
* @schema
|
|
9
|
+
* @param textCase
|
|
10
|
+
*/
|
|
11
|
+
export function TextCase(textCase: "uppercase" | "lowercase") {
|
|
12
|
+
return Component({
|
|
13
|
+
case: textCase
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
2
|
+
import {Textarea} from "./textarea.js";
|
|
3
|
+
|
|
4
|
+
describe("Textarea", () => {
|
|
5
|
+
it("should declare a model with Textarea field", async () => {
|
|
6
|
+
class Model {
|
|
7
|
+
@Textarea()
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
12
|
+
components: [
|
|
13
|
+
{
|
|
14
|
+
autoExpand: false,
|
|
15
|
+
disabled: false,
|
|
16
|
+
input: true,
|
|
17
|
+
label: "Test",
|
|
18
|
+
key: "test",
|
|
19
|
+
type: "textarea",
|
|
20
|
+
validate: {
|
|
21
|
+
required: false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
],
|
|
25
|
+
display: "form",
|
|
26
|
+
machineName: "model",
|
|
27
|
+
name: "model",
|
|
28
|
+
title: "Model",
|
|
29
|
+
type: "form",
|
|
30
|
+
submissionAccess: [],
|
|
31
|
+
access: [],
|
|
32
|
+
tags: []
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
export interface TextareaOpts extends Record<string, any> {
|
|
4
|
+
showCharCount?: boolean;
|
|
5
|
+
showWordCount?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Configure the property as Textarea component.
|
|
10
|
+
* @decorator
|
|
11
|
+
* @formio
|
|
12
|
+
* @property
|
|
13
|
+
* @schema
|
|
14
|
+
*/
|
|
15
|
+
export function Textarea(props: TextareaOpts = {}): PropertyDecorator {
|
|
16
|
+
return Component({
|
|
17
|
+
autoExpand: false,
|
|
18
|
+
...props,
|
|
19
|
+
type: "textarea"
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
2
|
+
import {Tooltip} from "./tooltip.js";
|
|
3
|
+
|
|
4
|
+
describe("@Tooltip", () => {
|
|
5
|
+
it("should add a tooltip on field", async () => {
|
|
6
|
+
class Model {
|
|
7
|
+
@Tooltip("The tooltip")
|
|
8
|
+
test: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
expect(await getFormioSchema(Model)).toEqual({
|
|
12
|
+
access: [],
|
|
13
|
+
components: [
|
|
14
|
+
{
|
|
15
|
+
disabled: false,
|
|
16
|
+
input: true,
|
|
17
|
+
key: "test",
|
|
18
|
+
label: "Test",
|
|
19
|
+
tooltip: "The tooltip",
|
|
20
|
+
type: "textfield",
|
|
21
|
+
validate: {
|
|
22
|
+
required: false
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
],
|
|
26
|
+
display: "form",
|
|
27
|
+
machineName: "model",
|
|
28
|
+
name: "model",
|
|
29
|
+
submissionAccess: [],
|
|
30
|
+
tags: [],
|
|
31
|
+
title: "Model",
|
|
32
|
+
type: "form"
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Adds a tooltip to the side of this field.
|
|
5
|
+
* @decorator
|
|
6
|
+
* @formio
|
|
7
|
+
* @property
|
|
8
|
+
* @schema
|
|
9
|
+
* @param tooltip
|
|
10
|
+
*/
|
|
11
|
+
export function Tooltip(tooltip: string) {
|
|
12
|
+
return Component({
|
|
13
|
+
tooltip
|
|
14
|
+
});
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {Component} from "./component.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Add validation rules.
|
|
5
|
+
* @decorator
|
|
6
|
+
* @formio
|
|
7
|
+
* @property
|
|
8
|
+
* @schema
|
|
9
|
+
* @param key
|
|
10
|
+
* @param value
|
|
11
|
+
*/
|
|
12
|
+
export function Validate(key: string, value: any) {
|
|
13
|
+
return Component({
|
|
14
|
+
validate: {
|
|
15
|
+
[key]: value
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import {FormioComponent, FormioForm} from "@tsed/formio-types";
|
|
2
|
+
import {JsonSchemaOptions} from "@tsed/schema";
|
|
3
|
+
|
|
4
|
+
export type FormioDataResolverCtx = {component: FormioComponent; form: FormioForm} & JsonSchemaOptions;
|
|
5
|
+
export type FormioDataResolver = (ctx: FormioDataResolverCtx) => Promise<any> | any;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Automatically generated by @tsed/barrels.
|
|
3
|
+
*/
|
|
4
|
+
export * from "./components/anyToComponent.js";
|
|
5
|
+
export * from "./components/arrayToComponent.js";
|
|
6
|
+
export * from "./components/booleanToComponent.js";
|
|
7
|
+
export * from "./components/dateToComponent.js";
|
|
8
|
+
export * from "./components/defaultToComponent.js";
|
|
9
|
+
export * from "./components/enumToComponent.js";
|
|
10
|
+
export * from "./components/mapToComponent.js";
|
|
11
|
+
export * from "./components/nestedToComponent.js";
|
|
12
|
+
export * from "./components/numberToComponent.js";
|
|
13
|
+
export * from "./components/objectToEditGrid.js";
|
|
14
|
+
export * from "./components/panelToComponent.js";
|
|
15
|
+
export * from "./components/propertiesToComponents.js";
|
|
16
|
+
export * from "./components/stringToComponent.js";
|
|
17
|
+
export * from "./decorators/component.js";
|
|
18
|
+
export * from "./decorators/conditional.js";
|
|
19
|
+
export * from "./decorators/currency.js";
|
|
20
|
+
export * from "./decorators/customClass.js";
|
|
21
|
+
export * from "./decorators/customConditional.js";
|
|
22
|
+
export * from "./decorators/dataSourceJson.js";
|
|
23
|
+
export * from "./decorators/dataSourceUrl.js";
|
|
24
|
+
export * from "./decorators/errorLabel.js";
|
|
25
|
+
export * from "./decorators/errorMessage.js";
|
|
26
|
+
export * from "./decorators/form.js";
|
|
27
|
+
export * from "./decorators/hidden.js";
|
|
28
|
+
export * from "./decorators/inputTags.js";
|
|
29
|
+
export * from "./decorators/label.js";
|
|
30
|
+
export * from "./decorators/mask.js";
|
|
31
|
+
export * from "./decorators/maxWords.js";
|
|
32
|
+
export * from "./decorators/minWords.js";
|
|
33
|
+
export * from "./decorators/modalEdit.js";
|
|
34
|
+
export * from "./decorators/multiple.js";
|
|
35
|
+
export * from "./decorators/openWhenEmpty.js";
|
|
36
|
+
export * from "./decorators/password.js";
|
|
37
|
+
export * from "./decorators/placeholder.js";
|
|
38
|
+
export * from "./decorators/prefix.js";
|
|
39
|
+
export * from "./decorators/select.js";
|
|
40
|
+
export * from "./decorators/suffix.js";
|
|
41
|
+
export * from "./decorators/tableView.js";
|
|
42
|
+
export * from "./decorators/tabs.js";
|
|
43
|
+
export * from "./decorators/textarea.js";
|
|
44
|
+
export * from "./decorators/textCase.js";
|
|
45
|
+
export * from "./decorators/tooltip.js";
|
|
46
|
+
export * from "./decorators/validate.js";
|
|
47
|
+
export * from "./domain/FormioDataResolver.js";
|
|
48
|
+
export * from "./registries/FormioMappersContainer.js";
|
|
49
|
+
export * from "./registries/FormsContainer.js";
|
|
50
|
+
export * from "./utils/getFormioProps.js";
|
|
51
|
+
export * from "./utils/getFormioSchema.js";
|
|
52
|
+
export * from "./utils/getRef.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface FormioMapper {
|
|
2
|
+
(schema: any, options: any): any;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
const FormioMappersContainer: Map<string, FormioMapper> = new Map();
|
|
6
|
+
|
|
7
|
+
export function registerFormioMapper(type: string, mapper: FormioMapper) {
|
|
8
|
+
return FormioMappersContainer.set(type, mapper);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function getFormioMapper(type: string): FormioMapper {
|
|
12
|
+
// istanbul ignore next
|
|
13
|
+
if (!FormioMappersContainer.has(type)) {
|
|
14
|
+
throw new Error(`Formio ${type} mapper doesn't exists`);
|
|
15
|
+
}
|
|
16
|
+
return FormioMappersContainer.get(type)!;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function execMapper(type: string, schema: any, options: any): any {
|
|
20
|
+
return getFormioMapper(type)(schema, options);
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function getFormioProps(schema: any) {
|
|
2
|
+
const formioProps: any = Object.entries(schema).reduce((props, [key, value]) => {
|
|
3
|
+
if (key.startsWith("x-formio-")) {
|
|
4
|
+
return {
|
|
5
|
+
...props,
|
|
6
|
+
[key.replace("x-formio-", "")]: value
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
return props;
|
|
10
|
+
}, {});
|
|
11
|
+
return formioProps;
|
|
12
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {JsonEntityStore, Property} from "@tsed/schema";
|
|
2
|
+
|
|
3
|
+
import {Form} from "../decorators/form.js";
|
|
4
|
+
import {getFormioSchema} from "../utils/getFormioSchema.js";
|
|
5
|
+
|
|
6
|
+
const schema = {
|
|
7
|
+
access: [],
|
|
8
|
+
components: [
|
|
9
|
+
{
|
|
10
|
+
disabled: false,
|
|
11
|
+
input: true,
|
|
12
|
+
key: "test",
|
|
13
|
+
label: "Test",
|
|
14
|
+
type: "textfield",
|
|
15
|
+
validate: {
|
|
16
|
+
required: false
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
display: "form",
|
|
21
|
+
machineName: "model",
|
|
22
|
+
name: "model",
|
|
23
|
+
submissionAccess: [],
|
|
24
|
+
tags: [],
|
|
25
|
+
title: "Model",
|
|
26
|
+
type: "form"
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe("getFormioSchema", () => {
|
|
30
|
+
it("should return undefined when the schema is unknown", async () => {
|
|
31
|
+
expect(await getFormioSchema("unknown")).toEqual(undefined);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should return schema by his machineName", async () => {
|
|
35
|
+
@Form()
|
|
36
|
+
class Model {
|
|
37
|
+
@Property()
|
|
38
|
+
test: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
expect(await getFormioSchema("model")).toEqual(schema);
|
|
42
|
+
expect(await getFormioSchema("Model")).toEqual(schema);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should return schema by his custom machine name", async () => {
|
|
46
|
+
@Form({name: "CustomModel"})
|
|
47
|
+
class Model {
|
|
48
|
+
@Property()
|
|
49
|
+
test: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
expect(await getFormioSchema("custom-model")).toEqual({
|
|
53
|
+
...schema,
|
|
54
|
+
machineName: "custom-model",
|
|
55
|
+
name: "custom-model",
|
|
56
|
+
title: "CustomModel"
|
|
57
|
+
});
|
|
58
|
+
expect(await getFormioSchema("CustomModel")).toEqual({
|
|
59
|
+
...schema,
|
|
60
|
+
machineName: "custom-model",
|
|
61
|
+
name: "custom-model",
|
|
62
|
+
title: "CustomModel"
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should return schema by his type", async () => {
|
|
67
|
+
@Form()
|
|
68
|
+
class Model {
|
|
69
|
+
@Property()
|
|
70
|
+
test: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
expect(await getFormioSchema(Model)).toEqual(schema);
|
|
74
|
+
});
|
|
75
|
+
it("should return schema by his store", async () => {
|
|
76
|
+
@Form()
|
|
77
|
+
class Model {
|
|
78
|
+
@Property()
|
|
79
|
+
test: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
expect(await getFormioSchema(JsonEntityStore.from(Model))).toEqual(schema);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import "../components/anyToComponent.js";
|
|
2
|
+
import "../components/arrayToComponent.js";
|
|
3
|
+
import "../components/booleanToComponent.js";
|
|
4
|
+
import "../components/dateToComponent.js";
|
|
5
|
+
import "../components/defaultToComponent.js";
|
|
6
|
+
import "../components/enumToComponent.js";
|
|
7
|
+
import "../components/mapToComponent.js";
|
|
8
|
+
import "../components/nestedToComponent.js";
|
|
9
|
+
import "../components/numberToComponent.js";
|
|
10
|
+
import "../components/objectToEditGrid.js";
|
|
11
|
+
import "../components/panelToComponent.js";
|
|
12
|
+
import "../components/propertiesToComponents.js";
|
|
13
|
+
import "../components/stringToComponent.js";
|
|
14
|
+
|
|
15
|
+
import {isString, Type} from "@tsed/core";
|
|
16
|
+
import {FormioForm} from "@tsed/formio-types";
|
|
17
|
+
import {getJsonSchema, JsonEntityStore, JsonSchemaOptions} from "@tsed/schema";
|
|
18
|
+
import {kebabCase} from "change-case";
|
|
19
|
+
|
|
20
|
+
import {execMapper} from "../registries/FormioMappersContainer.js";
|
|
21
|
+
import {FormsContainer} from "../registries/FormsContainer.js";
|
|
22
|
+
|
|
23
|
+
export async function getFormioSchema(
|
|
24
|
+
model: string | any | undefined,
|
|
25
|
+
options: JsonSchemaOptions = {}
|
|
26
|
+
): Promise<Omit<FormioForm, "_id" | "deleted" | "owner"> | undefined> {
|
|
27
|
+
if (!model) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (isString(model)) {
|
|
32
|
+
return getFormioSchema(FormsContainer.get(model) as Type<any>, options);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const entity = JsonEntityStore.from(model);
|
|
36
|
+
const schema = getJsonSchema(entity, {
|
|
37
|
+
...options,
|
|
38
|
+
inlineEnums: true,
|
|
39
|
+
customKeys: true
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const name = entity.schema.getName();
|
|
43
|
+
const machineName = kebabCase(name);
|
|
44
|
+
const resolvers: Promise<any>[] = [];
|
|
45
|
+
const components = execMapper("properties", schema, {...options, definitions: schema.definitions, resolvers});
|
|
46
|
+
const form = {
|
|
47
|
+
title: name,
|
|
48
|
+
type: "form",
|
|
49
|
+
display: "form",
|
|
50
|
+
tags: [],
|
|
51
|
+
access: [],
|
|
52
|
+
submissionAccess: [],
|
|
53
|
+
...entity.store.get<any>("formio:form", {}),
|
|
54
|
+
name: machineName,
|
|
55
|
+
machineName,
|
|
56
|
+
components
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
await Promise.all(resolvers.map((resolver: any) => resolver(form, options)));
|
|
60
|
+
|
|
61
|
+
return form as unknown as FormioForm;
|
|
62
|
+
}
|