perfect-payload 1.2.3 → 1.3.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.
Files changed (3) hide show
  1. package/README.md +919 -340
  2. package/index.js +41 -0
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -1280,6 +1280,47 @@ function perfectPayloadStructured(
1280
1280
 
1281
1281
  break;
1282
1282
 
1283
+ // ==================================================
1284
+ // CUSTOM VALIDATOR
1285
+ // ==================================================
1286
+
1287
+ case "customValidator":
1288
+ if (addNextError && attrExist && attributeValue !== null) {
1289
+ const validator = attributeRules?.[ruleName];
1290
+
1291
+ if (typeof validator !== "function") {
1292
+ throw new Error(
1293
+ `perfect-payload:- customValidator must be a function for attribute ${attributePath}`,
1294
+ );
1295
+ }
1296
+
1297
+ const validationResult = validator(attributeValue, data);
1298
+
1299
+ if (
1300
+ validationResult &&
1301
+ typeof validationResult.then === "function"
1302
+ ) {
1303
+ throw new Error(
1304
+ `perfect-payload:- customValidator must be synchronous for attribute ${attributePath}`,
1305
+ );
1306
+ }
1307
+
1308
+ if (validationResult !== true) {
1309
+ addStructuredError(
1310
+ rowErrors,
1311
+ attributePath,
1312
+ attributeRules?.["customValidatorCode"] ||
1313
+ "CUSTOM_VALIDATION_FAILED",
1314
+ attributeRules?.["customValidatorError"] ||
1315
+ `Custom validation failed for attribute ${attributePath}`,
1316
+ );
1317
+
1318
+ addNextError = false;
1319
+ }
1320
+ }
1321
+
1322
+ break;
1323
+
1283
1324
  default:
1284
1325
  break;
1285
1326
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "perfect-payload",
3
- "version": "1.2.3",
3
+ "version": "1.3.0",
4
4
  "type": "module",
5
5
  "description": "Lightweight JSON payload validation library with structured errors, nested validation, field paths, and customizable validation rules.",
6
6
  "main": "index.js",