kanun 0.1.3 → 0.1.4

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 (2) hide show
  1. package/dist/index.d.ts +30 -26
  2. package/package.json +1 -1
package/dist/index.d.ts CHANGED
@@ -73,17 +73,20 @@ declare class replaceAttributePayload {
73
73
  //#region src/Contracts/BaseContract.d.ts
74
74
  type InitialRule = string | ValidationCallback | IRuleContract | BaseRule;
75
75
  type TRule = string | IRuleContract;
76
- interface InitialRules extends GenericObject {
77
- [key: string]: InitialRule | InitialRule[];
78
- }
79
- interface Rules extends GenericObject {
80
- [key: string]: TRule[];
81
- }
76
+ type NestedStringMap = {
77
+ [key: string]: string | NestedStringMap;
78
+ };
79
+ type RuleLeaf = InitialRule | InitialRule[];
80
+ type KnownRulePaths<X extends GenericObject, Prefix extends string = ''> = { [K in keyof X & string]: X[K] extends (infer A)[] ? `${Prefix}${K}` | `${Prefix}${K}.*` | (A extends GenericObject ? `${Prefix}${K}.*.${KnownRulePaths<A>}` : never) : X[K] extends GenericObject ? `${Prefix}${K}` | `${Prefix}${K}.${KnownRulePaths<X[K]>}` : `${Prefix}${K}` }[keyof X & string];
81
+ type LooseNestedRulePaths<X extends GenericObject> = { [K in keyof X & string]: X[K] extends (infer _A)[] ? `${K}.*.${string}` : X[K] extends GenericObject ? `${K}.${string}` : never }[keyof X & string];
82
+ type ScopedInitialRules<X extends GenericObject> = Partial<Record<KnownRulePaths<X> | LooseNestedRulePaths<X>, RuleLeaf>> & { [K in keyof X & string]?: X[K] extends (infer A)[] ? RuleLeaf | (A extends GenericObject ? ScopedInitialRules<A> : never) : X[K] extends GenericObject ? RuleLeaf | ScopedInitialRules<X[K]> : RuleLeaf };
83
+ type InitialRules<X extends GenericObject = GenericObject> = ScopedInitialRules<keyof X extends never ? GenericObject : X>;
84
+ type Rules<X extends GenericObject = GenericObject> = Partial<Record<Extract<keyof X, string>, TRule[]>> & Record<string, TRule[]>;
82
85
  interface ImplicitAttributes {
83
86
  [key: string]: string[];
84
87
  }
85
- type CustomMessages = GenericObject & {};
86
- type CustomAttributes = GenericObject & {};
88
+ type CustomMessages<_X extends GenericObject = GenericObject> = GenericObject;
89
+ type CustomAttributes<_X extends GenericObject = GenericObject> = GenericObject;
87
90
  interface ErrorMessage {
88
91
  error_type?: string;
89
92
  message: string;
@@ -272,7 +275,7 @@ declare class ErrorBag {
272
275
  }
273
276
  //#endregion
274
277
  //#region src/BaseValidator.d.ts
275
- declare class BaseValidator {
278
+ declare class BaseValidator<D extends GenericObject = GenericObject> {
276
279
  /**
277
280
  * The lang used to return error messages
278
281
  */
@@ -308,18 +311,18 @@ declare class BaseValidator {
308
311
  /**
309
312
  * Custom messages returned based on the error
310
313
  */
311
- customMessages: CustomMessages;
314
+ customMessages: CustomMessages<D>;
312
315
  /**
313
316
  * Object of custom attribute name;
314
317
  */
315
- customAttributes: CustomAttributes;
316
- constructor(data: object, rules: InitialRules, customMessages?: CustomMessages, customAttributes?: CustomAttributes);
317
- setData(data: object): this;
318
- setRules(rules: InitialRules): this;
318
+ customAttributes: CustomAttributes<D>;
319
+ constructor(data: D, rules: InitialRules<D>, customMessages?: CustomMessages<D>, customAttributes?: CustomAttributes<D>);
320
+ setData<ND extends GenericObject>(data: ND): BaseValidator<ND>;
321
+ setRules(rules: InitialRules<D>): this;
319
322
  setLang(lang: string): this;
320
323
  getLang(): string;
321
- setCustomMessages(customMessages?: CustomMessages): this;
322
- setCustomAttributes(customAttributes?: CustomAttributes): this;
324
+ setCustomMessages(customMessages?: CustomMessages<D>): this;
325
+ setCustomAttributes(customAttributes?: CustomAttributes<D>): this;
323
326
  stopOnFirstFailure(stopOnFirstFailure?: boolean): this;
324
327
  errors(): ErrorBag;
325
328
  clearErrors(keys?: string[]): ErrorBag;
@@ -660,6 +663,7 @@ type ValidationRuleSet = RuleName | RuleName[] | `${ValidationRuleName}${string
660
663
  * Parse rule names from rule string or string[] definitions
661
664
  */
662
665
  type ExtractRules<R> = R extends string ? R extends `${infer Head}|${infer Tail}` ? Head extends `${infer Rule}:${string}` ? Rule | ExtractRules<Tail> : Head | ExtractRules<Tail> : R extends `${infer Rule}:${string}` ? Rule : R : R extends string[] ? ExtractRules<R[number]> : never;
666
+ type ValidatedByRules<D extends Record<string, any>, R extends RulesForData<D>> = { [K in Extract<keyof R, keyof D>]: D[K] };
663
667
  /**
664
668
  * Flatten data structure into dot-notation keys
665
669
  * including wildcards (*) for arrays.
@@ -699,13 +703,13 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
699
703
  *
700
704
  * @throws ValidationException if validation fails
701
705
  */
702
- validate(): Promise<Record<string, any>>;
706
+ validate(): Promise<ValidatedByRules<D, R>>;
703
707
  /**
704
708
  * Run the validator's rules against its data.
705
709
  * @param bagName
706
710
  * @returns
707
711
  */
708
- validateWithBag(bagName: string): Promise<Record<string, any>>;
712
+ validateWithBag(bagName: string): Promise<ValidatedByRules<D, R>>;
709
713
  /**
710
714
  * Stop validation on first failure.
711
715
  */
@@ -713,7 +717,7 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
713
717
  /**
714
718
  * Get the data that passed validation.
715
719
  */
716
- validatedData(): Record<string, any>;
720
+ validatedData(): ValidatedByRules<D, R>;
717
721
  /**
718
722
  * Return all validated input.
719
723
  */
@@ -759,7 +763,7 @@ declare class IValidator<D extends Record<string, any> = any, R extends RulesFor
759
763
  /**
760
764
  * Get current data.
761
765
  */
762
- getData(): Record<string, any>;
766
+ getData(): ValidatedByRules<D, R>;
763
767
  /**
764
768
  * Get current rules.
765
769
  */
@@ -869,7 +873,7 @@ declare function notRegex(value: RegExp): Regex;
869
873
  //#endregion
870
874
  //#region src/Core.d.ts
871
875
  declare class Password extends Password$1 {}
872
- declare function make(data?: GenericObject, rules?: InitialRules, customMessages?: CustomMessages, customAttributes?: CustomAttributes): BaseValidator;
876
+ declare function make<D extends GenericObject = GenericObject>(data?: D, rules?: InitialRules<D>, customMessages?: CustomMessages<D>, customAttributes?: CustomAttributes<D>): BaseValidator<D>;
873
877
  //#endregion
874
878
  //#region src/Rules/IImplicitRule.d.ts
875
879
  declare class IImplicitRule extends IRuleContract {
@@ -1030,13 +1034,13 @@ declare class Validator<D extends Record<string, any> = any, R extends RulesForD
1030
1034
  *
1031
1035
  * @throws ValidationException if validation fails
1032
1036
  */
1033
- validate(): Promise<Record<string, any>>;
1037
+ validate(): Promise<ValidatedByRules<D, R>>;
1034
1038
  /**
1035
1039
  * Run the validator's rules against its data.
1036
1040
  * @param bagName
1037
1041
  * @returns
1038
1042
  */
1039
- validateWithBag(bagName: string): Promise<Record<string, any>>;
1043
+ validateWithBag(bagName: string): Promise<ValidatedByRules<D, R>>;
1040
1044
  /**
1041
1045
  * Stop validation on first failure.
1042
1046
  */
@@ -1044,7 +1048,7 @@ declare class Validator<D extends Record<string, any> = any, R extends RulesForD
1044
1048
  /**
1045
1049
  * Get the data that passed validation.
1046
1050
  */
1047
- validatedData(): Record<string, any>;
1051
+ validatedData(): ValidatedByRules<D, R>;
1048
1052
  /**
1049
1053
  * Return all validated input.
1050
1054
  */
@@ -1090,7 +1094,7 @@ declare class Validator<D extends Record<string, any> = any, R extends RulesForD
1090
1094
  /**
1091
1095
  * Get current data.
1092
1096
  */
1093
- getData(): Record<string, any>;
1097
+ getData(): ValidatedByRules<D, R>;
1094
1098
  /**
1095
1099
  * Get current rules.
1096
1100
  */
@@ -1432,4 +1436,4 @@ declare class ValidationException extends Error {
1432
1436
  getResponse(): any;
1433
1437
  }
1434
1438
  //#endregion
1435
- 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, ParamableValidationRuleName, Password, PlainRuleName, ReplaceAttributeInterface, Rules, RulesForData, TRule, 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 };
1439
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kanun",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Framework-agnostic TypeScript-first validation library.",
5
5
  "type": "module",
6
6
  "files": [