@stubber/form-fields 1.5.2 → 1.6.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.
@@ -1,24 +1,30 @@
1
- <script>import { get as getStoreValue } from "svelte/store";
1
+ <script>import { get as getStoreValue, writable } from "svelte/store";
2
2
  import FormField from "./form-field.svelte";
3
3
  import { build_fields } from "./utils";
4
4
  import { validate_field } from "./validations/validate_field";
5
5
  export let initial_form;
6
6
  export let form;
7
- export let attachments;
7
+ export let attachments = writable([]);
8
8
  export let dependencies = void 0;
9
+ export let form_valid = false;
9
10
  if (initial_form.data) {
10
11
  form.update((data) => {
11
12
  return { ...data, ...initial_form.data };
12
13
  });
13
14
  }
14
15
  const fields = build_fields(form, attachments, dependencies, initial_form.spec.fields);
15
- $: {
16
- validate_form($form);
17
- }
16
+ let form_errors = {};
17
+ $: validate_form($form);
18
18
  const validate_form = async (_) => {
19
+ form_errors = {};
19
20
  for (const fieldStore of fields) {
20
- await validate_field(form, fieldStore);
21
+ const { key, errors } = await validate_field(form, fieldStore);
22
+ if (errors.length > 0) {
23
+ form_errors[key] = errors;
24
+ }
21
25
  }
26
+ form_errors = { ...form_errors };
27
+ form_valid = Object.keys(form_errors).length === 0;
22
28
  };
23
29
  </script>
24
30
 
@@ -1,12 +1,14 @@
1
1
  import { SvelteComponent } from "svelte";
2
2
  import { type Writable } from "svelte/store";
3
3
  import type { IFormDependencies, IInitialForm } from "./interfaces";
4
+ import type { UploadedFile } from "./fileserver";
4
5
  declare const __propDef: {
5
6
  props: {
6
7
  initial_form: IInitialForm;
7
8
  form: Writable<any>;
8
- attachments: Writable<any>;
9
+ attachments?: Writable<UploadedFile[]>;
9
10
  dependencies?: IFormDependencies | undefined;
11
+ form_valid?: boolean;
10
12
  };
11
13
  events: {
12
14
  [evt: string]: CustomEvent<any>;
@@ -154,6 +154,7 @@ export interface IInitialForm {
154
154
  export type IFieldType = keyof typeof fields;
155
155
  export interface IBuiltField<T = {}> {
156
156
  id: string;
157
+ key: string;
157
158
  formStore: Writable<any>;
158
159
  attachmentsStore: Writable<UploadedFile[]>;
159
160
  formDependencies?: IFormDependencies;
@@ -202,6 +203,3 @@ export interface IValidationResult {
202
203
  type: "error" | "info";
203
204
  message: string;
204
205
  }
205
- export type ValidationTree = {
206
- [key: string]: ValidationTree | IValidationResult[] | undefined;
207
- };
@@ -311,6 +311,7 @@ function select_field_type(newValue, trigger) {
311
311
  </Collapsible.Trigger>
312
312
 
313
313
  <Collapsible.Content class="flex flex-col gap-y-2 pl-2 pt-2">
314
+ <!-- todo this doesnt update when fieldtype changes -->
314
315
  {@const field_param_spec = field_params_spec_lookup[value.fieldtype ?? "unknown"]}
315
316
  {#if field_param_spec}
316
317
  <Form
@@ -10,6 +10,7 @@ export const build_field = (formStore, attachmentsStore, formDependencies, field
10
10
  let final_path = generate_field_data_path(field_key, field, base_path);
11
11
  const built_field = {
12
12
  id: `${field_key}-${final_path}`,
13
+ key: field_key,
13
14
  formStore,
14
15
  attachmentsStore,
15
16
  formDependencies,
@@ -1,6 +1,9 @@
1
- import type { IJsonataValidation, IRequiredValidation, IRexegValidation, IValidationResult, IBuiltField } from "../interfaces";
2
1
  import type { Writable } from "svelte/store";
3
- export declare const validate_field: (formStore: Writable<any>, fieldStore: Writable<IBuiltField>) => Promise<void>;
2
+ import type { IBuiltField, IJsonataValidation, IRequiredValidation, IRexegValidation, IValidationResult } from "../interfaces";
3
+ export declare const validate_field: (formStore: Writable<any>, fieldStore: Writable<IBuiltField>) => Promise<{
4
+ key: string;
5
+ errors: IValidationResult[];
6
+ }>;
4
7
  export declare const required_validation: (results: IValidationResult[], value: any, validation: IRequiredValidation) => void;
5
8
  export declare const regex_validation: (results: IValidationResult[], value: any, validation: IRexegValidation) => void;
6
9
  export declare const jsonata_validation: (results: IValidationResult[], _value: any, validation: IJsonataValidation, context?: any) => Promise<void>;
@@ -1,5 +1,5 @@
1
1
  import jsonata from "jsonata";
2
- import { get, set, unset } from "lodash-es";
2
+ import { unset } from "lodash-es";
3
3
  import { get as getStoreValue } from "svelte/store";
4
4
  export const validate_field = async (formStore, fieldStore) => {
5
5
  const results = [];
@@ -20,7 +20,7 @@ export const validate_field = async (formStore, fieldStore) => {
20
20
  f.hidden = true;
21
21
  return f;
22
22
  });
23
- return; // Skip validation if conditions are not met
23
+ return { key: field.key, errors: [] }; // Skip validation if conditions are not met
24
24
  }
25
25
  else {
26
26
  fieldStore.update((f) => {
@@ -31,10 +31,11 @@ export const validate_field = async (formStore, fieldStore) => {
31
31
  }
32
32
  // validate sub_fields if they exist (arraybuilder and section fields)
33
33
  if (field.sub_fields && field.sub_fields.length > 0) {
34
- // console.log("sub_fields", field.sub_fields);
35
34
  for (const sub_field of field.sub_fields || []) {
36
- // console.log(`Validating sub_field: ${sub_field.__key}`);
37
- await validate_field(formStore, sub_field);
35
+ const { key, errors } = await validate_field(formStore, sub_field);
36
+ if (errors.length > 0) {
37
+ results.push({ message: `${field.fieldtype} has errors in child fields`, type: "error" });
38
+ }
38
39
  }
39
40
  }
40
41
  const validations = field.validations || [];
@@ -56,14 +57,15 @@ export const validate_field = async (formStore, fieldStore) => {
56
57
  unset(f, "errors");
57
58
  return f;
58
59
  });
59
- return;
60
60
  }
61
- // console.log(`setting errors for field: ${field.data_path}`, results);
62
- // fieldStore.errors = results;
63
- fieldStore.update((f) => {
64
- f.errors = results;
65
- return f;
66
- });
61
+ else {
62
+ // fieldStore.errors = results;
63
+ fieldStore.update((f) => {
64
+ f.errors = results;
65
+ return f;
66
+ });
67
+ }
68
+ return { key: field.key, errors: results };
67
69
  };
68
70
  export const required_validation = (results, value, validation) => {
69
71
  if (!value || value.length === 0 || (typeof value === "string" && value.trim() === "")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stubber/form-fields",
3
- "version": "1.5.2",
3
+ "version": "1.6.1",
4
4
  "description": "An automatic form builder based on field specifications",
5
5
  "keywords": [
6
6
  "components",