kanun 0.1.7 → 1.0.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.
package/dist/index.d.ts CHANGED
@@ -279,6 +279,33 @@ declare class ErrorBag {
279
279
  clone(): ErrorBag;
280
280
  }
281
281
  //#endregion
282
+ //#region src/Rules/registerRule.d.ts
283
+ declare function register(rule: string, validate: (value: any, parameters?: string[], attribute?: string) => boolean | Promise<boolean>, replaceMessage?: (message: string, paramters: string[], data?: object, getDisplayableAttribute?: GenericCallable) => string): boolean;
284
+ declare function registerImplicit(rule: string, validate: (value: any, parameters?: string[] | number[], attribute?: string) => boolean | Promise<boolean>, replaceMessage?: (message: string, paramters: string[], data?: object, getDisplayableAttribute?: GenericCallable) => string): void;
285
+ //#endregion
286
+ //#region src/Plugin.d.ts
287
+ interface ValidationValueInspector {
288
+ type: string;
289
+ matches: (value: any) => boolean;
290
+ size?: (value: any) => number;
291
+ }
292
+ interface ValidatorPluginApi {
293
+ registerRule: typeof register;
294
+ registerImplicitRule: typeof registerImplicit;
295
+ registerValueInspector: (inspector: ValidationValueInspector) => void;
296
+ extendTranslations: (translations: GenericObject) => void;
297
+ }
298
+ interface ValidatorPlugin {
299
+ name: string;
300
+ install: (api: ValidatorPluginApi) => void;
301
+ }
302
+ declare function definePlugin(plugin: ValidatorPlugin): ValidatorPlugin;
303
+ declare function usePlugin(plugin: ValidatorPlugin): void;
304
+ declare function registerValueInspector(inspector: ValidationValueInspector): void;
305
+ declare function getValidationValueInspector(value: any): ValidationValueInspector | undefined;
306
+ declare function getValidationMessageType(value: any, hasNumericRule?: boolean): string;
307
+ declare function getValidationSize(value: any, hasNumericRule?: boolean): number;
308
+ //#endregion
282
309
  //#region src/BaseValidator.d.ts
