@apia/validations2-controller 5.2.1 → 5.2.3

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.
@@ -0,0 +1,51 @@
1
+ import { ValidationStore } from './store.js';
2
+
3
+ /**
4
+ * Borrador de edicion sobre un `ValidationStore`.
5
+ *
6
+ * Permite que un modal (o cualquier editor temporal) trabaje sobre una copia
7
+ * aislada de los valores del formulario: mientras se edita, el store original
8
+ * no cambia. Solo al llamar a `commit()` se copian los valores editados al
9
+ * store original, y `revert()` descarta lo editado volviendo a los valores con
10
+ * los que se creo el borrador.
11
+ *
12
+ * Se leen las definiciones de los `paths` indicados para que las condiciones
13
+ * (`required`, `requiredCondition`, `hiddenCondition`, ...) se evaluen igual
14
+ * que en el store original.
15
+ *
16
+ * @example
17
+ *
18
+ * const draft = createValidationDraft(store, ['notifications.C.subject']);
19
+ *
20
+ * <ValidationContext store={draft.store}>
21
+ * <ValidatedTextarea path="notifications.C.subject" label={label} />
22
+ * </ValidationContext>
23
+ */
24
+ declare class ValidationDraftStore<T extends Record<string, any> = Record<string, any>> {
25
+ private readonly parentStore;
26
+ /** Store aislado que deben consumir los componentes de edicion. */
27
+ readonly store: ValidationStore<T>;
28
+ private readonly paths;
29
+ private readonly initialValues;
30
+ constructor(parentStore: ValidationStore<T>, paths: string[]);
31
+ /** Valor actual del borrador. */
32
+ getValue<ValueType = unknown>(path: string): ValueType;
33
+ /**
34
+ * Registra un error en el borrador. A diferencia de escribir sobre el store
35
+ * original, el error desaparece si el usuario descarta la edicion.
36
+ */
37
+ setError(path: string, error?: string): void;
38
+ /**
39
+ * Copia al store original los valores del borrador. Solo se escriben los
40
+ * valores que realmente cambiaron, para no marcar el formulario como
41
+ * modificado cuando el usuario confirma sin editar nada.
42
+ */
43
+ commit(): void;
44
+ /** Descarta lo editado y vuelve a los valores con los que se abrio el borrador. */
45
+ revert(): void;
46
+ private captureValues;
47
+ }
48
+ declare const createValidationDraft: <T extends Record<string, any>>(parentStore: ValidationStore<T>, paths: string[]) => ValidationDraftStore<T>;
49
+
50
+ export { ValidationDraftStore, createValidationDraft };
51
+ //# sourceMappingURL=draft.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"draft.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/draft.js ADDED
@@ -0,0 +1,83 @@
1
+ import { ValidationStore } from './store.js';
2
+ import { deepEqual } from '@apia/util';
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => {
7
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
+ return value;
9
+ };
10
+ const IN_MEMORY_BACKEND = {
11
+ startCreation: async () => ({ definitions: {}, values: {} }),
12
+ startEdition: async () => ({ definitions: {}, values: {} }),
13
+ confirm: async () => ({ success: false, errors: [] })
14
+ };
15
+ class ValidationDraftStore {
16
+ constructor(parentStore, paths) {
17
+ this.parentStore = parentStore;
18
+ /** Store aislado que deben consumir los componentes de edicion. */
19
+ __publicField(this, "store");
20
+ __publicField(this, "paths");
21
+ __publicField(this, "initialValues");
22
+ this.paths = Array.from(new Set(paths));
23
+ this.store = new ValidationStore(IN_MEMORY_BACKEND);
24
+ const definitions = {};
25
+ for (const path of this.paths) {
26
+ const definition = parentStore.getDefinition(path);
27
+ if (definition) {
28
+ definitions[path] = definition;
29
+ }
30
+ }
31
+ this.store.setState({
32
+ definitions,
33
+ // Se copian todos los valores del formulario, no solo los `paths` del
34
+ // borrador, para que las condiciones de las definiciones puedan leer el
35
+ // resto del formulario igual que en el store original.
36
+ values: parentStore.export()
37
+ });
38
+ this.initialValues = this.captureValues();
39
+ }
40
+ /** Valor actual del borrador. */
41
+ getValue(path) {
42
+ return this.store.getValue(path);
43
+ }
44
+ /**
45
+ * Registra un error en el borrador. A diferencia de escribir sobre el store
46
+ * original, el error desaparece si el usuario descarta la edicion.
47
+ */
48
+ setError(path, error) {
49
+ this.store.setError(path, error);
50
+ }
51
+ /**
52
+ * Copia al store original los valores del borrador. Solo se escriben los
53
+ * valores que realmente cambiaron, para no marcar el formulario como
54
+ * modificado cuando el usuario confirma sin editar nada.
55
+ */
56
+ commit() {
57
+ for (const path of this.paths) {
58
+ const value = this.store.getValue(path);
59
+ if (deepEqual(value, this.parentStore.getValue(path))) {
60
+ continue;
61
+ }
62
+ this.parentStore.setValue(path, value);
63
+ }
64
+ }
65
+ /** Descarta lo editado y vuelve a los valores con los que se abrio el borrador. */
66
+ revert() {
67
+ for (const path of this.paths) {
68
+ this.store.setValue(path, this.initialValues[path], false);
69
+ this.store.setError(path);
70
+ }
71
+ }
72
+ captureValues() {
73
+ const values = {};
74
+ for (const path of this.paths) {
75
+ values[path] = this.store.getValue(path);
76
+ }
77
+ return values;
78
+ }
79
+ }
80
+ const createValidationDraft = (parentStore, paths) => new ValidationDraftStore(parentStore, paths);
81
+
82
+ export { ValidationDraftStore, createValidationDraft };
83
+ //# sourceMappingURL=draft.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"draft.js","sources":["../src/draft.ts"],"sourcesContent":["import { ValidationStore } from './store';\r\nimport { IValidationBackend, TValidationDefinition } from './types';\r\nimport { deepEqual } from '@apia/util';\r\n\r\n/**\r\n * Backend minimo para los stores que no cargan ni persisten datos por si\r\n * mismos. El borrador solo necesita el estado en memoria del store.\r\n */\r\nconst IN_MEMORY_BACKEND: IValidationBackend<any> = {\r\n startCreation: async () => ({ definitions: {}, values: {} }),\r\n startEdition: async () => ({ definitions: {}, values: {} }),\r\n confirm: async () => ({ success: false, errors: [] }),\r\n};\r\n\r\n/**\r\n * Borrador de edicion sobre un `ValidationStore`.\r\n *\r\n * Permite que un modal (o cualquier editor temporal) trabaje sobre una copia\r\n * aislada de los valores del formulario: mientras se edita, el store original\r\n * no cambia. Solo al llamar a `commit()` se copian los valores editados al\r\n * store original, y `revert()` descarta lo editado volviendo a los valores con\r\n * los que se creo el borrador.\r\n *\r\n * Se leen las definiciones de los `paths` indicados para que las condiciones\r\n * (`required`, `requiredCondition`, `hiddenCondition`, ...) se evaluen igual\r\n * que en el store original.\r\n *\r\n * @example\r\n *\r\n * const draft = createValidationDraft(store, ['notifications.C.subject']);\r\n *\r\n * <ValidationContext store={draft.store}>\r\n * <ValidatedTextarea path=\"notifications.C.subject\" label={label} />\r\n * </ValidationContext>\r\n */\r\nexport class ValidationDraftStore<\r\n T extends Record<string, any> = Record<string, any>,\r\n> {\r\n /** Store aislado que deben consumir los componentes de edicion. */\r\n public readonly store: ValidationStore<T>;\r\n\r\n private readonly paths: string[];\r\n private readonly initialValues: Record<string, unknown>;\r\n\r\n public constructor(\r\n private readonly parentStore: ValidationStore<T>,\r\n paths: string[],\r\n ) {\r\n this.paths = Array.from(new Set(paths));\r\n this.store = new ValidationStore<T>(IN_MEMORY_BACKEND);\r\n\r\n const definitions: Record<string, TValidationDefinition> = {};\r\n\r\n for (const path of this.paths) {\r\n const definition = parentStore.getDefinition(path);\r\n\r\n if (definition) {\r\n definitions[path] = definition;\r\n }\r\n }\r\n\r\n this.store.setState({\r\n definitions,\r\n // Se copian todos los valores del formulario, no solo los `paths` del\r\n // borrador, para que las condiciones de las definiciones puedan leer el\r\n // resto del formulario igual que en el store original.\r\n values: parentStore.export() as T,\r\n });\r\n\r\n this.initialValues = this.captureValues();\r\n }\r\n\r\n /** Valor actual del borrador. */\r\n public getValue<ValueType = unknown>(path: string) {\r\n return this.store.getValue<ValueType>(path);\r\n }\r\n\r\n /**\r\n * Registra un error en el borrador. A diferencia de escribir sobre el store\r\n * original, el error desaparece si el usuario descarta la edicion.\r\n */\r\n public setError(path: string, error?: string) {\r\n this.store.setError(path, error);\r\n }\r\n\r\n /**\r\n * Copia al store original los valores del borrador. Solo se escriben los\r\n * valores que realmente cambiaron, para no marcar el formulario como\r\n * modificado cuando el usuario confirma sin editar nada.\r\n */\r\n public commit() {\r\n for (const path of this.paths) {\r\n const value = this.store.getValue(path);\r\n\r\n if (deepEqual(value, this.parentStore.getValue(path))) {\r\n continue;\r\n }\r\n\r\n this.parentStore.setValue(path, value);\r\n }\r\n }\r\n\r\n /** Descarta lo editado y vuelve a los valores con los que se abrio el borrador. */\r\n public revert() {\r\n for (const path of this.paths) {\r\n this.store.setValue(path, this.initialValues[path], false);\r\n this.store.setError(path);\r\n }\r\n }\r\n\r\n private captureValues() {\r\n const values: Record<string, unknown> = {};\r\n\r\n for (const path of this.paths) {\r\n values[path] = this.store.getValue(path);\r\n }\r\n\r\n return values;\r\n }\r\n}\r\n\r\nexport const createValidationDraft = <T extends Record<string, any>>(\r\n parentStore: ValidationStore<T>,\r\n paths: string[],\r\n) => new ValidationDraftStore<T>(parentStore, paths);\r\n"],"names":[],"mappings":";;;;;;;;;AAQA,MAAM,iBAAA,GAA6C;AAAA,EACjD,aAAA,EAAe,aAAa,EAAE,WAAA,EAAa,EAAC,EAAG,MAAA,EAAQ,EAAC,EAAE,CAAA;AAAA,EAC1D,YAAA,EAAc,aAAa,EAAE,WAAA,EAAa,EAAC,EAAG,MAAA,EAAQ,EAAC,EAAE,CAAA;AAAA,EACzD,SAAS,aAAa,EAAE,SAAS,KAAA,EAAO,MAAA,EAAQ,EAAC,EAAE;AACrD,CAAA;AAuBO,MAAM,oBAAA,CAEX;AAAA,EAOO,WAAA,CACY,aACjB,KAAA,EACA;AAFiB,IAAA,IAAA,CAAA,WAAA,GAAA,WAAA;AANnB;AAAA,IAAA,aAAA,CAAA,IAAA,EAAgB,OAAA,CAAA;AAEhB,IAAA,aAAA,CAAA,IAAA,EAAiB,OAAA,CAAA;AACjB,IAAA,aAAA,CAAA,IAAA,EAAiB,eAAA,CAAA;AAMf,IAAA,IAAA,CAAK,QAAQ,KAAA,CAAM,IAAA,CAAK,IAAI,GAAA,CAAI,KAAK,CAAC,CAAA;AACtC,IAAA,IAAA,CAAK,KAAA,GAAQ,IAAI,eAAA,CAAmB,iBAAiB,CAA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AA,CAAc,IAAI,GAAG,KAAK,CAAA;AACzD,MAAA,IAAA,CAAK,KAAA,CAAM,SAAS,IAAI,CAAA;AAAA;AAC1B;AACF,EAEQ,aAAA,GAAgB;AACtB,IAAA,MAAM,SAAkC,EAAC;AAEzC,IAAA,KAAA,MAAW,IAAA,IAAQ,KAAK,KAAA,EAAO;AAC7B,MAAA,MAAA,CAAO,IAAI,CAAA,GAAI,IAAA,CAAK,KAAA,CAAM,SAAS,IAAI,CAAA;AAAA;AAGzC,IAAA,OAAO,MAAA;AAAA;AAEX;AAEO,MAAM,wBAAwB,CACnC,WAAA,EACA,UACG,IAAI,oBAAA,CAAwB,aAAa,KAAK;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Field } from './Field.js';
2
2
  export { ValidationStore } from './store.js';
