@fjell/validation 4.4.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/LICENSE +203 -0
- package/README.md +24 -0
- package/dist/ItemValidator.d.ts +43 -0
- package/dist/KeyValidator.d.ts +56 -0
- package/dist/LocationValidator.d.ts +39 -0
- package/dist/QueryValidator.d.ts +56 -0
- package/dist/errors.d.ts +9 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +607 -0
- package/dist/schema.d.ts +23 -0
- package/dist/types.d.ts +69 -0
- package/dist/utils.d.ts +4 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2025 Fjell Project
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
203
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @fjell/validation
|
|
2
|
+
|
|
3
|
+
Validation logic for Fjell items and keys. This package contains validators that can be used on both client and server side.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @fjell/validation
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ItemValidator, KeyValidator } from '@fjell/validation';
|
|
15
|
+
import { PriKey } from '@fjell/types';
|
|
16
|
+
|
|
17
|
+
const key: PriKey<'user'> = { kt: 'user', pk: '123' };
|
|
18
|
+
|
|
19
|
+
// Validate a key
|
|
20
|
+
if (KeyValidator.isPriKey(key)) {
|
|
21
|
+
console.log('Valid primary key');
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Item validation
|
|
3
|
+
*
|
|
4
|
+
* Validates that Item objects have correct key types and structures.
|
|
5
|
+
*/
|
|
6
|
+
import type { Item } from "@fjell/types";
|
|
7
|
+
import type { AllItemTypeArrays } from "@fjell/types";
|
|
8
|
+
/**
|
|
9
|
+
* Validates that an item or array of items have the correct primary key type
|
|
10
|
+
*
|
|
11
|
+
* @param input - The item or array of items to validate
|
|
12
|
+
* @param pkType - The expected primary key type
|
|
13
|
+
* @returns The validated item(s)
|
|
14
|
+
* @throws Error if any item is invalid or has wrong key type
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* // Validate single item
|
|
19
|
+
* const item = validatePK(fetchedItem, 'product');
|
|
20
|
+
*
|
|
21
|
+
* // Validate array of items
|
|
22
|
+
* const items = validatePK(fetchedItems, 'product');
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare const validatePK: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(input: Item<S, L1, L2, L3, L4, L5> | Item<S, L1, L2, L3, L4, L5>[], pkType: S) => Item<S, L1, L2, L3, L4, L5> | Item<S, L1, L2, L3, L4, L5>[];
|
|
26
|
+
/**
|
|
27
|
+
* Validates that an item's key matches the expected key type array
|
|
28
|
+
*
|
|
29
|
+
* This is used to validate composite items where the key structure must match
|
|
30
|
+
* the complete hierarchy (e.g., [product, store, region])
|
|
31
|
+
*
|
|
32
|
+
* @param item - The item to validate
|
|
33
|
+
* @param keyTypes - The expected key type array
|
|
34
|
+
* @returns The validated item
|
|
35
|
+
* @throws Error if item is invalid or key types don't match
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* // Validate item has correct key structure
|
|
40
|
+
* const item = validateKeys(fetchedItem, ['product', 'store']);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare const validateKeys: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(item: Item<S, L1, L2, L3, L4, L5>, keyTypes: AllItemTypeArrays<S, L1, L2, L3, L4, L5>) => Item<S, L1, L2, L3, L4, L5>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key validation (PriKey, ComKey)
|
|
3
|
+
*
|
|
4
|
+
* Validates key structures and ensures keys match the expected library type.
|
|
5
|
+
*/
|
|
6
|
+
import type { ComKey, PriKey } from "@fjell/types";
|
|
7
|
+
import type { Coordinate } from "@fjell/types";
|
|
8
|
+
/**
|
|
9
|
+
* Validates that a key is valid and matches the expected library type.
|
|
10
|
+
*
|
|
11
|
+
* This function performs comprehensive validation:
|
|
12
|
+
* 1. Validates key type (PriKey vs ComKey) matches library type (primary vs composite)
|
|
13
|
+
* 2. For composite keys, validates location key array order matches hierarchy
|
|
14
|
+
* 3. Validates key structure is correct
|
|
15
|
+
*
|
|
16
|
+
* @param key - The key to validate
|
|
17
|
+
* @param coordinate - The coordinate defining the library's key type hierarchy
|
|
18
|
+
* @param operation - The operation name (for error messages)
|
|
19
|
+
* @throws Error if key type doesn't match library type or location key array order is incorrect
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Validate a PriKey for a primary library
|
|
24
|
+
* validateKey(
|
|
25
|
+
* { kt: 'product', pk: '123' },
|
|
26
|
+
* coordinate,
|
|
27
|
+
* 'get'
|
|
28
|
+
* );
|
|
29
|
+
*
|
|
30
|
+
* // Validate a ComKey for a composite library
|
|
31
|
+
* validateKey(
|
|
32
|
+
* { kt: 'product', pk: '123', loc: [{ kt: 'store', lk: '456' }] },
|
|
33
|
+
* coordinate,
|
|
34
|
+
* 'get'
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const validateKey: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(key: PriKey<S> | ComKey<S, L1, L2, L3, L4, L5>, coordinate: Coordinate<S, L1, L2, L3, L4, L5>, operation: string) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Validates a PriKey structure
|
|
41
|
+
*
|
|
42
|
+
* @param key - The primary key to validate
|
|
43
|
+
* @param expectedType - The expected key type
|
|
44
|
+
* @param operation - The operation name (for error messages)
|
|
45
|
+
* @throws Error if key is invalid
|
|
46
|
+
*/
|
|
47
|
+
export declare const validatePriKey: <S extends string>(key: PriKey<S>, expectedType: S, operation: string) => void;
|
|
48
|
+
/**
|
|
49
|
+
* Validates a ComKey structure
|
|
50
|
+
*
|
|
51
|
+
* @param key - The composite key to validate
|
|
52
|
+
* @param coordinate - The coordinate defining the expected hierarchy
|
|
53
|
+
* @param operation - The operation name (for error messages)
|
|
54
|
+
* @throws Error if key is invalid
|
|
55
|
+
*/
|
|
56
|
+
export declare const validateComKey: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(key: ComKey<S, L1, L2, L3, L4, L5>, coordinate: Coordinate<S, L1, L2, L3, L4, L5>, operation: string) => void;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Location array validation
|
|
3
|
+
*
|
|
4
|
+
* Validates that LocKeyArray parameters match the expected Coordinate hierarchy.
|
|
5
|
+
*/
|
|
6
|
+
import type { LocKeyArray } from "@fjell/types";
|
|
7
|
+
import type { Coordinate } from "@fjell/types";
|
|
8
|
+
import type { ValidationResult } from "./types";
|
|
9
|
+
/**
|
|
10
|
+
* Validates that a standalone LocKeyArray parameter matches the expected hierarchy
|
|
11
|
+
* defined by the coordinate's key type array (kta).
|
|
12
|
+
*
|
|
13
|
+
* This is used to validate the `locations` parameter passed to operations like
|
|
14
|
+
* all(), find(), create(), etc.
|
|
15
|
+
*
|
|
16
|
+
* @param locations - The location key array to validate
|
|
17
|
+
* @param coordinate - The coordinate defining the library's key type hierarchy
|
|
18
|
+
* @param operation - The operation name (for error messages)
|
|
19
|
+
* @throws Error if location key array order is incorrect
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* validateLocations(
|
|
24
|
+
* [{kt: 'store', lk: '123'}],
|
|
25
|
+
* coordinate,
|
|
26
|
+
* 'find'
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare const validateLocations: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(locations: LocKeyArray<L1, L2, L3, L4, L5> | [] | undefined, coordinate: Coordinate<S, L1, L2, L3, L4, L5>, operation: string) => void;
|
|
31
|
+
/**
|
|
32
|
+
* Non-throwing version of validateLocations that returns a ValidationResult
|
|
33
|
+
*
|
|
34
|
+
* @param locations - The location key array to validate
|
|
35
|
+
* @param coordinate - The coordinate defining the library's key type hierarchy
|
|
36
|
+
* @param operation - The operation name (for error messages)
|
|
37
|
+
* @returns ValidationResult with valid flag and optional error message
|
|
38
|
+
*/
|
|
39
|
+
export declare const isValidLocations: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(locations: LocKeyArray<L1, L2, L3, L4, L5> | [] | undefined, coordinate: Coordinate<S, L1, L2, L3, L4, L5>, operation: string) => ValidationResult;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query and parameter validation
|
|
3
|
+
*
|
|
4
|
+
* Validates ItemQuery and OperationParams structures.
|
|
5
|
+
*/
|
|
6
|
+
import type { ItemQuery, OperationParams } from "@fjell/types";
|
|
7
|
+
/**
|
|
8
|
+
* Validates that a query parameter is a valid ItemQuery object.
|
|
9
|
+
*
|
|
10
|
+
* @param query - The query to validate
|
|
11
|
+
* @param operation - The operation name (for error messages)
|
|
12
|
+
* @throws Error if query is invalid
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* validateQuery({ filter: { status: 'active' } }, 'one');
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare const validateQuery: (query: ItemQuery | undefined, operation: string) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Validates that operation parameters are valid.
|
|
22
|
+
*
|
|
23
|
+
* @param params - The parameters to validate
|
|
24
|
+
* @param operation - The operation name (for error messages)
|
|
25
|
+
* @throws Error if parameters are invalid
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* validateOperationParams({ email: 'test@example.com' }, 'find');
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const validateOperationParams: (params: OperationParams | undefined, operation: string) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Validates that a finder name is valid.
|
|
35
|
+
*
|
|
36
|
+
* @param finder - The finder name to validate
|
|
37
|
+
* @param operation - The operation name (for error messages)
|
|
38
|
+
* @throws Error if finder name is invalid
|
|
39
|
+
*/
|
|
40
|
+
export declare const validateFinderName: (finder: string | undefined, operation: string) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Validates that an action name is valid.
|
|
43
|
+
*
|
|
44
|
+
* @param action - The action name to validate
|
|
45
|
+
* @param operation - The operation name (for error messages)
|
|
46
|
+
* @throws Error if action name is invalid
|
|
47
|
+
*/
|
|
48
|
+
export declare const validateActionName: (action: string | undefined, operation: string) => void;
|
|
49
|
+
/**
|
|
50
|
+
* Validates that a facet name is valid.
|
|
51
|
+
*
|
|
52
|
+
* @param facet - The facet name to validate
|
|
53
|
+
* @param operation - The operation name (for error messages)
|
|
54
|
+
* @throws Error if facet name is invalid
|
|
55
|
+
*/
|
|
56
|
+
export declare const validateFacetName: (facet: string | undefined, operation: string) => void;
|
package/dist/errors.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation module
|
|
3
|
+
*
|
|
4
|
+
* Provides centralized validation functions for:
|
|
5
|
+
* - Location arrays (LocKeyArray validation against Coordinate hierarchy)
|
|
6
|
+
* - Keys (PriKey, ComKey validation)
|
|
7
|
+
* - Items (Item key type validation)
|
|
8
|
+
* - Queries (ItemQuery validation)
|
|
9
|
+
* - Operation parameters (OperationParams validation)
|
|
10
|
+
* - Schema validation (Zod, Yup, etc.)
|
|
11
|
+
*/
|
|
12
|
+
export type { ValidationOptions, ValidationResult, SchemaValidator } from './types';
|
|
13
|
+
export { validateLocations, isValidLocations } from './LocationValidator';
|
|
14
|
+
export { validateKey, validatePriKey, validateComKey } from './KeyValidator';
|
|
15
|
+
export { validatePK, validateKeys } from './ItemValidator';
|
|
16
|
+
export { validateQuery, validateOperationParams, validateFinderName, validateActionName, validateFacetName } from './QueryValidator';
|
|
17
|
+
export { validateSchema } from './schema';
|
|
18
|
+
export { ValidationError } from './errors';
|
|
19
|
+
export type { FieldError } from './errors';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
// src/LocationValidator.ts
|
|
2
|
+
import Logging from "@fjell/logging";
|
|
3
|
+
var logger = Logging.getLogger("validation.LocationValidator");
|
|
4
|
+
var validateLocations = (locations, coordinate, operation) => {
|
|
5
|
+
if (!locations || locations.length === 0) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const keyTypeArray = coordinate.kta;
|
|
9
|
+
const expectedLocationTypes = keyTypeArray.slice(1);
|
|
10
|
+
const actualLocationTypes = locations.map((loc) => loc.kt);
|
|
11
|
+
logger.debug(`Validating locations for ${operation}`, {
|
|
12
|
+
expected: expectedLocationTypes,
|
|
13
|
+
actual: actualLocationTypes,
|
|
14
|
+
coordinate: keyTypeArray
|
|
15
|
+
});
|
|
16
|
+
if (actualLocationTypes.length > expectedLocationTypes.length) {
|
|
17
|
+
logger.error("Location key array has too many elements", {
|
|
18
|
+
expected: expectedLocationTypes.length,
|
|
19
|
+
actual: actualLocationTypes.length,
|
|
20
|
+
expectedTypes: expectedLocationTypes,
|
|
21
|
+
actualTypes: actualLocationTypes,
|
|
22
|
+
coordinate,
|
|
23
|
+
operation
|
|
24
|
+
});
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Invalid location key array for ${operation}: Expected at most ${expectedLocationTypes.length} location keys (hierarchy: [${expectedLocationTypes.join(", ")}]), but received ${actualLocationTypes.length} (types: [${actualLocationTypes.join(", ")}])`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < actualLocationTypes.length; i++) {
|
|
30
|
+
if (expectedLocationTypes[i] !== actualLocationTypes[i]) {
|
|
31
|
+
logger.error("Location key array order mismatch", {
|
|
32
|
+
position: i,
|
|
33
|
+
expected: expectedLocationTypes[i],
|
|
34
|
+
actual: actualLocationTypes[i],
|
|
35
|
+
expectedHierarchy: expectedLocationTypes,
|
|
36
|
+
actualOrder: actualLocationTypes,
|
|
37
|
+
coordinate,
|
|
38
|
+
operation
|
|
39
|
+
});
|
|
40
|
+
throw new Error(
|
|
41
|
+
`Invalid location key array order for ${operation}: At position ${i}, expected key type "${expectedLocationTypes[i]}" but received "${actualLocationTypes[i]}". Location keys must be ordered according to the hierarchy: [${expectedLocationTypes.join(", ")}]. Received order: [${actualLocationTypes.join(", ")}]`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
logger.debug(`Location key array validation passed for ${operation}`, { locations });
|
|
46
|
+
};
|
|
47
|
+
var isValidLocations = (locations, coordinate, operation) => {
|
|
48
|
+
try {
|
|
49
|
+
validateLocations(locations, coordinate, operation);
|
|
50
|
+
return { valid: true };
|
|
51
|
+
} catch (error) {
|
|
52
|
+
return {
|
|
53
|
+
valid: false,
|
|
54
|
+
error: error instanceof Error ? error.message : String(error)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/utils.ts
|
|
60
|
+
var isComKey = (key) => {
|
|
61
|
+
return typeof key !== "undefined" && (typeof key.pk !== "undefined" && typeof key.kt !== "undefined") && (typeof key.loc !== "undefined" && Array.isArray(key.loc));
|
|
62
|
+
};
|
|
63
|
+
var isPriKey = (key) => {
|
|
64
|
+
return typeof key !== "undefined" && (typeof key.pk !== "undefined" && typeof key.kt !== "undefined") && typeof key.loc === "undefined";
|
|
65
|
+
};
|
|
66
|
+
var toKeyTypeArray = (ik) => {
|
|
67
|
+
if (isComKey(ik)) {
|
|
68
|
+
const ck = ik;
|
|
69
|
+
return [ck.kt, ...ck.loc.map((l) => l.kt)];
|
|
70
|
+
} else {
|
|
71
|
+
return [ik.kt];
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// src/KeyValidator.ts
|
|
76
|
+
import Logging2 from "@fjell/logging";
|
|
77
|
+
var logger2 = Logging2.getLogger("validation.KeyValidator");
|
|
78
|
+
var validateLocationKeyOrder = (key, coordinate, operation) => {
|
|
79
|
+
const keyTypeArray = coordinate.kta;
|
|
80
|
+
const expectedLocationTypes = keyTypeArray.slice(1);
|
|
81
|
+
const actualLocationTypes = key.loc.map((loc) => loc.kt);
|
|
82
|
+
if (expectedLocationTypes.length !== actualLocationTypes.length) {
|
|
83
|
+
logger2.error("Location key array length mismatch", {
|
|
84
|
+
expected: expectedLocationTypes.length,
|
|
85
|
+
actual: actualLocationTypes.length,
|
|
86
|
+
key,
|
|
87
|
+
coordinate,
|
|
88
|
+
operation
|
|
89
|
+
});
|
|
90
|
+
const expectedOrder = expectedLocationTypes.map(
|
|
91
|
+
(kt, i) => ` [${i}] { kt: '${kt}', lk: <value> }`
|
|
92
|
+
).join("\n");
|
|
93
|
+
const actualOrder = key.loc.map(
|
|
94
|
+
(loc, i) => ` [${i}] { kt: '${loc.kt}', lk: ${JSON.stringify(loc.lk)} }`
|
|
95
|
+
).join("\n");
|
|
96
|
+
throw new Error(
|
|
97
|
+
`Location key array length mismatch for ${operation} operation.
|
|
98
|
+
|
|
99
|
+
Expected ${expectedLocationTypes.length} location keys but received ${actualLocationTypes.length}.
|
|
100
|
+
|
|
101
|
+
Expected location key order for '${keyTypeArray[0]}':
|
|
102
|
+
${expectedOrder}
|
|
103
|
+
|
|
104
|
+
Received location key order:
|
|
105
|
+
${actualOrder}`
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
for (let i = 0; i < expectedLocationTypes.length; i++) {
|
|
109
|
+
if (expectedLocationTypes[i] !== actualLocationTypes[i]) {
|
|
110
|
+
logger2.error("Location key array order mismatch", {
|
|
111
|
+
position: i,
|
|
112
|
+
expected: expectedLocationTypes[i],
|
|
113
|
+
actual: actualLocationTypes[i],
|
|
114
|
+
key,
|
|
115
|
+
coordinate,
|
|
116
|
+
operation
|
|
117
|
+
});
|
|
118
|
+
const expectedOrder = expectedLocationTypes.map(
|
|
119
|
+
(kt, i2) => ` [${i2}] { kt: '${kt}', lk: <value> }`
|
|
120
|
+
).join("\n");
|
|
121
|
+
const actualOrder = key.loc.map(
|
|
122
|
+
(loc, i2) => ` [${i2}] { kt: '${loc.kt}', lk: ${JSON.stringify(loc.lk)} }`
|
|
123
|
+
).join("\n");
|
|
124
|
+
throw new Error(
|
|
125
|
+
`Location key array order mismatch for ${operation} operation.
|
|
126
|
+
|
|
127
|
+
At position ${i}, expected key type "${expectedLocationTypes[i]}" but received "${actualLocationTypes[i]}".
|
|
128
|
+
|
|
129
|
+
Expected location key order for '${keyTypeArray[0]}':
|
|
130
|
+
${expectedOrder}
|
|
131
|
+
|
|
132
|
+
Received location key order:
|
|
133
|
+
${actualOrder}
|
|
134
|
+
|
|
135
|
+
Tip: Location keys must be ordered according to the hierarchy: [${expectedLocationTypes.join(", ")}]`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
var validateKey = (key, coordinate, operation) => {
|
|
141
|
+
logger2.debug(`Validating key for ${operation}`, { key, coordinate: coordinate.kta });
|
|
142
|
+
if (!key || key === null) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Invalid key structure for ${operation} operation.
|
|
145
|
+
|
|
146
|
+
The provided key is null or undefined.
|
|
147
|
+
|
|
148
|
+
Valid key formats:
|
|
149
|
+
PriKey: { kt: string, pk: string|number }
|
|
150
|
+
ComKey: { kt: string, pk: string|number, loc: Array<{ kt: string, lk: string|number }> }`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
const isCompositeLibrary = coordinate.kta.length > 1;
|
|
154
|
+
const keyIsComposite = isComKey(key);
|
|
155
|
+
const keyIsPrimary = isPriKey(key);
|
|
156
|
+
if (isCompositeLibrary && !keyIsComposite) {
|
|
157
|
+
logger2.error(`Composite library received primary key in ${operation}`, { key, coordinate });
|
|
158
|
+
const keyTypeArray = coordinate.kta;
|
|
159
|
+
throw new Error(
|
|
160
|
+
`Invalid key type for ${operation} operation.
|
|
161
|
+
|
|
162
|
+
This is a composite item library. You must provide a ComKey with location keys.
|
|
163
|
+
|
|
164
|
+
Expected: ComKey with format:
|
|
165
|
+
{
|
|
166
|
+
kt: '${keyTypeArray[0]}',
|
|
167
|
+
pk: string|number,
|
|
168
|
+
loc: [
|
|
169
|
+
` + keyTypeArray.slice(1).map((kt) => ` { kt: '${kt}', lk: string|number }`).join(",\n") + `
|
|
170
|
+
]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
Received: PriKey with format:
|
|
174
|
+
${JSON.stringify(key, null, 2)}
|
|
175
|
+
|
|
176
|
+
Example correct usage:
|
|
177
|
+
library.operations.${operation}({
|
|
178
|
+
kt: '${keyTypeArray[0]}',
|
|
179
|
+
pk: 'item-id',
|
|
180
|
+
loc: [${keyTypeArray.slice(1).map((kt) => `{ kt: '${kt}', lk: 'parent-id' }`).join(", ")}]
|
|
181
|
+
})`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (!isCompositeLibrary && keyIsComposite) {
|
|
185
|
+
logger2.error(`Primary library received composite key in ${operation}`, { key, coordinate });
|
|
186
|
+
const keyTypeArray = coordinate.kta;
|
|
187
|
+
throw new Error(
|
|
188
|
+
`Invalid key type for ${operation} operation.
|
|
189
|
+
|
|
190
|
+
This is a primary item library. You should provide a PriKey without location keys.
|
|
191
|
+
|
|
192
|
+
Expected: PriKey with format:
|
|
193
|
+
{ kt: '${keyTypeArray[0]}', pk: string|number }
|
|
194
|
+
|
|
195
|
+
Received: ComKey with format:
|
|
196
|
+
${JSON.stringify(key, null, 2)}
|
|
197
|
+
|
|
198
|
+
Example correct usage:
|
|
199
|
+
library.operations.${operation}({ kt: '${keyTypeArray[0]}', pk: 'item-id' })`
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
if (!keyIsPrimary && !keyIsComposite) {
|
|
203
|
+
logger2.error(`Invalid key structure in ${operation}`, { key, coordinate });
|
|
204
|
+
throw new Error(
|
|
205
|
+
`Invalid key structure for ${operation} operation.
|
|
206
|
+
|
|
207
|
+
The provided key does not match PriKey or ComKey format.
|
|
208
|
+
|
|
209
|
+
Received:
|
|
210
|
+
${JSON.stringify(key, null, 2)}
|
|
211
|
+
|
|
212
|
+
Valid key formats:
|
|
213
|
+
PriKey: { kt: string, pk: string|number }
|
|
214
|
+
ComKey: { kt: string, pk: string|number, loc: Array<{ kt: string, lk: string|number }> }`
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
const expectedKeyType = coordinate.kta[0];
|
|
218
|
+
if (key.kt !== expectedKeyType) {
|
|
219
|
+
logger2.error(`Key type mismatch in ${operation}`, {
|
|
220
|
+
expected: expectedKeyType,
|
|
221
|
+
received: key.kt,
|
|
222
|
+
key,
|
|
223
|
+
coordinate
|
|
224
|
+
});
|
|
225
|
+
throw new Error(
|
|
226
|
+
`Invalid key type for ${operation} operation.
|
|
227
|
+
|
|
228
|
+
Expected key type: '${expectedKeyType}'
|
|
229
|
+
Received key type: '${key.kt}'
|
|
230
|
+
|
|
231
|
+
Example correct usage:
|
|
232
|
+
library.operations.${operation}({ kt: '${expectedKeyType}', pk: 'item-id', ... })`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
if (keyIsComposite) {
|
|
236
|
+
const comKey = key;
|
|
237
|
+
if (comKey.loc.length === 0) {
|
|
238
|
+
logger2.debug(`Empty loc array detected in ${operation} - will search across all locations`, { key });
|
|
239
|
+
} else {
|
|
240
|
+
validateLocationKeyOrder(comKey, coordinate, operation);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
logger2.debug(`Key validation passed for ${operation}`, { key });
|
|
244
|
+
};
|
|
245
|
+
var validatePriKey = (key, expectedType, operation) => {
|
|
246
|
+
if (!key || key === null) {
|
|
247
|
+
throw new Error(`[${operation}] PriKey is undefined or null`);
|
|
248
|
+
}
|
|
249
|
+
if (!key.kt) {
|
|
250
|
+
throw new Error(`[${operation}] PriKey is missing 'kt' field`);
|
|
251
|
+
}
|
|
252
|
+
if (key.kt !== expectedType) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
`[${operation}] PriKey has incorrect type.
|
|
255
|
+
Expected: '${expectedType}'
|
|
256
|
+
Received: '${key.kt}'`
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
if (typeof key.pk === "undefined" || key.pk === null) {
|
|
260
|
+
throw new Error(`[${operation}] PriKey is missing 'pk' field`);
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
var validateComKey = (key, coordinate, operation) => {
|
|
264
|
+
if (!key || key === null) {
|
|
265
|
+
throw new Error(`[${operation}] ComKey is undefined or null`);
|
|
266
|
+
}
|
|
267
|
+
if (!key.kt) {
|
|
268
|
+
throw new Error(`[${operation}] ComKey is missing 'kt' field`);
|
|
269
|
+
}
|
|
270
|
+
if (key.kt !== coordinate.kta[0]) {
|
|
271
|
+
throw new Error(
|
|
272
|
+
`[${operation}] ComKey has incorrect type.
|
|
273
|
+
Expected: '${coordinate.kta[0]}'
|
|
274
|
+
Received: '${key.kt}'`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
if (typeof key.pk === "undefined" || key.pk === null) {
|
|
278
|
+
throw new Error(`[${operation}] ComKey is missing 'pk' field`);
|
|
279
|
+
}
|
|
280
|
+
if (!Array.isArray(key.loc)) {
|
|
281
|
+
throw new Error(`[${operation}] ComKey is missing or invalid 'loc' field (must be an array)`);
|
|
282
|
+
}
|
|
283
|
+
if (key.loc.length > 0) {
|
|
284
|
+
validateLocationKeyOrder(key, coordinate, operation);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
// src/ItemValidator.ts
|
|
289
|
+
import Logging3 from "@fjell/logging";
|
|
290
|
+
var logger3 = Logging3.getLogger("validation.ItemValidator");
|
|
291
|
+
var validatePKForItem = (item, pkType) => {
|
|
292
|
+
if (!item) {
|
|
293
|
+
logger3.error("Item validation failed - item is undefined", {
|
|
294
|
+
component: "core",
|
|
295
|
+
operation: "validatePK",
|
|
296
|
+
expectedType: pkType,
|
|
297
|
+
item,
|
|
298
|
+
suggestion: "Ensure the operation returns a valid item object, not undefined/null"
|
|
299
|
+
});
|
|
300
|
+
throw new Error(
|
|
301
|
+
`Item validation failed: item is undefined. Expected item of type '${pkType}'. This usually indicates a database operation returned null/undefined unexpectedly.`
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
if (!item.key) {
|
|
305
|
+
logger3.error("Item validation failed - item missing key", {
|
|
306
|
+
component: "core",
|
|
307
|
+
operation: "validatePK",
|
|
308
|
+
expectedType: pkType,
|
|
309
|
+
item,
|
|
310
|
+
suggestion: "Ensure the item has a valid key property with kt and pk fields"
|
|
311
|
+
});
|
|
312
|
+
throw new Error(
|
|
313
|
+
`Item validation failed: item does not have a key property. Expected key with type '${pkType}'. Item: ${JSON.stringify(item)}. This indicates a database processing error.`
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
const keyTypeArray = toKeyTypeArray(item.key);
|
|
317
|
+
if (keyTypeArray[0] !== pkType) {
|
|
318
|
+
logger3.error("Key type mismatch during validation", {
|
|
319
|
+
component: "core",
|
|
320
|
+
operation: "validatePK",
|
|
321
|
+
expectedType: pkType,
|
|
322
|
+
actualType: keyTypeArray[0],
|
|
323
|
+
keyTypeArray,
|
|
324
|
+
itemKey: item.key,
|
|
325
|
+
suggestion: `Ensure the item key has kt: '${pkType}', not '${keyTypeArray[0]}'`
|
|
326
|
+
});
|
|
327
|
+
throw new Error(
|
|
328
|
+
`Item has incorrect primary key type. Expected '${pkType}', got '${keyTypeArray[0]}'. Key: ${JSON.stringify(item.key)}. This indicates a data model mismatch.`
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
return item;
|
|
332
|
+
};
|
|
333
|
+
var validatePK = (input, pkType) => {
|
|
334
|
+
logger3.trace("Checking Return Type", { input });
|
|
335
|
+
if (Array.isArray(input)) {
|
|
336
|
+
return input.map((item) => validatePKForItem(item, pkType));
|
|
337
|
+
}
|
|
338
|
+
return validatePKForItem(input, pkType);
|
|
339
|
+
};
|
|
340
|
+
var validateKeys = (item, keyTypes) => {
|
|
341
|
+
logger3.trace("Checking Return Type", { item });
|
|
342
|
+
if (!item) {
|
|
343
|
+
logger3.error("Key validation failed - item is undefined", {
|
|
344
|
+
component: "core",
|
|
345
|
+
operation: "validateKeys",
|
|
346
|
+
expectedKeyTypes: keyTypes,
|
|
347
|
+
suggestion: "Ensure the operation returns a valid item object, not undefined/null"
|
|
348
|
+
});
|
|
349
|
+
throw new Error(
|
|
350
|
+
`Key validation failed: item is undefined. Expected item with key types [${keyTypes.join(", ")}]. This usually indicates a database operation returned null/undefined unexpectedly.`
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
if (!item.key) {
|
|
354
|
+
logger3.error("Key validation failed - item missing key", {
|
|
355
|
+
component: "core",
|
|
356
|
+
operation: "validateKeys",
|
|
357
|
+
expectedKeyTypes: keyTypes,
|
|
358
|
+
item: JSON.stringify(item),
|
|
359
|
+
suggestion: "Ensure the item has a valid key property"
|
|
360
|
+
});
|
|
361
|
+
throw new Error(
|
|
362
|
+
`Key validation failed: item does not have a key property. Expected key with types [${keyTypes.join(", ")}]. Item: ${JSON.stringify(item)}. This indicates a database processing error.`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
const keyTypeArray = toKeyTypeArray(item.key);
|
|
366
|
+
if (keyTypeArray.length !== keyTypes.length) {
|
|
367
|
+
logger3.error("Key hierarchy depth mismatch", {
|
|
368
|
+
component: "core",
|
|
369
|
+
operation: "validateKeys",
|
|
370
|
+
expectedKeyTypes: keyTypes,
|
|
371
|
+
expectedDepth: keyTypes.length,
|
|
372
|
+
actualKeyTypes: keyTypeArray,
|
|
373
|
+
actualDepth: keyTypeArray.length,
|
|
374
|
+
itemKey: item.key,
|
|
375
|
+
suggestion: `Check coordinate definition. Expected hierarchy depth of ${keyTypes.length}, got ${keyTypeArray.length}`
|
|
376
|
+
});
|
|
377
|
+
throw new Error(
|
|
378
|
+
`Item has incorrect key hierarchy depth. Expected ${keyTypes.length} levels [${keyTypes.join(" > ")}], but got ${keyTypeArray.length} levels [${keyTypeArray.join(" > ")}]. Key: ${JSON.stringify(item.key)}. This indicates a coordinate/hierarchy mismatch.`
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
const match = JSON.stringify(keyTypeArray) === JSON.stringify(keyTypes);
|
|
382
|
+
if (!match) {
|
|
383
|
+
logger3.error("Key Type Array Mismatch", {
|
|
384
|
+
component: "core",
|
|
385
|
+
operation: "validateKeys",
|
|
386
|
+
expectedKeyTypes: keyTypes,
|
|
387
|
+
actualKeyTypes: keyTypeArray,
|
|
388
|
+
itemKey: item.key,
|
|
389
|
+
suggestion: `Ensure item key matches expected hierarchy: [${keyTypes.join(" > ")}]`
|
|
390
|
+
});
|
|
391
|
+
throw new Error(
|
|
392
|
+
`Item has incorrect key types. Expected [${keyTypes.join(" > ")}], but got [${keyTypeArray.join(" > ")}]. Key: ${JSON.stringify(item.key)}. This indicates a data model mismatch.`
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
return item;
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
// src/QueryValidator.ts
|
|
399
|
+
import Logging4 from "@fjell/logging";
|
|
400
|
+
var logger4 = Logging4.getLogger("validation.QueryValidator");
|
|
401
|
+
var validateQuery = (query, operation) => {
|
|
402
|
+
if (typeof query === "undefined" || query === null) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (typeof query !== "object") {
|
|
406
|
+
logger4.error(`Invalid query type for ${operation}`, { query, type: typeof query });
|
|
407
|
+
throw new Error(
|
|
408
|
+
`[${operation}] Invalid query parameter.
|
|
409
|
+
|
|
410
|
+
Expected: object or undefined
|
|
411
|
+
Received: ${typeof query}
|
|
412
|
+
|
|
413
|
+
Example valid queries:
|
|
414
|
+
{}
|
|
415
|
+
{ filter: { status: 'active' } }
|
|
416
|
+
{ limit: 10, sort: { field: 'name', order: 'asc' } }`
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
if (Array.isArray(query)) {
|
|
420
|
+
logger4.error(`Query cannot be an array for ${operation}`, { query });
|
|
421
|
+
throw new Error(
|
|
422
|
+
`[${operation}] Invalid query parameter.
|
|
423
|
+
|
|
424
|
+
Query cannot be an array.
|
|
425
|
+
Received: ${JSON.stringify(query)}`
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
logger4.debug(`Query validation passed for ${operation}`, { query });
|
|
429
|
+
};
|
|
430
|
+
var validateOperationParams = (params, operation) => {
|
|
431
|
+
if (typeof params === "undefined") {
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (params === null) {
|
|
435
|
+
logger4.error(`Params cannot be null for ${operation}`, { params });
|
|
436
|
+
throw new Error(
|
|
437
|
+
`[${operation}] Invalid operation parameters.
|
|
438
|
+
|
|
439
|
+
Parameters cannot be null.
|
|
440
|
+
Expected: object or undefined
|
|
441
|
+
Received: null
|
|
442
|
+
|
|
443
|
+
Example valid parameters:
|
|
444
|
+
{}
|
|
445
|
+
{ email: 'user@example.com' }
|
|
446
|
+
{ status: 'active', limit: 10 }`
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
if (typeof params !== "object") {
|
|
450
|
+
logger4.error(`Invalid params type for ${operation}`, { params, type: typeof params });
|
|
451
|
+
throw new Error(
|
|
452
|
+
`[${operation}] Invalid operation parameters.
|
|
453
|
+
|
|
454
|
+
Expected: object or undefined
|
|
455
|
+
Received: ${typeof params}
|
|
456
|
+
|
|
457
|
+
Example valid parameters:
|
|
458
|
+
{}
|
|
459
|
+
{ email: 'user@example.com' }
|
|
460
|
+
{ status: 'active', limit: 10 }`
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
if (Array.isArray(params)) {
|
|
464
|
+
logger4.error(`Params cannot be an array for ${operation}`, { params });
|
|
465
|
+
throw new Error(
|
|
466
|
+
`[${operation}] Invalid operation parameters.
|
|
467
|
+
|
|
468
|
+
Parameters cannot be an array.
|
|
469
|
+
Received: ${JSON.stringify(params)}`
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
for (const [key, value] of Object.entries(params)) {
|
|
473
|
+
const valueType = typeof value;
|
|
474
|
+
const isValidType = valueType === "string" || valueType === "number" || valueType === "boolean" || value instanceof Date || Array.isArray(value) && value.every(
|
|
475
|
+
(v) => typeof v === "string" || typeof v === "number" || typeof v === "boolean" || v instanceof Date
|
|
476
|
+
);
|
|
477
|
+
if (!isValidType) {
|
|
478
|
+
logger4.error(`Invalid param value type for ${operation}`, { key, value, valueType });
|
|
479
|
+
throw new Error(
|
|
480
|
+
`[${operation}] Invalid value type for parameter "${key}".
|
|
481
|
+
|
|
482
|
+
Allowed types: string, number, boolean, Date, or arrays of these types
|
|
483
|
+
Received: ${valueType}
|
|
484
|
+
Value: ${JSON.stringify(value)}`
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
logger4.debug(`Operation params validation passed for ${operation}`, { params });
|
|
489
|
+
};
|
|
490
|
+
var validateFinderName = (finder, operation) => {
|
|
491
|
+
if (!finder || typeof finder !== "string") {
|
|
492
|
+
logger4.error(`Invalid finder name for ${operation}`, { finder, type: typeof finder });
|
|
493
|
+
throw new Error(
|
|
494
|
+
`[${operation}] Finder name must be a non-empty string.
|
|
495
|
+
|
|
496
|
+
Received: ${JSON.stringify(finder)}`
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
if (finder.trim().length === 0) {
|
|
500
|
+
logger4.error(`Empty finder name for ${operation}`, { finder });
|
|
501
|
+
throw new Error(
|
|
502
|
+
`[${operation}] Finder name cannot be empty or whitespace only.
|
|
503
|
+
|
|
504
|
+
Received: "${finder}"`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
logger4.debug(`Finder name validation passed for ${operation}`, { finder });
|
|
508
|
+
};
|
|
509
|
+
var validateActionName = (action, operation) => {
|
|
510
|
+
if (!action || typeof action !== "string") {
|
|
511
|
+
logger4.error(`Invalid action name for ${operation}`, { action, type: typeof action });
|
|
512
|
+
throw new Error(
|
|
513
|
+
`[${operation}] Action name must be a non-empty string.
|
|
514
|
+
|
|
515
|
+
Received: ${JSON.stringify(action)}`
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
if (action.trim().length === 0) {
|
|
519
|
+
logger4.error(`Empty action name for ${operation}`, { action });
|
|
520
|
+
throw new Error(
|
|
521
|
+
`[${operation}] Action name cannot be empty or whitespace only.
|
|
522
|
+
|
|
523
|
+
Received: "${action}"`
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
logger4.debug(`Action name validation passed for ${operation}`, { action });
|
|
527
|
+
};
|
|
528
|
+
var validateFacetName = (facet, operation) => {
|
|
529
|
+
if (!facet || typeof facet !== "string") {
|
|
530
|
+
logger4.error(`Invalid facet name for ${operation}`, { facet, type: typeof facet });
|
|
531
|
+
throw new Error(
|
|
532
|
+
`[${operation}] Facet name must be a non-empty string.
|
|
533
|
+
|
|
534
|
+
Received: ${JSON.stringify(facet)}`
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
if (facet.trim().length === 0) {
|
|
538
|
+
logger4.error(`Empty facet name for ${operation}`, { facet });
|
|
539
|
+
throw new Error(
|
|
540
|
+
`[${operation}] Facet name cannot be empty or whitespace only.
|
|
541
|
+
|
|
542
|
+
Received: "${facet}"`
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
logger4.debug(`Facet name validation passed for ${operation}`, { facet });
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
// src/errors.ts
|
|
549
|
+
var ValidationError = class extends Error {
|
|
550
|
+
fieldErrors;
|
|
551
|
+
constructor(message, fieldErrors) {
|
|
552
|
+
super(message);
|
|
553
|
+
this.name = "ValidationError";
|
|
554
|
+
this.fieldErrors = fieldErrors;
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
// src/schema.ts
|
|
559
|
+
async function validateSchema(data, schema) {
|
|
560
|
+
if (!schema) {
|
|
561
|
+
return data;
|
|
562
|
+
}
|
|
563
|
+
try {
|
|
564
|
+
if (schema.parseAsync) {
|
|
565
|
+
return await schema.parseAsync(data);
|
|
566
|
+
}
|
|
567
|
+
const result = schema.safeParse(data);
|
|
568
|
+
if (result.success) {
|
|
569
|
+
return result.data;
|
|
570
|
+
} else {
|
|
571
|
+
throw result.error;
|
|
572
|
+
}
|
|
573
|
+
} catch (error) {
|
|
574
|
+
if (error && Array.isArray(error.issues)) {
|
|
575
|
+
const fieldErrors = error.issues.map((issue) => ({
|
|
576
|
+
path: issue.path,
|
|
577
|
+
message: issue.message,
|
|
578
|
+
code: issue.code
|
|
579
|
+
}));
|
|
580
|
+
const validationError = new ValidationError(
|
|
581
|
+
"Schema validation failed",
|
|
582
|
+
fieldErrors
|
|
583
|
+
);
|
|
584
|
+
throw validationError;
|
|
585
|
+
}
|
|
586
|
+
if (error instanceof ValidationError) {
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
throw new ValidationError(`Validation failed: ${error.message || "Unknown error"}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
export {
|
|
593
|
+
ValidationError,
|
|
594
|
+
isValidLocations,
|
|
595
|
+
validateActionName,
|
|
596
|
+
validateComKey,
|
|
597
|
+
validateFacetName,
|
|
598
|
+
validateFinderName,
|
|
599
|
+
validateKey,
|
|
600
|
+
validateKeys,
|
|
601
|
+
validateLocations,
|
|
602
|
+
validateOperationParams,
|
|
603
|
+
validatePK,
|
|
604
|
+
validatePriKey,
|
|
605
|
+
validateQuery,
|
|
606
|
+
validateSchema
|
|
607
|
+
};
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SchemaValidator } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Validates data against a schema validator (Zod, Yup, etc.)
|
|
4
|
+
*
|
|
5
|
+
* Supports both synchronous and asynchronous validation.
|
|
6
|
+
* Transforms validator errors into Fjell ValidationError with FieldError[].
|
|
7
|
+
*
|
|
8
|
+
* @template T - The validated/parsed type
|
|
9
|
+
* @param data - The data to validate
|
|
10
|
+
* @param schema - Optional schema validator
|
|
11
|
+
* @returns Promise resolving to validated/parsed data
|
|
12
|
+
* @throws ValidationError if validation fails
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { validateSchema } from '@fjell/validation';
|
|
17
|
+
* import { z } from 'zod';
|
|
18
|
+
*
|
|
19
|
+
* const schema = z.object({ name: z.string() });
|
|
20
|
+
* const validated = await validateSchema({ name: 'Alice' }, schema);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function validateSchema<T>(data: unknown, schema?: SchemaValidator<T>): Promise<T>;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared validation types and options
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Options for validation functions
|
|
6
|
+
*/
|
|
7
|
+
export interface ValidationOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Custom error message prefix
|
|
10
|
+
*/
|
|
11
|
+
errorPrefix?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Whether to allow undefined (treated as empty array for location validation)
|
|
14
|
+
*/
|
|
15
|
+
allowUndefined?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to throw errors or return validation results
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
throwOnError?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Result of a validation operation (for non-throwing mode)
|
|
24
|
+
*/
|
|
25
|
+
export interface ValidationResult {
|
|
26
|
+
/**
|
|
27
|
+
* Whether validation passed
|
|
28
|
+
*/
|
|
29
|
+
valid: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Error message if validation failed
|
|
32
|
+
*/
|
|
33
|
+
error?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Detailed validation context
|
|
36
|
+
*/
|
|
37
|
+
context?: Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Generic interface for schema validators (Zod, Yup, Joi, etc.)
|
|
41
|
+
*
|
|
42
|
+
* This interface enables duck typing - any validator that implements
|
|
43
|
+
* these methods will work with validateSchema.
|
|
44
|
+
*
|
|
45
|
+
* @template T - The validated/parsed type
|
|
46
|
+
*/
|
|
47
|
+
export interface SchemaValidator<T> {
|
|
48
|
+
/**
|
|
49
|
+
* Synchronous parse method.
|
|
50
|
+
* Should throw an error if invalid, or return the parsed data.
|
|
51
|
+
*/
|
|
52
|
+
parse: (data: unknown) => T;
|
|
53
|
+
/**
|
|
54
|
+
* Safe parse method that returns a result object instead of throwing.
|
|
55
|
+
* Used as fallback if parseAsync is not available.
|
|
56
|
+
*/
|
|
57
|
+
safeParse: (data: unknown) => {
|
|
58
|
+
success: true;
|
|
59
|
+
data: T;
|
|
60
|
+
} | {
|
|
61
|
+
success: false;
|
|
62
|
+
error: any;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Optional asynchronous parse method.
|
|
66
|
+
* Preferred over parse/safeParse if available.
|
|
67
|
+
*/
|
|
68
|
+
parseAsync?: (data: unknown) => Promise<T>;
|
|
69
|
+
}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { ComKey, PriKey } from "@fjell/types";
|
|
2
|
+
export declare const isComKey: (key: any) => key is ComKey<any, any, any, any, any, any>;
|
|
3
|
+
export declare const isPriKey: (key: any) => key is PriKey<any>;
|
|
4
|
+
export declare const toKeyTypeArray: <S extends string, L1 extends string = never, L2 extends string = never, L3 extends string = never, L4 extends string = never, L5 extends string = never>(ik: ComKey<S, L1, L2, L3, L4, L5> | PriKey<S>) => string[];
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fjell/validation",
|
|
3
|
+
"description": "Validation Logic for Fjell Items and Keys",
|
|
4
|
+
"version": "4.4.0",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"validation",
|
|
7
|
+
"fjell"
|
|
8
|
+
],
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"scripts": {
|
|
21
|
+
"lint": "eslint . --ext .ts --fix",
|
|
22
|
+
"dev": "concurrently \"tsc --noEmit --watch\" \"node build.js --watch\"",
|
|
23
|
+
"build": "npm run lint && node build.js",
|
|
24
|
+
"clean": "rm -rf dist",
|
|
25
|
+
"test": "npm run lint && vitest run --coverage",
|
|
26
|
+
"precommit": "npm run clean && npm run lint && npm run build && npm run test",
|
|
27
|
+
"prepublishOnly": "npm run clean && npm run build"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@fjell/logging": "^4.4.65",
|
|
31
|
+
"@fjell/types": "4.4.4"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
35
|
+
"@eslint/js": "^9.39.1",
|
|
36
|
+
"@fjell/common-config": "^1.1.36",
|
|
37
|
+
"@swc/core": "^1.15.2",
|
|
38
|
+
"@tsconfig/recommended": "^1.0.13",
|
|
39
|
+
"@types/luxon": "^3.7.1",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^8.46.4",
|
|
41
|
+
"@typescript-eslint/parser": "^8.46.4",
|
|
42
|
+
"@vitest/coverage-v8": "4.0.9",
|
|
43
|
+
"concurrently": "^9.2.1",
|
|
44
|
+
"esbuild": "0.25.11",
|
|
45
|
+
"eslint": "^9.38.0",
|
|
46
|
+
"typescript": "^5.9.3",
|
|
47
|
+
"vitest": "4.0.9"
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/getfjell/validation.git"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|