283
310
  declare class BaseValidator<D extends GenericObject = GenericObject> {
284
311
  /**
@@ -321,15 +348,68 @@ declare class BaseValidator<D extends GenericObject = GenericObject> {
321
348
  * Object of custom attribute name;
322
349
  */
323
350
  customAttributes: CustomAttributes<D>;
351
+ /**
352
+ * Arbitrary per-validator context for plugins.
353
+ */
354
+ private context;
324
355
  constructor(data: D, rules: InitialRules<D>, customMessages?: CustomMessages<D>, customAttributes?: CustomAttributes<D>);
356
+ static use(plugin: ValidatorPlugin): typeof BaseValidator;
357
+ static useContext(context?: GenericObject): typeof BaseValidator;
358
+ use(plugin: ValidatorPlugin): this;
325
359
  setData<ND extends GenericObject>(data: ND): BaseValidator<ND>;
326
360
  setRules(rules: InitialRules<D>): this;
327
361
  setLang(lang: string): this;
362
+ /**
363
+ * Set the validator's context.
364
+ */
365
+ withContext(context?: GenericObject): this;
366
+ /**
367
+ * Get the validator's context.
368
+ * This is useful for custom rules that need access to additional data or services.
369
+ *
370
+ * @returns The current context object
371
+ */
372
+ getContext(): GenericObject;
373
+ /**
374
+ * Get the current language used by the validator.
375
+ *
376
+ * @returns
377
+ */
328
378
  getLang(): string;
379
+ /**
380
+ * Set custom error messages for the validator.
381
+ *
382
+ * @param customMessages
383
+ * @returns
384
+ */
329
385
  setCustomMessages(customMessages?: CustomMessages<D>): this;
386
+ /**
387
+ * Set custom attribute names for the validator.
388
+ *
389
+ * @param customAttributes
390
+ * @returns
391
+ */
330
392
  setCustomAttributes(customAttributes?: CustomAttributes<D>): this;
393
+ /**
394
+ * Set whether the validator should stop validating after the first failure.
395
+ *
396
+ * @param stopOnFirstFailure
397
+ * @returns
398
+ */
331
399
  stopOnFirstFailure(stopOnFirstFailure?: boolean): this;
400
+ /**
401
+ * Get the error messages related to the validation.
402
+ *
403
+ * @returns
404
+ */
332
405
  errors(): ErrorBag;
406
+ /**
407
+ * Clear the error messages for the given keys.
408
+ * If no keys are provided, all error messages will be cleared.
409
+ *
410
+ * @param keys The keys of the error messages to clear
411
+ * @returns The updated ErrorBag instance
412
+ */
333
413
  clearErrors(keys?: string[]): ErrorBag;
334
414
  /**
335
415
  * Create a new ErrorBag instance and set the custom errors, thus removing previous error messages
@@ -349,6 +429,9 @@ declare class BaseValidator<D extends GenericObject = GenericObject> {
349
429
  validateAsync(key?: string, value?: any): Promise<boolean>;
350
430
  /**
351
431
  * Get the displayable name of the attribute.
432
+ *
433
+ * @param attribute
434
+ * @returns
352
435
  */
353
436
  getDisplayableAttribute(attribute: string): string;
354
437
  private addCustomErrors;
@@ -653,8 +736,13 @@ declare class RequiredIf extends BaseRule {
653
736
  }
654
737
  //#endregion
655
738
  //#region src/Contracts/ValidationRuleName.d.ts
656
- type ParamableValidationRuleName = 'accepted_if' | 'after' | 'after_or_equal' | 'before' | 'before_or_equal' | 'between' | 'date_equals' | 'datetime' | 'declined_if' | 'digits_between' | 'different' | 'exists' | 'ends_with' | 'gt' | 'gte' | 'in' | 'includes' | 'lt' | 'lte' | 'max' | 'min' | 'not_in' | 'not_includes' | 'required_if' | 'required_unless' | 'required_with' | 'required_with_all' | 'required_without' | 'required_without_all' | 'same' | 'size' | 'starts_with' | 'unique';
657
- type PlainRuleName = 'accepted' | 'alpha' | 'alpha_dash' | 'alpha_num' | 'array' | 'array_unique' | 'bail' | 'boolean' | 'confirmed' | 'date' | 'declined' | 'digits' | 'email' | 'integer' | 'json' | 'not_regex' | 'nullable' | 'numeric' | 'object' | 'present' | 'regex' | 'required' | 'sometimes' | 'string' | 'url' | 'hex' | 'uuid';
739
+ interface CustomValidationRuleNameMap {
740
+ [key: string]: any;
741
+ }
742
+ type CustomParamableValidationRuleName = Extract<{ [K in keyof CustomValidationRuleNameMap]: CustomValidationRuleNameMap[K] extends 'paramable' ? K : never }[keyof CustomValidationRuleNameMap], string>;
743
+ type CustomPlainRuleName = Extract<{ [K in keyof CustomValidationRuleNameMap]: CustomValidationRuleNameMap[K] extends 'plain' ? K : never }[keyof CustomValidationRuleNameMap], string>;
744
+ type ParamableValidationRuleName = 'accepted_if' | 'after' | 'after_or_equal' | 'before' | 'before_or_equal' | 'between' | 'date_equals' | 'datetime' | 'declined_if' | 'digits_between' | 'different' | 'exists' | 'ends_with' | 'gt' | 'gte' | 'in' | 'includes' | 'lt' | 'lte' | 'max' | 'min' | 'not_in' | 'not_includes' | 'required_if' | 'required_unless' | 'required_with' | 'required_with_all' | 'required_without' | 'required_without_all' | 'same' | 'size' | 'starts_with' | 'unique' | CustomParamableValidationRuleName;
745
+ type PlainRuleName = 'accepted' | 'alpha' | 'alpha_dash' | 'alpha_num' | 'array' | 'array_unique' | 'bail' | 'boolean' | 'confirmed' | 'date' | 'declined' | 'digits' | 'email' | 'integer' | 'json' | 'not_regex' | 'nullable' | 'numeric' | 'object' | 'present' | 'regex' | 'required' | 'sometimes' | 'string' | 'url' | 'hex' | 'uuid' | CustomPlainRuleName;
658
746
  type ValidationRuleName = ParamableValidationRuleName | PlainRuleName;
659
747
  type MethodRules = Regex | In | NotIn | RequiredIf;
660
748
  /**
@@ -695,6 +783,8 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
695
783
  */
696
784
  static make<D extends Record<string, any>, R extends RulesForData<D>>(data: D, rules: R, messages: Partial<Record<MessagesForRules<R>, string>>): IValidator<D, R>;
697
785
  static useDatabase(driver: IDatabaseDriver): typeof IValidator;
786
+ static use(plugin: ValidatorPlugin): typeof IValidator;
787
+ static useContext(context: Record<string, any>): typeof IValidator;
698
788
  /**
699
789
  * Run the validator and store results.
700
790
  */
@@ -719,6 +809,9 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
719
809
  * Stop validation on first failure.
720
810
  */
721
811
  stopOnFirstFailure(): this;
812
+ use(plugin: ValidatorPlugin): this;
813
+ withContext(context: Record<string, any>): this;
814
+ getContext(): Record<string, any>;
722
815
  /**
723
816
  * Get the data that passed validation.
724
817
  */
@@ -777,6 +870,29 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
777
870
  getDatabaseDriver(): IDatabaseDriver | undefined;
778
871
  }
