foorm 0.0.2-alpha.0 → 0.0.2-alpha.1

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/foorm.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StringOrFtring, TFoormEntry, TFoormValidatorFn, TFoormFn, TFtring } from './types';
1
+ import { StringOrFtring, TFoormEntry, TFoormValidatorFn, TFoormFnCtx, TFoormFn, TFtring, TFoormEntryExecutable } from './types';
2
2
  export interface TFoormSubmit<S = TFtring, B = TFtring> {
3
3
  text: string | S;
4
4
  disabled?: boolean | B;
@@ -18,63 +18,24 @@ export declare class Foorm {
18
18
  setTitle(title: string): void;
19
19
  setSubmit(submit: TFoormSubmit): void;
20
20
  transportable(): Required<TFoormOptions>;
21
- protected normalizeEntry(e: TFoormEntry): {
22
- name: string;
23
- label: string | TFtring;
24
- type: string;
25
- description?: string | TFtring | undefined;
26
- hint?: string | TFtring | undefined;
27
- placeholder?: string | TFtring | undefined;
28
- classes?: string | TFtring | Record<string, boolean | TFtring> | undefined;
29
- styles?: string | TFtring | Record<string, string | TFtring> | undefined;
30
- component?: string | undefined;
31
- field: string;
32
- value?: string | undefined;
33
- options?: string[] | undefined;
34
- attrs?: Record<string, unknown> | undefined;
35
- optional?: boolean | TFtring | undefined;
36
- disabled?: boolean | TFtring | undefined;
37
- hidden?: boolean | TFtring | undefined;
38
- length?: number | undefined;
39
- validators?: (TFtring | TFoormValidatorFn<string>)[] | undefined;
40
- };
21
+ protected normalizeEntry<T, O>(e: TFoormEntry<T, O>): TFoormEntry<T, O>;
41
22
  executable(): {
42
23
  title: string | TFoormFn<undefined, string>;
43
24
  submit: {
44
25
  text: string | TFoormFn<undefined, string>;
45
26
  disabled: boolean | TFoormFn<undefined, boolean>;
46
27
  };
47
- entries: {
48
- label: string | TFoormFn<unknown, string>;
49
- description: string | TFoormFn<unknown, string>;
50
- hint: unknown;
51
- placeholder: string | TFoormFn<unknown, string>;
52
- classes: string | TFoormFn<unknown, string> | Record<string, boolean | TFoormFn<unknown, boolean>>;
53
- styles: string | TFoormFn<unknown, string> | Record<string, string | TFoormFn<unknown, string>>;
54
- optional: boolean | TFoormFn<unknown, boolean>;
55
- disabled: boolean | TFoormFn<unknown, boolean>;
56
- hidden: boolean | TFoormFn<unknown, boolean>;
57
- name: string;
58
- type: string;
59
- component?: string | undefined;
60
- field: string;
61
- value?: string | undefined;
62
- options?: string[] | undefined;
63
- attrs?: Record<string, unknown> | undefined;
64
- length?: number | undefined;
65
- validators?: (TFtring | TFoormValidatorFn<string>)[] | undefined;
66
- }[];
28
+ entries: TFoormEntryExecutable[];
67
29
  };
68
- getValidator(): (inputs: Record<string, unknown>) => {
30
+ prepareValidators(_validators: TFoormEntry['validators']): TFoormValidatorFn<string>[];
31
+ getFormValidator(): (inputs: Record<string, unknown>) => {
69
32
  passed: boolean;
70
33
  errors: Record<string, string>;
71
34
  };
72
35
  }
73
36
  export type TFoormExecutableMeta = ReturnType<Foorm['executable']>;
74
- export declare function validate<T = string>(opts: {
75
- v: T;
76
- data: Record<string, unknown>;
77
- entry?: TFoormEntry<T>;
37
+ export type TFoormExecutableEntry = TFoormExecutableMeta['entries'][number];
38
+ export declare function validate<T = string>(opts: TFoormFnCtx<T> & {
78
39
  validators: TFoormValidatorFn<T>[];
79
40
  }): {
80
41
  passed: boolean;
package/dist/index.cjs CHANGED
@@ -59,10 +59,15 @@ class Foorm {
59
59
  // strings || objects
60
60
  classes: transformFtringsInObj(e.classes, this.fns), styles: transformFtringsInObj(e.styles, this.fns),
61
61
  // booleans
62
- optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns) }))),
62
+ optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators) }))),
63
63
  };
