@carecard/validate 3.1.25 → 3.1.27

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.
Files changed (3) hide show
  1. package/index.d.ts +115 -75
  2. package/package.json +1 -1
  3. package/readme.md +4 -3
package/index.d.ts CHANGED
@@ -1,14 +1,27 @@
1
+ /**
2
+ * Runtime-compatible TypeScript declarations for the CommonJS
3
+ * `@carecard/validate` package.
4
+ */
5
+
6
+ export type ValidatePropertiesInput = Record<string, unknown> | null | undefined;
7
+ export type ValidatePropertiesResult = Record<string, unknown>;
8
+ export type BoolValidator = (input: unknown) => boolean;
9
+ export type FailureMessageValidator = (input: unknown) => string | null;
10
+ export type InStringArrayValidator = (stringArray: readonly string[], input: unknown) => boolean;
11
+
1
12
  /**
2
13
  * Utility function to validate multiple properties of an object at once.
3
14
  */
4
- export function validateProperties(obj?: Record<string, any>): Record<string, any>;
15
+ export function validateProperties(obj?: ValidatePropertiesInput): ValidatePropertiesResult;
16
+
17
+ export type ValidateWhitelistFlattenKeyStyle = 'path' | 'leaf';
5
18
 
6
19
  /**
7
20
  * Options for {@link validateWhitelistProperties}.
8
21
  */
9
22
  export interface ValidateWhitelistPropertiesOptions {
10
23
  /** Properties allowed in the input but not required. */
11
- optionalProperties?: string[];
24
+ optionalProperties?: readonly string[] | null;
12
25
  /** When true, the returned object's keys are converted to snake_case. */
13
26
  convertToSnakeCase?: boolean;
14
27
  /**
@@ -24,46 +37,74 @@ export interface ValidateWhitelistPropertiesOptions {
24
37
  *
25
38
  * Defaults to `path`.
26
39
  */
27
- flattenKeyStyle?: 'path' | 'leaf';
40
+ flattenKeyStyle?: ValidateWhitelistFlattenKeyStyle;
28
41
  }
29
42
 