3
3
  export { ApiaValidationBackend } from './ApiaValidationBackend.js';
4
+ export { ValidationDraftStore, createValidationDraft } from './draft.js';
4
5
  export { IValidationBackend, TPayload, TServerInitialResponse, TServerValidationResult, TValidationDefinition, TValidationError, TValidationOption, TValidationResult, TValidationRules, TValidationState, ValidationsContext, ValidationsSubmitProcessor } from './types.js';
5
6
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { Field } from './Field.js';
2
2
  export { ValidationStore } from './store.js';
3
3
  export { ApiaValidationBackend } from './ApiaValidationBackend.js';
4
+ export { ValidationDraftStore, createValidationDraft } from './draft.js';
4
5
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apia/validations2-controller",
3
- "version": "5.2.1",
3
+ "version": "5.2.3",
4
4
  "sideEffects": false,
5
5
  "author": "Alexis Leite <alexisleite@live.com>",
6
6
  "main": "dist/index.js",
@@ -30,9 +30,9 @@
30
30
  "registry": "https://registry.npmjs.org/"
31
31
  },
32
32
  "dependencies": {
33
- "@apia/api2": "^5.2.1",
34
- "@apia/util": "^5.2.1",
33
+ "@apia/api2": "^5.2.3",
34
+ "@apia/util": "^5.2.3",
35
35
  "mobx": "6.12.3"
36
36
  },
37
- "gitHead": "0f50f1b97ab48cd6fb2f47195cca1b9d93fa80fa"
37
+ "gitHead": "1e66d1cfe47cd897db7d90055ff9ddb02b9dbfca"
38
38
  }