64
64
  }
65
- getValidator() {
65
+ prepareValidators(_validators) {
66
+ const validators = (_validators || []).map((v) => isFtring(v) ? this.fns.getFn(v.v) : v);
67
+ validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
68
+ return validators;
69
+ }
70
+ getFormValidator() {
66
71
  if (!this.fns)
67
72
  this.fns = new ftring$1.FtringsPool();
68
73
  const entries = this.executable().entries;
@@ -71,7 +76,7 @@ class Foorm {
71
76
  if (entry.field) {
72
77
  fields[entry.field] = {
73
78
  entry,
74
- validators: (entry.validators || []).map((v) => isFtring(v) ? this.fns.getFn(v.v) : v),
79
+ validators: this.prepareValidators(entry.validators),
75
80
  };
76
81
  }
77
82
  fields[entry.field].validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
@@ -80,25 +85,34 @@ class Foorm {
80
85
  let passed = true;
81
86
  const errors = {};
82
87
  for (const [key, value] of Object.entries(fields)) {
83
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
84
88
  const evalEntry = Object.assign({}, value.entry);
85
89
  const ctx = {
86
90
  v: data[key],
87
- validators: value.validators,
88
- entry: value.entry,
91
+ entry: {
92
+ field: evalEntry.field,
93
+ type: evalEntry.type,
94
+ component: evalEntry.component,
95
+ name: evalEntry.name,
96
+ length: evalEntry.length,
97
+ },
89
98
  data,
90
99
  };
91
- if (typeof evalEntry.optional === 'function') {
92
- evalEntry.optional = evalEntry.optional(ctx);
93
- }
94
- if (typeof evalEntry.disabled === 'function') {
95
- evalEntry.disabled = evalEntry.disabled(ctx);
100
+ if (ctx.entry) {
101
+ if (typeof evalEntry.disabled === 'function') {
102
+ ctx.entry.disabled = evalEntry.disabled = evalEntry.disabled(ctx);
103
+ }
104
+ if (typeof evalEntry.optional === 'function') {
105
+ ctx.entry.optional = evalEntry.optional = evalEntry.optional(ctx);
106
+ }
107
+ if (typeof evalEntry.hidden === 'function') {
108
+ ctx.entry.hidden = evalEntry.hidden = evalEntry.hidden(ctx);
109
+ }
96
110
  }
97
111
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
112
  const result = validate({
99
113
  v: data[key],
100
114
  validators: value.validators,
101
- entry: evalEntry,
115
+ entry: ctx.entry,
102
116
  data,
103
117
  });
104
118
  if (!result.passed) {
package/dist/index.mjs CHANGED
@@ -57,10 +57,15 @@ class Foorm {
57
57
  // strings || objects
58
58
  classes: transformFtringsInObj(e.classes, this.fns), styles: transformFtringsInObj(e.styles, this.fns),
59
59
  // booleans
60
- optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns) }))),
60
+ optional: transformFtrings(e.optional, this.fns), disabled: transformFtrings(e.disabled, this.fns), hidden: transformFtrings(e.hidden, this.fns), validators: this.prepareValidators(e.validators) }))),
61
61
  };
62
62
  }