30
- /**
31
- * Validates and transforms whitelisted properties from an input object.
32
- *
33
- * - Supports nested objects via dot-notation paths (e.g. `"address.city"`),
34
- * up to 5 levels deep. The function checks that each path resolves to an
35
- * existing leaf property and validates the leaf value by its leaf segment.
36
- * - Extracts only the whitelisted (required + optional) leaf properties and
37
- * rebuilds the same nested shape in the result.
38
- * - Validates values via {@link validateProperties}.
39
- * - Throws a "Bad_Input" error when any required property is missing/invalid,
40
- * when a provided optional property has an invalid value, when a path
41
- * exceeds 5 levels of nesting, or when the combined count of
42
- * `requiredProperties` and `options.optionalProperties` exceeds 5000.
43
- * - Array values are supported: if a leaf value is an array, the per-leaf
44
- * validator is applied to each element. The leaf is accepted only when every
45
- * element passes validation, and the returned value is an array of the
46
- * validated elements (e.g. `{ name: ["First", "Other"] }` is validated like
47
- * `{ name: "First" }` and `{ name: "Other" }` individually).
48
- * - Optionally converts the resulting keys (including nested keys) to snake_case.
49
- * - Optionally flattens the result after snake_case conversion.
50
- *
51
- * @param inputObject The input object (e.g. `req.body` or `req.params`).
52
- * @param requiredProperties Leaf paths that must be present and valid. Dot-notation supported.
53
- * @param options Optional additional leaf paths plus output transformation flags.
54
- */
55
- export function validateWhitelistProperties(
56
- inputObject: Record<string, any>,
57
- requiredProperties?: string[],
58
- options?: ValidateWhitelistPropertiesOptions,
59
- ): Promise<Record<string, any>>;
43
+ export interface ValidateWhitelistPropertiesFunction {
44
+ /**
45
+ * Validates and transforms whitelisted properties from an input object.
46
+ *
47
+ * - Supports nested objects via dot-notation paths (e.g. `"address.city"`),
48
+ * up to 5 levels deep. The function checks that each path resolves to an
49
+ * existing leaf property and validates the leaf value by its leaf segment.
50
+ * - Extracts only the whitelisted (required + optional) leaf properties and
51
+ * rebuilds the same nested shape in the result.
52
+ * - Validates values via {@link validateProperties}.
53
+ * - Throws a "Bad_Input" error when any required property is missing/invalid,
54
+ * when a provided optional property has an invalid value, when a path
55
+ * exceeds 5 levels of nesting, or when the combined count of
56
+ * `requiredProperties` and `options.optionalProperties` exceeds 5000.
57
+ * - Array values are supported: if a leaf value is an array, the per-leaf
58
+ * validator is applied to each element. The leaf is accepted only when every
59
+ * element passes validation, and the returned value is an array of the
60
+ * validated elements.
61
+ * - Optionally converts the resulting keys, including nested keys, to snake_case.
62
+ * - Optionally flattens the result after snake_case conversion.
63
+ *
64
+ * @param inputObject The input object, for example `req.body` or `req.params`.
65
+ * @param requiredProperties Leaf paths that must be present and valid. Dot notation is supported.
66
+ * @param options Optional additional leaf paths plus output transformation flags.
67
+ */
68
+ (
69
+ inputObject?: ValidatePropertiesInput,
70
+ requiredProperties?: readonly string[] | null,
71
+ options?: ValidateWhitelistPropertiesOptions | null,
72
+ ): Promise<ValidatePropertiesResult>;
73
+ validateWhitelistProperties: ValidateWhitelistPropertiesFunction;
74
+ MAX_NESTING_DEPTH: 5;
75
+ MAX_KEYS_PER_CALL: 5000;
76
+ }
77
+
78
+ export const validateWhitelistProperties: ValidateWhitelistPropertiesFunction;
79
+
80
+ export type UserRoleRequestRole = 'student' | 'intern' | 'volunteer';
81
+ export type UserRoleRequestScopeRequirement = boolean | typeof REQUIRE_SCOPE_WHEN_ROLE_OR_SCOPE_PRESENT;
60
82
 
61
83
  export const DEFAULT_USER_ROLE_REQUEST_ROLE: 'student';
62
84
  export const REQUIRE_SCOPE_WHEN_ROLE_OR_SCOPE_PRESENT: 'whenRoleOrScopePresent';
63
85
 
64
86
  export interface ValidateNewUserRoleRequestOptions {
65
- defaultRole?: 'student' | undefined;
66
- requireScope?: boolean | typeof REQUIRE_SCOPE_WHEN_ROLE_OR_SCOPE_PRESENT;
87
+ defaultRole?: UserRoleRequestRole | undefined;
88
+ requireScope?: UserRoleRequestScopeRequirement;
89
+ }
90
+
91
+ export interface ValidateNewUserRoleRequestInput extends Record<string, unknown> {
92
+ role_name?: unknown;
93
+ roleName?: unknown;
94
+ role?: unknown;
95
+ institution_id?: unknown;
96
+ institutionId?: unknown;
97
+ campus_id?: unknown;
98
+ campusId?: unknown;
99
+ program_id?: unknown;
100
+ programId?: unknown;
101
+ }
102
+
103
+ export interface ValidateNewUserRoleRequestPayload extends Record<string, unknown> {
104
+ role_name?: string;
105
+ institution_id?: string;
106
+ campus_id?: string;
107
+ program_id?: string;
67
108
  }
68
109
 
69
110
  /**
@@ -72,79 +113,78 @@ export interface ValidateNewUserRoleRequestOptions {
72
113
  * both institution_id and campus_id must be provided.
73
114
  */
74
115
  export function validateNewUserRoleRequestObject(
75
- roleRequest?: Record<string, any>,
76
- options?: ValidateNewUserRoleRequestOptions,
77
- ): Record<string, any>;
116
+ roleRequest?: ValidateNewUserRoleRequestInput | null,
117
+ options?: ValidateNewUserRoleRequestOptions | null,
118
+ ): ValidateNewUserRoleRequestPayload;
78
119
 
