@kokimoki/app 0.4.3 → 0.5.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/dist/fields.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export interface FieldOptions {
2
2
  label?: string;
3
- optional: boolean;
4
3
  }
5
4
  export declare abstract class Field<T> {
6
5
  readonly options: FieldOptions;
@@ -8,6 +7,14 @@ export declare abstract class Field<T> {
8
7
  abstract get value(): T;
9
8
  abstract get schema(): any;
10
9
  }
10
+ export declare class BooleanField extends Field<boolean> {
11
+ value: boolean;
12
+ constructor(value: boolean, options?: FieldOptions);
13
+ get schema(): {
14
+ type: string;
15
+ default: boolean;
16
+ };
17
+ }
11
18
  export declare class ConstField<T extends string> extends Field<string extends T ? never : T> {
12
19
  value: string extends T ? never : T;
13
20
  constructor(value: string extends T ? never : T, options?: FieldOptions);
@@ -47,17 +54,23 @@ export declare class IntegerField extends Field<number> {
47
54
  default: number;
48
55
  };
49
56
  }
50
- export declare class FormGroup<T extends Record<string, Field<any>>> extends Field<{
51
- [key in keyof T]: Field<T>["value"];
52
- }> {
53
- fields: T;
54
- constructor(fields: T, options?: FieldOptions);
57
+ export declare class FormGroup<T extends Record<string, Field<any>>, O extends Record<string, Field<any>>> extends Field<{
58
+ [key in keyof T]: T[key]["value"];
59
+ } & Partial<{
60
+ [key in keyof O]: O[key]["value"];
61
+ }>> {
62
+ requiredFields: T;
63
+ optionalFields: O;
64
+ constructor(requiredFields: T, optionalFields?: O, options?: FieldOptions);
55
65
  get value(): {
56
66
  [key in keyof T]: T[key]["value"];
57
- };
67
+ } & Partial<{
68
+ [key in keyof O]: O[key]["value"];
69
+ }>;
58
70
  get schema(): {
59
71
  type: string;
60
72
  properties: any;
73
+ required: string[];
61
74
  };
62
75
  }
63
76
  export declare class FormArray<T> extends Field<T[]> {
@@ -70,5 +83,5 @@ export declare class FormArray<T> extends Field<T[]> {
70
83
  default: T[];
71
84
  };
72
85
  }
73
- export declare class Form<T extends Record<string, Field<any>>> extends FormGroup<T> {
86
+ export declare class Form<R extends Record<string, Field<any>>, O extends Record<string, Field<any>>> extends FormGroup<R, O> {
74
87
  }
package/dist/fields.js CHANGED
@@ -1,12 +1,23 @@
1
- const defaultFieldOptions = {
2
- optional: false,
3
- };
1
+ const defaultFieldOptions = {};
4
2
  export class Field {
5
3
  options;
6
4
  constructor(options) {
7
5
  this.options = options;
8
6
  }
9
7
  }
8
+ export class BooleanField extends Field {
9
+ value;
10
+ constructor(value, options = defaultFieldOptions) {
11
+ super(options);
12
+ this.value = value;
13
+ }
14
+ get schema() {
15
+ return {
16
+ type: "boolean",
17
+ default: this.value,
18
+ };
19
+ }
20
+ }
10
21
  export class ConstField extends Field {
11
22
  value;
12
23
  constructor(value, options = defaultFieldOptions) {
@@ -73,24 +84,37 @@ export class IntegerField extends Field {
73
84
  }
74
85
  }
75
86
  export class FormGroup extends Field {
76
- fields;
77
- constructor(fields, options = defaultFieldOptions) {
87
+ requiredFields;
88
+ optionalFields;
89
+ constructor(requiredFields, optionalFields = {}, options = defaultFieldOptions) {
78
90
  super(options);
79
- this.fields = fields;
91
+ this.requiredFields = requiredFields;
92
+ this.optionalFields = optionalFields;
80
93
  }
81
94
  get value() {
82
- return Object.entries(this.fields).reduce((acc, [key, field]) => {
95
+ const value = Object.entries(this.requiredFields).reduce((acc, [key, field]) => {
83
96
  acc[key] = field.value;
84
97
  return acc;
85
98
  }, {});
99
+ Object.entries(this.optionalFields).forEach(([key, field]) => {
100
+ if (field.value !== undefined) {
101
+ value[key] = field.value;
102
+ }
103
+ });
104
+ return value;
86
105
  }
87
106
  get schema() {
107
+ const properties = {};
108
+ Object.entries(this.requiredFields).forEach(([key, field]) => {
109
+ properties[key] = field.schema;
110
+ });
111
+ Object.entries(this.optionalFields).forEach(([key, field]) => {
112
+ properties[key] = field.schema;
113
+ });
88
114
  return {
89
115
  type: "object",
90
- properties: Object.entries(this.fields).reduce((acc, [key, field]) => {
91
- acc[key] = field.schema;
92
- return acc;
93
- }, {}),
116
+ properties,
117
+ required: Object.keys(this.requiredFields),
94
118
  };
95
119
  }
96
120
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kokimoki/app",
3
- "version": "0.4.3",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "Kokimoki app",
6
6
  "main": "dist/index.js",