63
- getValidator() {
63
+ prepareValidators(_validators) {
64
+ const validators = (_validators || []).map((v) => isFtring(v) ? this.fns.getFn(v.v) : v);
65
+ validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
66
+ return validators;
67
+ }
68
+ getFormValidator() {
64
69
  if (!this.fns)
65
70
  this.fns = new FtringsPool();
66
71
  const entries = this.executable().entries;
@@ -69,7 +74,7 @@ class Foorm {
69
74
  if (entry.field) {
70
75
  fields[entry.field] = {
71
76
  entry,
72
- validators: (entry.validators || []).map((v) => isFtring(v) ? this.fns.getFn(v.v) : v),
77
+ validators: this.prepareValidators(entry.validators),
73
78
  };
74
79
  }
75
80
  fields[entry.field].validators.unshift(this.fns.getFn('entry.optional || !!v || "Required"'));
@@ -78,25 +83,34 @@ class Foorm {
78
83
  let passed = true;
79
84
  const errors = {};
80
85
  for (const [key, value] of Object.entries(fields)) {
81
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
86
  const evalEntry = Object.assign({}, value.entry);
83
87
  const ctx = {
84
88
  v: data[key],
85
- validators: value.validators,
86
- entry: value.entry,
89
+ entry: {
90
+ field: evalEntry.field,
91
+ type: evalEntry.type,
92
+ component: evalEntry.component,
93
+ name: evalEntry.name,
94
+ length: evalEntry.length,
95
+ },
87
96
  data,
88
97
  };
89
- if (typeof evalEntry.optional === 'function') {
90
- evalEntry.optional = evalEntry.optional(ctx);
91
- }
92
- if (typeof evalEntry.disabled === 'function') {
93
- evalEntry.disabled = evalEntry.disabled(ctx);
98
+ if (ctx.entry) {
99
+ if (typeof evalEntry.disabled === 'function') {
100
+ ctx.entry.disabled = evalEntry.disabled = evalEntry.disabled(ctx);
101
+ }
102
+ if (typeof evalEntry.optional === 'function') {
103
+ ctx.entry.optional = evalEntry.optional = evalEntry.optional(ctx);
104
+ }
105
+ if (typeof evalEntry.hidden === 'function') {
106
+ ctx.entry.hidden = evalEntry.hidden = evalEntry.hidden(ctx);
107
+ }
94
108
  }
95
109
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
110
  const result = validate({
97
111
  v: data[key],
98
112
  validators: value.validators,
99
- entry: evalEntry,
113
+ entry: ctx.entry,
100
114
  data,
101
115
  });
102
116
  if (!result.passed) {
package/dist/types.d.ts CHANGED
@@ -8,12 +8,18 @@ export type ObjSOF = Record<string, StringOrFtring>;
8
8
  export type TFoormFnCtx<T = string> = {
9
9
  v?: T;
10
10
  data: Record<string, unknown>;
11
- entry?: TFoormEntry<T>;
11
+ entry?: Pick<TFoormEntry<T, unknown, string, boolean>, TRelevantFields> & {
12
+ optional?: boolean;
13
+ disabled?: boolean;
14
+ hidden?: boolean;
15
+ };
12
16
  action?: string;
13
17
  };
14
18
  export type TFoormValidatorFn<T = string> = (ctx: TFoormFnCtx<T>) => string | boolean;
15
19
  export type TFoormFn<T = string, R = string | boolean> = (ctx: TFoormFnCtx<T>) => R;
20
+ type TRelevantFields = 'field' | 'type' | 'component' | 'name' | 'attrs' | 'length';
16
21
  export interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtring, FNFTR = TFtring> {
22
+ field: string;
17
23
  label?: string | SFTR;
18
24
  description?: string | SFTR;
19
25
  hint?: string | SFTR;
@@ -23,7 +29,6 @@ export interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtr
23
29
  type?: string;
24
30
  component?: string;
25
31
  name?: string;
26
- field: string;
27
32
  value?: T;
28
33
  options?: O[];
29
34
  attrs?: Record<string, unknown>;
@@ -33,3 +38,5 @@ export interface TFoormEntry<T = string, O = string, SFTR = TFtring, BFTR = TFtr
33
38
  length?: number;
34
39
  validators?: (FNFTR | TFoormValidatorFn<T>)[];
35
40
  }
41
+ export type TFoormEntryExecutable<T = unknown, O = string> = TFoormEntry<T, O, TFoormFn<T, string>, TFoormFn<T, boolean>, TFoormValidatorFn<T>>;
42
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foorm",
3
- "version": "0.0.2-alpha.0",
3
+ "version": "0.0.2-alpha.1",
4
4
  "description": "foorm",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",