79
120
  /** Checks if the string is a valid image URL format. */
80
- export function isImageUrl(imageUrl: any): boolean;
121
+ export const isImageUrl: BoolValidator;
81
122
  /** Checks if the value is an integer. */
82
- export function isInteger(number: any): boolean;
123
+ export const isInteger: BoolValidator;
83
124
  /** Checks if the string is a valid JSON string and can be parsed into an object. */
84
- export function isValidJsonString(str: any): boolean;
125
+ export const isValidJsonString: BoolValidator;
85
126
  /** Checks if the string represents a valid integer. */
86
- export function isValidIntegerString(str: any): boolean;
127
+ export const isValidIntegerString: BoolValidator;
87
128
  /** Checks if the string is a valid UUID. */
88
- export function isValidUuidString(str: any): boolean;
129
+ export const isValidUuidString: BoolValidator;
89
130
  /** Checks if the string contains only alphanumeric characters, spaces, underscores, or hyphens. */
90
- export function isCharactersString(str: any): boolean;
131
+ export const isCharactersString: BoolValidator;
91
132
  /** Checks if the string is a valid street address format. */
92
- export function isStreetString(str: any): boolean;
133
+ export const isStreetString: BoolValidator;
93
134
  /** Checks if the string is a valid name format. */
94
- export function isNameString(str: any): boolean;
135
+ export const isNameString: BoolValidator;
95
136
  /** Checks if the string is safe for search queries. */
96
- export function isSafeSearchString(str: any): boolean;
137
+ export const isSafeSearchString: BoolValidator;
97
138
  /** Checks if the string is a valid email address. */
98
- export function isEmailString(email: any): boolean;
139
+ export const isEmailString: BoolValidator;
99
140
  /** Checks if the string is a valid JWT format. */
100
- export function isJwtString(jwt: any): boolean;
141
+ export const isJwtString: BoolValidator;
101
142
  /** Checks if the string is a valid strong password. */
102
- export function isPasswordString(password: any): boolean;
143
+ export const isPasswordString: BoolValidator;
103
144
  /** Checks if the string is a valid simple password. */
104
- export function isSimplePasswordString(password: any): boolean;
145
+ export const isSimplePasswordString: BoolValidator;
105
146
  /** Returns a failure message if the password is not strong enough. */
106
- export function isPasswordStringFailureMessage(password: any): string | null;
147
+ export const isPasswordStringFailureMessage: FailureMessageValidator;
107
148
  /** Returns a failure message if the password is not valid as a simple password. */
108
- export function isSimplePasswordStringFailureMessage(password: any): string | null;
149
+ export const isSimplePasswordStringFailureMessage: FailureMessageValidator;
109
150
  /** Checks if the string is a valid username. */
110
- export function isUsernameString(str: any): boolean;
151
+ export const isUsernameString: BoolValidator;
111
152
  /** Checks if the string is a valid phone number. */
112
- export function isPhoneNumber(str: any): boolean;
153
+ export const isPhoneNumber: BoolValidator;
113
154
  /** Checks if the string is URL-safe. */
114
- export function isUrlSafeString(inputString: any): boolean;
155
+ export const isUrlSafeString: BoolValidator;
115
156
  /** Checks if the string length is between 6 and 24 characters. */
116
- export function isString6To24CharacterLong(password: any): boolean;
157
+ export const isString6To24CharacterLong: BoolValidator;
117
158
  /** Checks if the string length is between 6 and 16 characters. */
118
- export function isString6To16CharacterLong(password: any): boolean;
159
+ export const isString6To16CharacterLong: BoolValidator;
119
160
  /** Checks if the string is a valid Canadian province abbreviation (ON, QC). */
120
- export function isProvinceString(inputString: any): boolean;
161
+ export const isProvinceString: BoolValidator;
121
162
  /** Checks if the value is a boolean. */
122
- export function isBoolValue(inputValue: any): boolean;
163
+ export const isBoolValue: BoolValidator;
123
164
  /** Checks if the string is a valid Canadian postal code. */