779
872
  //#endregion
873
+ //#region src/Context.d.ts
874
+ type ValidatorContext = Record<string, any>;
875
+ /**
876
+ * Get the current validator context.
877
+ *
878
+ * @returns
879
+ */
880
+ declare function getValidatorContext(): ValidatorContext;
881
+ /**
882
+ * Use a context for the current validator instance and all of its children.
883
+ *
884
+ * @param context
885
+ * @returns
886
+ */
887
+ declare function useValidatorContext(context?: ValidatorContext): ValidatorContext;
888
+ /**
889
+ * Run a function with a given validator context.
890
+ *
891
+ * @param context The context to use during the execution of the callback
892
+ * @param callback The function to execute with the given context
893
+ */
894
+ declare function runWithValidatorContext<T>(context: ValidatorContext, callback: () => T): T;
895
+ //#endregion
780
896
  //#region src/Rules/password.d.ts
781
897
  declare class Password$1 extends IRuleContract {
782
898
  /**
@@ -865,10 +981,6 @@ declare class Password$1 extends IRuleContract {
865
981
  static default(): IRuleContract | Password$1;
866
982
  }
867
983
  //#endregion
868
- //#region src/Rules/registerRule.d.ts
869
- declare function register(rule: string, validate: (value: any, parameters?: string[], attribute?: string) => boolean | Promise<boolean>, replaceMessage?: (message: string, paramters: string[], data?: object, getDisplayableAttribute?: GenericCallable) => string): boolean;
870
- declare function registerImplicit(rule: string, validate: (value: any, parameters?: string[] | number[], attribute?: string) => boolean | Promise<boolean>, replaceMessage?: (message: string, paramters: string[], data?: object, getDisplayableAttribute?: GenericCallable) => string): void;
871
- //#endregion
872
984
  //#region src/rule.d.ts
873
985
  declare function requiredIf(callback: boolean | CallableFunction): RequiredIf;
874
986
  declare function ruleIn(values: (string | number)[]): In;
@@ -1020,12 +1132,36 @@ declare class Validator<D extends Record<string, any> = any, R extends RulesForD
1020
1132
  private errorBagName;
1021
1133
  private registeredCustomRules;
1022
1134
  private shouldStopOnFirstFailure;
1135
+ private context;
1023
1136
  constructor(data: D, rules: R, messages?: Partial<Record<MessagesForRules<R>, string>>);
1024
1137
  /**
1025
1138
  * Validate the data and return the instance
1026
1139
  */
1027
1140
  static make<D extends Record<string, any>, R extends RulesForData<D>>(data: D, rules: R, messages?: Partial<Record<MessagesForRules<R>, string>>): Validator<D, R>;
1028
1141
  static useDatabase(driver: IDatabaseDriver): typeof Validator;
1142
+ /**
1143
+ * Set the validator's context.
1144
+ *
1145
+ * @param context
1146
+ * @returns
1147
+ */
1148
+ static useContext(context?: Record<string, any>): typeof Validator;
1149
+ /**
1150
+ * Register a plugin with the validator.
1151
+ * Plugins can add rules, messages, and even override existing rules and messages.
1152
+ *
1153
+ * @param plugin The plugin to register
1154
+ * @returns The Validator class for chaining
1155
+ */
1156
+ static use(plugin: ValidatorPlugin): typeof Validator;
1157
+ /**
1158
+ * Register a plugin with the validator.
1159
+ * Plugins can add rules, messages, and even override existing rules and messages.
1160
+ *
1161
+ * @param plugin The plugin to register
1162
+ * @returns The Validator instance for chaining
1163
+ */
1164
+ use(plugin: ValidatorPlugin): this;
1029
1165
  /**
1030
1166
  * Run the validator and store results.
1031
1167
  */
@@ -1050,6 +1186,21 @@ declare class Validator<D extends Record<string, any> = any, R extends RulesForD
1050
1186
  * Stop validation on first failure.
1051
1187
  */
1052
1188
  stopOnFirstFailure(): this;
1189
+ /**
1190
+ * Set the validator's context.
1191
+ * This is useful for custom rules that need access to additional data or services.
1192
+ *
1193
+ * @param context
1194
+ * @returns
1195
+ */
1196
+ withContext(context?: Record<string, any>): this;
1197
+ /**
1198
+ * Get the validator's context.
1199
+ * This is useful for custom rules that need access to additional data or services.
1200
+ *
1201
+ * @returns The current context object
1202
+ */
1203
+ getContext(): Record<string, any>;
1053
1204
  /**
1054
1205
  * Get the data that passed validation.
1055
1206
  */
@@ -1144,6 +1295,10 @@ declare class Lang {
1144
1295
  * Store the translations passed by the user
1145
1296
  */
1146
1297
  static translations: GenericObject;
1298
+ /**
1299
+ * Store translations contributed by plugins.
1300
+ */
1301
+ static translationExtensions: GenericObject;
1147
1302
  /**
1148
1303
  * Stores the messages that are already loaded
1149
1304
  */
@@ -1169,6 +1324,10 @@ declare class Lang {
1169
1324
  * @param translations
1170
1325
  */
1171
1326
  static setTranslationObject(translations: GenericObject): void;
1327
+ /**
1328
+ * Merge additional translations into the global catalog.
1329
+ */
1330
+ static extendTranslationObject(translations: GenericObject): void;
1172
1331
  /**
1173
1332
  * Set the default lang that should be used. And assign the default messages
1174
1333
  *
@@ -1194,6 +1353,7 @@ declare class Lang {
1194
1353
  * @returns
1195
1354
  */
1196
1355
  static load(lang: string): void;
1356
+ private static resetLoadedMessages;
1197
1357
  }
1198
1358
  //#endregion
1199
1359
  //#region src/Providers/ValidationServiceProvider.d.ts
@@ -1341,6 +1501,32 @@ declare function convertValuesToNull(values: string[]): (string | null)[];
1341
1501
  * @returns
1342
1502
  */
1343
1503
  declare const plural: (word: string, count: number) => string;
1504
+ /**
1505
+ * Determine if a value is an object. Arrays and null are not considered objects.
1506
+ *
1507
+ * @param value
1508
+ * @returns
1509
+ */
1510
+ declare function isObject(value: unknown): value is GenericObject;
1511
+ /**
1512
+ * Deeply merge two objects.
1513
+ * The source object will overwrite the target object when there is a conflict.
1514
+ * Arrays and non-object values will be overwritten, not merged.
1515
+ *
1516
+ * @param target
1517
+ * @param source
1518
+ * @returns
1519
+ */
1520
+ declare function mergeDeep(target: GenericObject, source: GenericObject): GenericObject;
1521
+ /**
1522
+ * Deeply find a value in an object using a dot-notated path.
1523
+ * Returns undefined if the path does not exist.
1524
+ *
1525
+ * @param obj
1526
+ * @param path
1527
+ * @returns
1528
+ */
1529
+ declare function deepFindMessage(obj: GenericObject, path: string): any;
1344
1530
  //#endregion
1345
1531
  //#region src/utilities/object.d.ts
1346
1532
  /**
@@ -1368,21 +1554,6 @@ declare function deepSet(target: any, path: string | string[], value: any): void
1368
1554
  * @returns
1369
1555
  */
1370
1556
  declare function dotify(obj: GenericObject, ignoreRulesArray?: boolean, withBaseObjectType?: boolean): GenericObject;
1371
- /**
1372
- * Check if the value is an object
1373
- *
1374
- * @param value
1375
- * @returns
1376
- */
1377
- declare function isObject(value: any): any;
1378
- /**
1379
- * Deeply merge nested objects
1380
- *
1381
- * @param target
1382
- * @param source
1383
- * @returns
1384
- */
1385
- declare function mergeDeep(target: GenericObject, source: GenericObject): GenericObject;
1386
1557
  /**
1387
1558
  * Check if objects are deep equal
1388
1559
  *
@@ -1397,6 +1568,7 @@ declare class ValidationException extends Error {
1397
1568
  validator: Validator<any, any>;
1398
1569
  response?: any;
1399
1570
  status: number;
1571
+ statusCode: number;
1400
1572
  errorBag: string;
1401
1573
  redirectTo?: string;
1402
1574
  name: string;
@@ -1441,4 +1613,4 @@ declare class ValidationException extends Error {
1441
1613
  getResponse(): any;
1442
1614
  }
1443
1615
  //#endregion
1444
- export { BaseValidationRuleClass, BaseValidator, CustomAttributes, CustomErrors, CustomMessages, CustomValidationRules, DotPaths, ErrorBag, ErrorMessage, Errors, ExtendedRules, ExtractRules, FieldMessages, GenericCallable, GenericObject, IDatabaseDriver, IMessageBag, IValidationRule, IValidator, ImplicitAttributes, ImplicitRule, InitialRule, InitialRules, Lang, MessageBag, Messages, MessagesForRules, NestedStringMap, ParamableValidationRuleName, Password, PlainRuleName, ReplaceAttributeInterface, Rules, RulesForData, TRule, ValidatedByRules, ValidationCallback, ValidationDataInterface, ValidationDatabaseExistsInput, ValidationException, ValidationMessageProvider, ValidationRule, ValidationRuleCallable, ValidationRuleName, ValidationRuleParserInterface, ValidationRuleSet, ValidationServiceProvider, Validator, addImplicitRule, buildValidationMethodName, compare, convertValuesToBoolean, convertValuesToNull, convertValuesToNumber, deepEqual, deepFind, deepSet, dotify, getFormattedAttribute, getKeyCombinations, getMessage, getNumericRules, getSize, isArrayOfRules, isImplicitRule, isInteger, isNumericRule, isObject, isRule, isSizeRule, make, mergeDeep, notRegex, plural, regex, register, registerImplicit, requiredIf, ruleIn, ruleNotIn, sameType, toDate, toSnakeCase };
1616
+ export { BaseValidationRuleClass, BaseValidator, CustomAttributes, CustomErrors, CustomMessages, CustomParamableValidationRuleName, CustomPlainRuleName, CustomValidationRuleNameMap, CustomValidationRules, DotPaths, ErrorBag, ErrorMessage, Errors, ExtendedRules, ExtractRules, FieldMessages, GenericCallable, GenericObject, IDatabaseDriver, IMessageBag, IValidationRule, IValidator, ImplicitAttributes, ImplicitRule, InitialRule, InitialRules, Lang, MessageBag, Messages, MessagesForRules, NestedStringMap, ParamableValidationRuleName, Password, PlainRuleName, ReplaceAttributeInterface, Rules, RulesForData, TRule, ValidatedByRules, ValidationCallback, ValidationDataInterface, ValidationDatabaseExistsInput, ValidationException, ValidationMessageProvider, ValidationRule, ValidationRuleCallable, ValidationRuleName, ValidationRuleParserInterface, ValidationRuleSet, ValidationServiceProvider, ValidationValueInspector, Validator, ValidatorPlugin, ValidatorPluginApi, addImplicitRule, buildValidationMethodName, compare, convertValuesToBoolean, convertValuesToNull, convertValuesToNumber, deepEqual, deepFind, deepFindMessage, deepSet, definePlugin, dotify, getFormattedAttribute, getKeyCombinations, getMessage, getNumericRules, getSize, getValidationMessageType, getValidationSize, getValidationValueInspector, getValidatorContext, isArrayOfRules, isImplicitRule, isInteger, isNumericRule, isObject, isRule, isSizeRule, make, mergeDeep, notRegex, plural, regex, register, registerImplicit, registerValueInspector, requiredIf, ruleIn, ruleNotIn, runWithValidatorContext, sameType, toDate, toSnakeCase, usePlugin, useValidatorContext };