124
- export function isPostalCodeString(inputString: any): boolean;
165
+ export const isPostalCodeString: BoolValidator;
125
166
  /** Checks if the string contains only allowed "safe" characters. */
126
- export function isSafeString(str: any): boolean;
167
+ export const isSafeString: BoolValidator;
127
168
  /** Checks if the value is non-empty text up to the supported maximum length. */
128
- export function isTextString(str: any): boolean;
129
- /** Checks if a string exists within a given array of strings (case-insensitive). */
130
- export function isInStringArray(StringArray: string[], inputString: any): boolean;
169
+ export const isTextString: BoolValidator;
170
+ /** Checks if a string exists within a given array of strings, case-insensitive. */
171
+ export const isInStringArray: InStringArrayValidator;
131
172
  /** Checks if the string is one of the supported user role request statuses. */
132
- export function isUserRoleRequestStatusString(inputString: any): boolean;
173
+ export const isUserRoleRequestStatusString: BoolValidator;
133
174
  /** Checks if the string is a supported new user role request role. */
134
- export function isUserRoleRequestRoleString(inputString: any): boolean;
135
- /** Checks if the string is a valid country code (e.g., +1). */
136
- export function isCountryCodeString(str: any): boolean;
175
+ export const isUserRoleRequestRoleString: BoolValidator;
176
+ /** Checks if the string is a valid country code, for example +1. */
177
+ export const isCountryCodeString: BoolValidator;
137
178
  /** Checks if the string is a valid domain name. */
138
- export function isValidDomainName(domain: any): boolean;
179
+ export const isValidDomainName: BoolValidator;
139
180
  /** Checks if the string is a valid ISO 8601 timestamp with time zone. */
140
- export function isValidTimestampzString(str: any): boolean;
181
+ export const isValidTimestampzString: BoolValidator;
141
182
  /** Checks if the string is a valid ISO 8601 timestamp without time zone. */
142
- export function isValidTimestampString(str: any): boolean;
143
-
183
+ export const isValidTimestampString: BoolValidator;
144
184
  /** Checks if the string is a valid URL. */
145
- export function isValidUrl(url: any): boolean;
185
+ export const isValidUrl: BoolValidator;
146
186
  /** Checks if the array contains only safe strings. */
147
- export function isValidArrayOfStrings(arr: any): boolean;
187
+ export const isValidArrayOfStrings: BoolValidator;
148
188
 
149
189
  /**
150
190
  * Utility functions for validating various types of strings and values.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carecard/validate",
3
- "version": "3.1.25",
3
+ "version": "3.1.27",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/CareCard-ca/pkg-validate.git"
package/readme.md CHANGED
@@ -121,7 +121,7 @@ where the package supports both.
121
121
 
122
122
  `validateWhitelistProperties` extracts only the required and optional property
123
123
  paths you provide, validates each leaf through `validateProperties`, and returns
124
- a `Promise<Record<string, any>>` with the sanitized output.
124
+ a `Promise<ValidatePropertiesResult>` with the sanitized output.
125
125
 
126
126
  ```js
127
127
  const body = {
@@ -333,10 +333,11 @@ await validateWhitelistProperties(input, ['name', 'user.name', 'user.email'], {
333
333
  The package ships `index.d.ts` and declares types for the CommonJS exports.
334
334
 
335
335
  ```ts
336
- import { validateWhitelistProperties, isEmailString } from '@carecard/validate';
336
+ import { validateWhitelistProperties, isEmailString, ValidatePropertiesResult } from '@carecard/validate';
337
337
 
338
338
  const valid: boolean = isEmailString('jane@example.com');
339
- const output: Record<string, any> = await validateWhitelistProperties({ first_name: 'Jane' }, ['first_name']);
339
+ const output: ValidatePropertiesResult = await validateWhitelistProperties({ first_name: 'Jane' }, ['first_name']);
340
+ const maxDepth: 5 = validateWhitelistProperties.MAX_NESTING_DEPTH;
340
341
  ```
341
342
 
342
343
  ## Project Layout