@warlock.js/seal 4.0.134 → 4.0.136
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/cjs/factory/validators.d.ts +1 -1
- package/cjs/factory/validators.d.ts.map +1 -1
- package/cjs/factory/validators.js +2 -2
- package/cjs/factory/validators.js.map +1 -1
- package/cjs/helpers/validation-helpers.d.ts +22 -0
- package/cjs/helpers/validation-helpers.d.ts.map +1 -1
- package/cjs/helpers/validation-helpers.js +56 -34
- package/cjs/helpers/validation-helpers.js.map +1 -1
- package/cjs/index.js +1 -1
- package/cjs/mutators/date-mutators.js +1 -1
- package/cjs/mutators/date-mutators.js.map +1 -1
- package/cjs/rules/array/array-rules.d.ts.map +1 -1
- package/cjs/rules/array/array-rules.js +1 -0
- package/cjs/rules/array/array-rules.js.map +1 -1
- package/cjs/rules/common/enum.d.ts.map +1 -1
- package/cjs/rules/common/enum.js +17 -3
- package/cjs/rules/common/enum.js.map +1 -1
- package/cjs/rules/common/equals-field-rules.d.ts.map +1 -1
- package/cjs/rules/common/equals-field-rules.js +1 -0
- package/cjs/rules/common/equals-field-rules.js.map +1 -1
- package/cjs/rules/common/type-rules.d.ts +4 -0
- package/cjs/rules/common/type-rules.d.ts.map +1 -1
- package/cjs/rules/common/type-rules.js +14 -1
- package/cjs/rules/common/type-rules.js.map +1 -1
- package/cjs/rules/common/unknown-key.js +2 -2
- package/cjs/rules/common/unknown-key.js.map +1 -1
- package/cjs/rules/conditional/forbidden-if-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/forbidden-if-rules.js +6 -0
- package/cjs/rules/conditional/forbidden-if-rules.js.map +1 -1
- package/cjs/rules/conditional/index.d.ts +1 -0
- package/cjs/rules/conditional/index.d.ts.map +1 -1
- package/cjs/rules/conditional/present-if-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/present-if-rules.js +2 -0
- package/cjs/rules/conditional/present-if-rules.js.map +1 -1
- package/cjs/rules/conditional/present-unless-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/present-unless-rules.js +2 -0
- package/cjs/rules/conditional/present-unless-rules.js.map +1 -1
- package/cjs/rules/conditional/present-with-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/present-with-rules.js +9 -2
- package/cjs/rules/conditional/present-with-rules.js.map +1 -1
- package/cjs/rules/conditional/present-without-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/present-without-rules.js +9 -2
- package/cjs/rules/conditional/present-without-rules.js.map +1 -1
- package/cjs/rules/conditional/required-if-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/required-if-rules.js +22 -7
- package/cjs/rules/conditional/required-if-rules.js.map +1 -1
- package/cjs/rules/conditional/required-unless-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/required-unless-rules.js +2 -0
- package/cjs/rules/conditional/required-unless-rules.js.map +1 -1
- package/cjs/rules/conditional/required-when-rule.d.ts +18 -0
- package/cjs/rules/conditional/required-when-rule.d.ts.map +1 -0
- package/cjs/rules/conditional/required-when-rule.js +26 -0
- package/cjs/rules/conditional/required-when-rule.js.map +1 -0
- package/cjs/rules/conditional/required-with-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/required-with-rules.js +1 -0
- package/cjs/rules/conditional/required-with-rules.js.map +1 -1
- package/cjs/rules/conditional/required-without-rules.d.ts.map +1 -1
- package/cjs/rules/conditional/required-without-rules.js +1 -0
- package/cjs/rules/conditional/required-without-rules.js.map +1 -1
- package/cjs/rules/core/equal.d.ts.map +1 -1
- package/cjs/rules/core/equal.js +1 -0
- package/cjs/rules/core/equal.js.map +1 -1
- package/cjs/rules/core/required.d.ts +1 -1
- package/cjs/rules/core/required.js +1 -1
- package/cjs/rules/date/date-comparison-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-comparison-rules.js +2 -0
- package/cjs/rules/date/date-comparison-rules.js.map +1 -1
- package/cjs/rules/date/date-day-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-day-rules.js +4 -1
- package/cjs/rules/date/date-day-rules.js.map +1 -1
- package/cjs/rules/date/date-field-comparison-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-field-comparison-rules.js +4 -0
- package/cjs/rules/date/date-field-comparison-rules.js.map +1 -1
- package/cjs/rules/date/date-period-rules.d.ts +20 -4
- package/cjs/rules/date/date-period-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-period-rules.js +44 -5
- package/cjs/rules/date/date-period-rules.js.map +1 -1
- package/cjs/rules/date/date-relative-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-relative-rules.js +3 -0
- package/cjs/rules/date/date-relative-rules.js.map +1 -1
- package/cjs/rules/date/date-special-rules.d.ts.map +1 -1
- package/cjs/rules/date/date-special-rules.js +5 -6
- package/cjs/rules/date/date-special-rules.js.map +1 -1
- package/cjs/rules/date/date.d.ts.map +1 -1
- package/cjs/rules/date/date.js +15 -6
- package/cjs/rules/date/date.js.map +1 -1
- package/cjs/rules/file/dimensions.d.ts.map +1 -1
- package/cjs/rules/file/dimensions.js +4 -0
- package/cjs/rules/file/dimensions.js.map +1 -1
- package/cjs/rules/file/file-size.d.ts.map +1 -1
- package/cjs/rules/file/file-size.js +4 -22
- package/cjs/rules/file/file-size.js.map +1 -1
- package/cjs/rules/length/length-rules.d.ts.map +1 -1
- package/cjs/rules/length/length-rules.js +13 -22
- package/cjs/rules/length/length-rules.js.map +1 -1
- package/cjs/rules/number/number-rules.d.ts.map +1 -1
- package/cjs/rules/number/number-rules.js +13 -2
- package/cjs/rules/number/number-rules.js.map +1 -1
- package/cjs/rules/scalar/accepted-rule.d.ts.map +1 -1
- package/cjs/rules/scalar/accepted-rule.js +12 -5
- package/cjs/rules/scalar/accepted-rule.js.map +1 -1
- package/cjs/rules/scalar/declined-rule.d.ts.map +1 -1
- package/cjs/rules/scalar/declined-rule.js +12 -5
- package/cjs/rules/scalar/declined-rule.js.map +1 -1
- package/cjs/rules/string/matches.d.ts.map +1 -1
- package/cjs/rules/string/matches.js +1 -0
- package/cjs/rules/string/matches.js.map +1 -1
- package/cjs/rules/string/pattern.d.ts.map +1 -1
- package/cjs/rules/string/pattern.js +1 -0
- package/cjs/rules/string/pattern.js.map +1 -1
- package/cjs/rules/string/string-comparison.d.ts.map +1 -1
- package/cjs/rules/string/string-comparison.js +4 -0
- package/cjs/rules/string/string-comparison.js.map +1 -1
- package/cjs/types/context-types.d.ts +0 -1
- package/cjs/types/context-types.d.ts.map +1 -1
- package/cjs/types/inference-types.d.ts +10 -5
- package/cjs/types/inference-types.d.ts.map +1 -1
- package/cjs/types/rule-types.d.ts +4 -0
- package/cjs/types/rule-types.d.ts.map +1 -1
- package/cjs/validators/array-validator.d.ts +2 -2
- package/cjs/validators/array-validator.d.ts.map +1 -1
- package/cjs/validators/array-validator.js +13 -23
- package/cjs/validators/array-validator.js.map +1 -1
- package/cjs/validators/base-validator.d.ts +79 -369
- package/cjs/validators/base-validator.d.ts.map +1 -1
- package/cjs/validators/base-validator.js +144 -826
- package/cjs/validators/base-validator.js.map +1 -1
- package/cjs/validators/boolean-validator.d.ts +18 -15
- package/cjs/validators/boolean-validator.d.ts.map +1 -1
- package/cjs/validators/boolean-validator.js +46 -16
- package/cjs/validators/boolean-validator.js.map +1 -1
- package/cjs/validators/date-validator.d.ts +8 -3
- package/cjs/validators/date-validator.d.ts.map +1 -1
- package/cjs/validators/date-validator.js +176 -246
- package/cjs/validators/date-validator.js.map +1 -1
- package/cjs/validators/float-validator.js +1 -1
- package/cjs/validators/float-validator.js.map +1 -1
- package/cjs/validators/index.d.ts +5 -0
- package/cjs/validators/index.d.ts.map +1 -1
- package/cjs/validators/int-validator.js +1 -1
- package/cjs/validators/int-validator.js.map +1 -1
- package/cjs/validators/methods/equality-conditional-methods.d.ts +88 -0
- package/cjs/validators/methods/equality-conditional-methods.d.ts.map +1 -0
- package/cjs/validators/methods/equality-conditional-methods.js +118 -0
- package/cjs/validators/methods/equality-conditional-methods.js.map +1 -0
- package/cjs/validators/methods/forbidden-methods.d.ts +58 -0
- package/cjs/validators/methods/forbidden-methods.d.ts.map +1 -0
- package/cjs/validators/methods/forbidden-methods.js +122 -0
- package/cjs/validators/methods/forbidden-methods.js.map +1 -0
- package/cjs/validators/methods/present-methods.d.ts +102 -0
- package/cjs/validators/methods/present-methods.d.ts.map +1 -0
- package/cjs/validators/methods/present-methods.js +229 -0
- package/cjs/validators/methods/present-methods.js.map +1 -0
- package/cjs/validators/methods/required-methods.d.ts +174 -0
- package/cjs/validators/methods/required-methods.d.ts.map +1 -0
- package/cjs/validators/methods/required-methods.js +246 -0
- package/cjs/validators/methods/required-methods.js.map +1 -0
- package/cjs/validators/number-validator.d.ts +11 -13
- package/cjs/validators/number-validator.d.ts.map +1 -1
- package/cjs/validators/number-validator.js +60 -78
- package/cjs/validators/number-validator.js.map +1 -1
- package/cjs/validators/numeric-validator.js +1 -1
- package/cjs/validators/numeric-validator.js.map +1 -1
- package/cjs/validators/object-validator.d.ts +1 -0
- package/cjs/validators/object-validator.d.ts.map +1 -1
- package/cjs/validators/object-validator.js +21 -15
- package/cjs/validators/object-validator.js.map +1 -1
- package/cjs/validators/primitive-validator.d.ts +70 -0
- package/cjs/validators/primitive-validator.d.ts.map +1 -0
- package/cjs/validators/primitive-validator.js +80 -0
- package/cjs/validators/primitive-validator.js.map +1 -0
- package/cjs/validators/record-validator.d.ts +4 -0
- package/cjs/validators/record-validator.d.ts.map +1 -1
- package/cjs/validators/record-validator.js +7 -1
- package/cjs/validators/record-validator.js.map +1 -1
- package/cjs/validators/scalar-validator.d.ts +5 -15
- package/cjs/validators/scalar-validator.d.ts.map +1 -1
- package/cjs/validators/scalar-validator.js +20 -78
- package/cjs/validators/scalar-validator.js.map +1 -1
- package/cjs/validators/string-validator.d.ts +2 -18
- package/cjs/validators/string-validator.d.ts.map +1 -1
- package/cjs/validators/string-validator.js +71 -164
- package/cjs/validators/string-validator.js.map +1 -1
- package/cjs/validators/tuple-validator.d.ts.map +1 -1
- package/cjs/validators/tuple-validator.js +2 -2
- package/cjs/validators/tuple-validator.js.map +1 -1
- package/cjs/validators/union-validator.d.ts.map +1 -1
- package/cjs/validators/union-validator.js +1 -3
- package/cjs/validators/union-validator.js.map +1 -1
- package/esm/factory/validators.d.ts +1 -1
- package/esm/factory/validators.d.ts.map +1 -1
- package/esm/factory/validators.js +2 -2
- package/esm/factory/validators.js.map +1 -1
- package/esm/helpers/validation-helpers.d.ts +22 -0
- package/esm/helpers/validation-helpers.d.ts.map +1 -1
- package/esm/helpers/validation-helpers.js +56 -34
- package/esm/helpers/validation-helpers.js.map +1 -1
- package/esm/index.js +1 -1
- package/esm/mutators/date-mutators.js +1 -1
- package/esm/mutators/date-mutators.js.map +1 -1
- package/esm/rules/array/array-rules.d.ts.map +1 -1
- package/esm/rules/array/array-rules.js +1 -0
- package/esm/rules/array/array-rules.js.map +1 -1
- package/esm/rules/common/enum.d.ts.map +1 -1
- package/esm/rules/common/enum.js +18 -4
- package/esm/rules/common/enum.js.map +1 -1
- package/esm/rules/common/equals-field-rules.d.ts.map +1 -1
- package/esm/rules/common/equals-field-rules.js +1 -0
- package/esm/rules/common/equals-field-rules.js.map +1 -1
- package/esm/rules/common/type-rules.d.ts +4 -0
- package/esm/rules/common/type-rules.d.ts.map +1 -1
- package/esm/rules/common/type-rules.js +15 -2
- package/esm/rules/common/type-rules.js.map +1 -1
- package/esm/rules/common/unknown-key.js +2 -2
- package/esm/rules/common/unknown-key.js.map +1 -1
- package/esm/rules/conditional/forbidden-if-rules.d.ts.map +1 -1
- package/esm/rules/conditional/forbidden-if-rules.js +6 -0
- package/esm/rules/conditional/forbidden-if-rules.js.map +1 -1
- package/esm/rules/conditional/index.d.ts +1 -0
- package/esm/rules/conditional/index.d.ts.map +1 -1
- package/esm/rules/conditional/present-if-rules.d.ts.map +1 -1
- package/esm/rules/conditional/present-if-rules.js +2 -0
- package/esm/rules/conditional/present-if-rules.js.map +1 -1
- package/esm/rules/conditional/present-unless-rules.d.ts.map +1 -1
- package/esm/rules/conditional/present-unless-rules.js +2 -0
- package/esm/rules/conditional/present-unless-rules.js.map +1 -1
- package/esm/rules/conditional/present-with-rules.d.ts.map +1 -1
- package/esm/rules/conditional/present-with-rules.js +9 -2
- package/esm/rules/conditional/present-with-rules.js.map +1 -1
- package/esm/rules/conditional/present-without-rules.d.ts.map +1 -1
- package/esm/rules/conditional/present-without-rules.js +9 -2
- package/esm/rules/conditional/present-without-rules.js.map +1 -1
- package/esm/rules/conditional/required-if-rules.d.ts.map +1 -1
- package/esm/rules/conditional/required-if-rules.js +22 -7
- package/esm/rules/conditional/required-if-rules.js.map +1 -1
- package/esm/rules/conditional/required-unless-rules.d.ts.map +1 -1
- package/esm/rules/conditional/required-unless-rules.js +2 -0
- package/esm/rules/conditional/required-unless-rules.js.map +1 -1
- package/esm/rules/conditional/required-when-rule.d.ts +18 -0
- package/esm/rules/conditional/required-when-rule.d.ts.map +1 -0
- package/esm/rules/conditional/required-when-rule.js +26 -0
- package/esm/rules/conditional/required-when-rule.js.map +1 -0
- package/esm/rules/conditional/required-with-rules.d.ts.map +1 -1
- package/esm/rules/conditional/required-with-rules.js +1 -0
- package/esm/rules/conditional/required-with-rules.js.map +1 -1
- package/esm/rules/conditional/required-without-rules.d.ts.map +1 -1
- package/esm/rules/conditional/required-without-rules.js +1 -0
- package/esm/rules/conditional/required-without-rules.js.map +1 -1
- package/esm/rules/core/equal.d.ts.map +1 -1
- package/esm/rules/core/equal.js +1 -0
- package/esm/rules/core/equal.js.map +1 -1
- package/esm/rules/core/required.d.ts +1 -1
- package/esm/rules/core/required.js +1 -1
- package/esm/rules/date/date-comparison-rules.d.ts.map +1 -1
- package/esm/rules/date/date-comparison-rules.js +2 -0
- package/esm/rules/date/date-comparison-rules.js.map +1 -1
- package/esm/rules/date/date-day-rules.d.ts.map +1 -1
- package/esm/rules/date/date-day-rules.js +4 -1
- package/esm/rules/date/date-day-rules.js.map +1 -1
- package/esm/rules/date/date-field-comparison-rules.d.ts.map +1 -1
- package/esm/rules/date/date-field-comparison-rules.js +4 -0
- package/esm/rules/date/date-field-comparison-rules.js.map +1 -1
- package/esm/rules/date/date-period-rules.d.ts +20 -4
- package/esm/rules/date/date-period-rules.d.ts.map +1 -1
- package/esm/rules/date/date-period-rules.js +44 -5
- package/esm/rules/date/date-period-rules.js.map +1 -1
- package/esm/rules/date/date-relative-rules.d.ts.map +1 -1
- package/esm/rules/date/date-relative-rules.js +3 -0
- package/esm/rules/date/date-relative-rules.js.map +1 -1
- package/esm/rules/date/date-special-rules.d.ts.map +1 -1
- package/esm/rules/date/date-special-rules.js +5 -6
- package/esm/rules/date/date-special-rules.js.map +1 -1
- package/esm/rules/date/date.d.ts.map +1 -1
- package/esm/rules/date/date.js +15 -6
- package/esm/rules/date/date.js.map +1 -1
- package/esm/rules/file/dimensions.d.ts.map +1 -1
- package/esm/rules/file/dimensions.js +4 -0
- package/esm/rules/file/dimensions.js.map +1 -1
- package/esm/rules/file/file-size.d.ts.map +1 -1
- package/esm/rules/file/file-size.js +4 -22
- package/esm/rules/file/file-size.js.map +1 -1
- package/esm/rules/length/length-rules.d.ts.map +1 -1
- package/esm/rules/length/length-rules.js +13 -22
- package/esm/rules/length/length-rules.js.map +1 -1
- package/esm/rules/number/number-rules.d.ts.map +1 -1
- package/esm/rules/number/number-rules.js +13 -2
- package/esm/rules/number/number-rules.js.map +1 -1
- package/esm/rules/scalar/accepted-rule.d.ts.map +1 -1
- package/esm/rules/scalar/accepted-rule.js +12 -5
- package/esm/rules/scalar/accepted-rule.js.map +1 -1
- package/esm/rules/scalar/declined-rule.d.ts.map +1 -1
- package/esm/rules/scalar/declined-rule.js +12 -5
- package/esm/rules/scalar/declined-rule.js.map +1 -1
- package/esm/rules/string/matches.d.ts.map +1 -1
- package/esm/rules/string/matches.js +1 -0
- package/esm/rules/string/matches.js.map +1 -1
- package/esm/rules/string/pattern.d.ts.map +1 -1
- package/esm/rules/string/pattern.js +1 -0
- package/esm/rules/string/pattern.js.map +1 -1
- package/esm/rules/string/string-comparison.d.ts.map +1 -1
- package/esm/rules/string/string-comparison.js +4 -0
- package/esm/rules/string/string-comparison.js.map +1 -1
- package/esm/types/context-types.d.ts +0 -1
- package/esm/types/context-types.d.ts.map +1 -1
- package/esm/types/inference-types.d.ts +10 -5
- package/esm/types/inference-types.d.ts.map +1 -1
- package/esm/types/rule-types.d.ts +4 -0
- package/esm/types/rule-types.d.ts.map +1 -1
- package/esm/validators/array-validator.d.ts +2 -2
- package/esm/validators/array-validator.d.ts.map +1 -1
- package/esm/validators/array-validator.js +13 -23
- package/esm/validators/array-validator.js.map +1 -1
- package/esm/validators/base-validator.d.ts +79 -369
- package/esm/validators/base-validator.d.ts.map +1 -1
- package/esm/validators/base-validator.js +144 -826
- package/esm/validators/base-validator.js.map +1 -1
- package/esm/validators/boolean-validator.d.ts +18 -15
- package/esm/validators/boolean-validator.d.ts.map +1 -1
- package/esm/validators/boolean-validator.js +46 -16
- package/esm/validators/boolean-validator.js.map +1 -1
- package/esm/validators/date-validator.d.ts +8 -3
- package/esm/validators/date-validator.d.ts.map +1 -1
- package/esm/validators/date-validator.js +176 -246
- package/esm/validators/date-validator.js.map +1 -1
- package/esm/validators/float-validator.js +1 -1
- package/esm/validators/float-validator.js.map +1 -1
- package/esm/validators/index.d.ts +5 -0
- package/esm/validators/index.d.ts.map +1 -1
- package/esm/validators/int-validator.js +1 -1
- package/esm/validators/int-validator.js.map +1 -1
- package/esm/validators/methods/equality-conditional-methods.d.ts +88 -0
- package/esm/validators/methods/equality-conditional-methods.d.ts.map +1 -0
- package/esm/validators/methods/equality-conditional-methods.js +118 -0
- package/esm/validators/methods/equality-conditional-methods.js.map +1 -0
- package/esm/validators/methods/forbidden-methods.d.ts +58 -0
- package/esm/validators/methods/forbidden-methods.d.ts.map +1 -0
- package/esm/validators/methods/forbidden-methods.js +122 -0
- package/esm/validators/methods/forbidden-methods.js.map +1 -0
- package/esm/validators/methods/present-methods.d.ts +102 -0
- package/esm/validators/methods/present-methods.d.ts.map +1 -0
- package/esm/validators/methods/present-methods.js +229 -0
- package/esm/validators/methods/present-methods.js.map +1 -0
- package/esm/validators/methods/required-methods.d.ts +174 -0
- package/esm/validators/methods/required-methods.d.ts.map +1 -0
- package/esm/validators/methods/required-methods.js +246 -0
- package/esm/validators/methods/required-methods.js.map +1 -0
- package/esm/validators/number-validator.d.ts +11 -13
- package/esm/validators/number-validator.d.ts.map +1 -1
- package/esm/validators/number-validator.js +60 -78
- package/esm/validators/number-validator.js.map +1 -1
- package/esm/validators/numeric-validator.js +1 -1
- package/esm/validators/numeric-validator.js.map +1 -1
- package/esm/validators/object-validator.d.ts +1 -0
- package/esm/validators/object-validator.d.ts.map +1 -1
- package/esm/validators/object-validator.js +21 -15
- package/esm/validators/object-validator.js.map +1 -1
- package/esm/validators/primitive-validator.d.ts +70 -0
- package/esm/validators/primitive-validator.d.ts.map +1 -0
- package/esm/validators/primitive-validator.js +80 -0
- package/esm/validators/primitive-validator.js.map +1 -0
- package/esm/validators/record-validator.d.ts +4 -0
- package/esm/validators/record-validator.d.ts.map +1 -1
- package/esm/validators/record-validator.js +8 -2
- package/esm/validators/record-validator.js.map +1 -1
- package/esm/validators/scalar-validator.d.ts +5 -15
- package/esm/validators/scalar-validator.d.ts.map +1 -1
- package/esm/validators/scalar-validator.js +20 -78
- package/esm/validators/scalar-validator.js.map +1 -1
- package/esm/validators/string-validator.d.ts +2 -18
- package/esm/validators/string-validator.d.ts.map +1 -1
- package/esm/validators/string-validator.js +71 -164
- package/esm/validators/string-validator.js.map +1 -1
- package/esm/validators/tuple-validator.d.ts.map +1 -1
- package/esm/validators/tuple-validator.js +2 -2
- package/esm/validators/tuple-validator.js.map +1 -1
- package/esm/validators/union-validator.d.ts.map +1 -1
- package/esm/validators/union-validator.js +1 -3
- package/esm/validators/union-validator.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {clone}from'@mongez/reinforcements';import {invalidRule,VALID_RULE}from'../helpers/validation-helpers.js';import {isEmptyValue}from'../helpers/is-empty-value.js';import {
|
|
1
|
+
import {clone}from'@mongez/reinforcements';import {invalidRule,VALID_RULE}from'../helpers/validation-helpers.js';import {isEmptyValue}from'../helpers/is-empty-value.js';import {requiredRule}from'../rules/core/required.js';/**
|
|
2
2
|
* Base validator class - foundation for all validators
|
|
3
3
|
*/
|
|
4
4
|
class BaseValidator {
|
|
@@ -8,6 +8,23 @@ class BaseValidator {
|
|
|
8
8
|
description;
|
|
9
9
|
shouldOmit = false;
|
|
10
10
|
isNullable = false;
|
|
11
|
+
isMutable = false;
|
|
12
|
+
/**
|
|
13
|
+
* Whether the field is optional.
|
|
14
|
+
* - false (default): field is required unless a requiredRule governs the condition.
|
|
15
|
+
* - true: field can be absent or empty — set by calling .optional().
|
|
16
|
+
*
|
|
17
|
+
* Also used as a TypeScript literal brand via the optional() return type.
|
|
18
|
+
*/
|
|
19
|
+
isOptional = false;
|
|
20
|
+
/**
|
|
21
|
+
* The single required-condition rule for this field.
|
|
22
|
+
* - null: field uses strict default (always required when not optional).
|
|
23
|
+
* - set: the rule governs when the field is required (e.g., requiredIf).
|
|
24
|
+
*
|
|
25
|
+
* Stored separately from rules[] and prepended at validate() time.
|
|
26
|
+
*/
|
|
27
|
+
requiredRule = this.createRule(requiredRule);
|
|
11
28
|
/**
|
|
12
29
|
* Pipeline to transform the mutated/original data before returning it
|
|
13
30
|
*/
|
|
@@ -22,6 +39,28 @@ class BaseValidator {
|
|
|
22
39
|
* Attributed that will be always using the attribute translator
|
|
23
40
|
*/
|
|
24
41
|
translatedAttributes = {};
|
|
42
|
+
/**
|
|
43
|
+
* Mark the validator as mutable
|
|
44
|
+
*/
|
|
45
|
+
get mutable() {
|
|
46
|
+
this.isMutable = true;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Mark the validator as immutable
|
|
51
|
+
*/
|
|
52
|
+
get immutable() {
|
|
53
|
+
this.isMutable = false;
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the instance to apply changes to.
|
|
58
|
+
* By default (immutable), returns a clone so the original is unchanged.
|
|
59
|
+
* When `.mutable` is set, returns `this` to mutate in place.
|
|
60
|
+
*/
|
|
61
|
+
get instance() {
|
|
62
|
+
return this.isMutable ? this : this.clone();
|
|
63
|
+
}
|
|
25
64
|
/**
|
|
26
65
|
* Get the default value
|
|
27
66
|
* Supports lazy evaluation via callbacks
|
|
@@ -33,15 +72,17 @@ class BaseValidator {
|
|
|
33
72
|
* Determine if value accepts null value
|
|
34
73
|
*/
|
|
35
74
|
nullable() {
|
|
36
|
-
|
|
37
|
-
|
|
75
|
+
const instance = this.instance;
|
|
76
|
+
instance.isNullable = true;
|
|
77
|
+
return instance;
|
|
38
78
|
}
|
|
39
79
|
/**
|
|
40
80
|
* Explicitly disallow null values after calling nullable
|
|
41
81
|
*/
|
|
42
82
|
notNullable() {
|
|
43
|
-
|
|
44
|
-
|
|
83
|
+
const instance = this.instance;
|
|
84
|
+
instance.isNullable = false;
|
|
85
|
+
return instance;
|
|
45
86
|
}
|
|
46
87
|
/**
|
|
47
88
|
* Add transformer with optional options
|
|
@@ -62,11 +103,33 @@ class BaseValidator {
|
|
|
62
103
|
* ```
|
|
63
104
|
*/
|
|
64
105
|
addTransformer(transform, options = {}) {
|
|
106
|
+
const instance = this.instance;
|
|
107
|
+
instance.addMutableTransformer(transform, options);
|
|
108
|
+
return instance;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Add transformer with optional options
|
|
112
|
+
*
|
|
113
|
+
* @param transform - The transformer callback function
|
|
114
|
+
* @param options - Optional options to pass to the transformer
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* // Without options
|
|
119
|
+
* v.date().addTransformer(data => data.toISOString())
|
|
120
|
+
*
|
|
121
|
+
* // With options
|
|
122
|
+
* v.date().addTransformer(
|
|
123
|
+
* (data, { options }) => dayjs(data).format(options.format),
|
|
124
|
+
* { format: 'YYYY-MM-DD' }
|
|
125
|
+
* )
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
addMutableTransformer(transform, options = {}) {
|
|
65
129
|
this.dataTransformers.push({
|
|
66
130
|
transform,
|
|
67
131
|
options,
|
|
68
132
|
});
|
|
69
|
-
return this;
|
|
70
133
|
}
|
|
71
134
|
/**
|
|
72
135
|
* Transform the output value - simple one-time transformation
|
|
@@ -86,8 +149,7 @@ class BaseValidator {
|
|
|
86
149
|
* ```
|
|
87
150
|
*/
|
|
88
151
|
outputAs(callback) {
|
|
89
|
-
this.addTransformer((data, { context }) => callback(data, context));
|
|
90
|
-
return this;
|
|
152
|
+
return this.addTransformer((data, { context }) => callback(data, context));
|
|
91
153
|
}
|
|
92
154
|
/**
|
|
93
155
|
* Transform output to JSON string
|
|
@@ -120,10 +182,9 @@ class BaseValidator {
|
|
|
120
182
|
* @category Transformer
|
|
121
183
|
*/
|
|
122
184
|
toJSON(indent) {
|
|
123
|
-
this.addTransformer((data, { options }) => JSON.stringify(data, null, options.indent), {
|
|
185
|
+
return this.addTransformer((data, { options }) => JSON.stringify(data, null, options.indent), {
|
|
124
186
|
indent: indent ?? 0,
|
|
125
187
|
});
|
|
126
|
-
return this;
|
|
127
188
|
}
|
|
128
189
|
/**
|
|
129
190
|
* Start data transformation pipeline
|
|
@@ -156,26 +217,29 @@ class BaseValidator {
|
|
|
156
217
|
* });
|
|
157
218
|
*/
|
|
158
219
|
attributes(attributes) {
|
|
220
|
+
const instance = this.instance;
|
|
159
221
|
for (const key in attributes) {
|
|
160
|
-
|
|
222
|
+
instance.attributesText[key] = attributes[key];
|
|
161
223
|
}
|
|
162
|
-
return
|
|
224
|
+
return instance;
|
|
163
225
|
}
|
|
164
226
|
/**
|
|
165
227
|
* Define a lazy getter property for each attribute in the given object and use the config attribute translator
|
|
166
228
|
*/
|
|
167
229
|
transAttributes(attributes) {
|
|
230
|
+
const instance = this.instance;
|
|
168
231
|
for (const key in attributes) {
|
|
169
|
-
|
|
232
|
+
instance.translatedAttributes[key] = attributes[key];
|
|
170
233
|
}
|
|
171
|
-
return
|
|
234
|
+
return instance;
|
|
172
235
|
}
|
|
173
236
|
/**
|
|
174
237
|
* Add description to the validator
|
|
175
238
|
*/
|
|
176
239
|
describe(description) {
|
|
177
|
-
|
|
178
|
-
|
|
240
|
+
const instance = this.instance;
|
|
241
|
+
instance.description = description;
|
|
242
|
+
return instance;
|
|
179
243
|
}
|
|
180
244
|
/**
|
|
181
245
|
* Check if this validator can handle the given value's type
|
|
@@ -233,6 +297,8 @@ class BaseValidator {
|
|
|
233
297
|
cloned.description = this.description;
|
|
234
298
|
cloned.attributesText = { ...this.attributesText };
|
|
235
299
|
cloned.isNullable = this.isNullable;
|
|
300
|
+
cloned.isOptional = this.isOptional;
|
|
301
|
+
cloned.requiredRule = this.requiredRule; // same reference is fine — rule is treated as immutable
|
|
236
302
|
return cloned;
|
|
237
303
|
}
|
|
238
304
|
/**
|
|
@@ -263,8 +329,9 @@ class BaseValidator {
|
|
|
263
329
|
* ```
|
|
264
330
|
*/
|
|
265
331
|
omit() {
|
|
266
|
-
|
|
267
|
-
|
|
332
|
+
const instance = this.instance;
|
|
333
|
+
instance.shouldOmit = true;
|
|
334
|
+
return instance;
|
|
268
335
|
}
|
|
269
336
|
/**
|
|
270
337
|
* @alias omit
|
|
@@ -279,60 +346,58 @@ class BaseValidator {
|
|
|
279
346
|
return this.shouldOmit;
|
|
280
347
|
}
|
|
281
348
|
/**
|
|
282
|
-
*
|
|
283
|
-
*/
|
|
284
|
-
equal(value, errorMessage) {
|
|
285
|
-
const rule = this.addRule(equalRule, errorMessage);
|
|
286
|
-
rule.context.options.value = value;
|
|
287
|
-
return this;
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Value must be the same as another field's value
|
|
291
|
-
*/
|
|
292
|
-
sameAs(field, errorMessage) {
|
|
293
|
-
const rule = this.addRule(equalsFieldRule, errorMessage);
|
|
294
|
-
rule.context.options.field = field;
|
|
295
|
-
rule.context.options.scope = "global";
|
|
296
|
-
return this;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Value must be the same as another sibling field's value
|
|
349
|
+
* Add rule to the validator
|
|
300
350
|
*/
|
|
301
|
-
|
|
302
|
-
const
|
|
303
|
-
rule
|
|
304
|
-
|
|
305
|
-
return this;
|
|
351
|
+
addRule(rule, errorMessage, options = {}) {
|
|
352
|
+
const instance = this.instance;
|
|
353
|
+
instance.addMutableRule(rule, errorMessage, options);
|
|
354
|
+
return instance;
|
|
306
355
|
}
|
|
307
356
|
/**
|
|
308
|
-
*
|
|
357
|
+
* Set the required-condition rule for this field.
|
|
358
|
+
*
|
|
359
|
+
* Unlike addRule(), this does NOT push to rules[]. The rule is stored in the
|
|
360
|
+
* dedicated `requiredRule` slot and is prepended to the validation pipeline
|
|
361
|
+
* at runtime. Only one required rule can be active per field — this replaces
|
|
362
|
+
* any previously set required rule.
|
|
363
|
+
*
|
|
364
|
+
* Also marks the field as not optional (isOptional = false).
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* // Used internally by required(), requiredIf(), requiredWith(), etc.
|
|
369
|
+
* BaseValidator.prototype.required = function(msg) {
|
|
370
|
+
* return this.setRequiredRule(requiredRule, msg);
|
|
371
|
+
* };
|
|
372
|
+
* ```
|
|
309
373
|
*/
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return
|
|
374
|
+
setRequiredRule(rule, errorMessage, options = {}) {
|
|
375
|
+
const instance = this.instance;
|
|
376
|
+
instance.isOptional = false;
|
|
377
|
+
instance.requiredRule = instance.createRule(rule, errorMessage, options);
|
|
378
|
+
return instance;
|
|
315
379
|
}
|
|
316
380
|
/**
|
|
317
|
-
*
|
|
381
|
+
* Add mutable rule
|
|
318
382
|
*/
|
|
319
|
-
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
return this;
|
|
383
|
+
addMutableRule(rule, errorMessage, options = {}) {
|
|
384
|
+
const newRule = this.createRule(rule, errorMessage, options);
|
|
385
|
+
this.rules.push(newRule);
|
|
386
|
+
return newRule;
|
|
324
387
|
}
|
|
325
388
|
/**
|
|
326
|
-
*
|
|
389
|
+
* Create new rule
|
|
327
390
|
*/
|
|
328
|
-
|
|
391
|
+
createRule(rule, errorMessage, options = {}) {
|
|
329
392
|
const newRule = {
|
|
330
393
|
...clone(rule),
|
|
331
394
|
context: {
|
|
332
395
|
errorMessage,
|
|
333
|
-
options
|
|
396
|
+
options,
|
|
334
397
|
attributesList: this.attributesText,
|
|
335
398
|
translatedAttributes: this.translatedAttributes,
|
|
399
|
+
translationParams: {},
|
|
400
|
+
translatableParams: {},
|
|
336
401
|
},
|
|
337
402
|
};
|
|
338
403
|
if (errorMessage) {
|
|
@@ -341,7 +406,6 @@ class BaseValidator {
|
|
|
341
406
|
if (rule.sortOrder === undefined) {
|
|
342
407
|
newRule.sortOrder = this.rules.length + 1;
|
|
343
408
|
}
|
|
344
|
-
this.rules.push(newRule);
|
|
345
409
|
return newRule;
|
|
346
410
|
}
|
|
347
411
|
/**
|
|
@@ -368,18 +432,13 @@ class BaseValidator {
|
|
|
368
432
|
*/
|
|
369
433
|
useRule(rule, options) {
|
|
370
434
|
const { errorMessage, ...ruleOptions } = options || {};
|
|
371
|
-
|
|
372
|
-
// Assign rule-specific options
|
|
373
|
-
if (Object.keys(ruleOptions).length > 0) {
|
|
374
|
-
Object.assign(ruleInstance.context.options, ruleOptions);
|
|
375
|
-
}
|
|
376
|
-
return this;
|
|
435
|
+
return this.addRule(rule, errorMessage, ruleOptions);
|
|
377
436
|
}
|
|
378
437
|
/**
|
|
379
438
|
* Define custom rule
|
|
380
439
|
*/
|
|
381
440
|
refine(callback) {
|
|
382
|
-
this.addRule({
|
|
441
|
+
return this.addRule({
|
|
383
442
|
name: "custom",
|
|
384
443
|
async validate(value, context) {
|
|
385
444
|
const result = await callback(value, context);
|
|
@@ -390,12 +449,19 @@ class BaseValidator {
|
|
|
390
449
|
return VALID_RULE;
|
|
391
450
|
},
|
|
392
451
|
});
|
|
393
|
-
return this;
|
|
394
452
|
}
|
|
395
453
|
/**
|
|
396
454
|
* Add mutator to the validator
|
|
397
455
|
*/
|
|
398
456
|
addMutator(mutator, options = {}) {
|
|
457
|
+
const instance = this.instance;
|
|
458
|
+
instance.addMutableMutator(mutator, options);
|
|
459
|
+
return instance;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Add mutable mutator
|
|
463
|
+
*/
|
|
464
|
+
addMutableMutator(mutator, options = {}) {
|
|
399
465
|
this.mutators.push({
|
|
400
466
|
mutate: mutator,
|
|
401
467
|
context: {
|
|
@@ -403,567 +469,14 @@ class BaseValidator {
|
|
|
403
469
|
ctx: {},
|
|
404
470
|
},
|
|
405
471
|
});
|
|
406
|
-
return this;
|
|
407
472
|
}
|
|
408
473
|
/**
|
|
409
474
|
* Set default value for the field
|
|
410
475
|
*/
|
|
411
476
|
default(value) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
// ==================== UNCONDITIONAL STATES ====================
|
|
416
|
-
/**
|
|
417
|
-
* This value must be present and has a value
|
|
418
|
-
*/
|
|
419
|
-
required(errorMessage) {
|
|
420
|
-
this.addRule(requiredRule, errorMessage);
|
|
421
|
-
return this;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* Value must be present but not necessarily has a value
|
|
425
|
-
*/
|
|
426
|
-
present(errorMessage) {
|
|
427
|
-
this.addRule(presentRule, errorMessage);
|
|
428
|
-
return this;
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Mark the field as optional, so pass it if it has no value or has a value
|
|
432
|
-
* Because this is the default behavior, this method is just syntactic sugar
|
|
433
|
-
*/
|
|
434
|
-
optional() {
|
|
435
|
-
return this;
|
|
436
|
-
}
|
|
437
|
-
// ==================== REQUIRED: BASED ON FIELD PRESENCE ====================
|
|
438
|
-
/**
|
|
439
|
-
* Value is required if another field exists
|
|
440
|
-
*/
|
|
441
|
-
requiredWith(field, errorMessage) {
|
|
442
|
-
const rule = this.addRule(requiredWithRule, errorMessage);
|
|
443
|
-
rule.context.options.field = field;
|
|
444
|
-
rule.context.options.scope = "global";
|
|
445
|
-
return this;
|
|
446
|
-
}
|
|
447
|
-
/**
|
|
448
|
-
* Value is required if another sibling field exists
|
|
449
|
-
*/
|
|
450
|
-
requiredWithSibling(field, errorMessage) {
|
|
451
|
-
const rule = this.addRule(requiredWithRule, errorMessage);
|
|
452
|
-
rule.context.options.field = field;
|
|
453
|
-
rule.context.options.scope = "sibling";
|
|
454
|
-
return this;
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Value is required if another field is missing
|
|
458
|
-
*/
|
|
459
|
-
requiredWithout(field, errorMessage) {
|
|
460
|
-
const rule = this.addRule(requiredWithoutRule, errorMessage);
|
|
461
|
-
rule.context.options.field = field;
|
|
462
|
-
rule.context.options.scope = "global";
|
|
463
|
-
return this;
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Value is required if another sibling field is missing
|
|
467
|
-
*/
|
|
468
|
-
requiredWithoutSibling(field, errorMessage) {
|
|
469
|
-
const rule = this.addRule(requiredWithoutRule, errorMessage);
|
|
470
|
-
rule.context.options.field = field;
|
|
471
|
-
rule.context.options.scope = "sibling";
|
|
472
|
-
return this;
|
|
473
|
-
}
|
|
474
|
-
// ==================== REQUIRED: BASED ON FIELD VALUE ====================
|
|
475
|
-
/**
|
|
476
|
-
* Value is required if another field equals a specific value
|
|
477
|
-
*/
|
|
478
|
-
requiredIf(field, value, errorMessage) {
|
|
479
|
-
const rule = this.addRule(requiredIfRule, errorMessage);
|
|
480
|
-
rule.context.options.field = field;
|
|
481
|
-
rule.context.options.value = value;
|
|
482
|
-
rule.context.options.scope = "global";
|
|
483
|
-
return this;
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Value is required if another sibling field equals a specific value
|
|
487
|
-
*/
|
|
488
|
-
requiredIfSibling(field, value, errorMessage) {
|
|
489
|
-
const rule = this.addRule(requiredIfRule, errorMessage);
|
|
490
|
-
rule.context.options.field = field;
|
|
491
|
-
rule.context.options.value = value;
|
|
492
|
-
rule.context.options.scope = "sibling";
|
|
493
|
-
return this;
|
|
494
|
-
}
|
|
495
|
-
/**
|
|
496
|
-
* Value is required unless another field equals a specific value
|
|
497
|
-
*/
|
|
498
|
-
requiredUnless(field, value, errorMessage) {
|
|
499
|
-
const rule = this.addRule(requiredUnlessRule, errorMessage);
|
|
500
|
-
rule.context.options.field = field;
|
|
501
|
-
rule.context.options.value = value;
|
|
502
|
-
rule.context.options.scope = "global";
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* Value is required unless another sibling field equals a specific value
|
|
507
|
-
*/
|
|
508
|
-
requiredUnlessSibling(field, value, errorMessage) {
|
|
509
|
-
const rule = this.addRule(requiredUnlessRule, errorMessage);
|
|
510
|
-
rule.context.options.field = field;
|
|
511
|
-
rule.context.options.value = value;
|
|
512
|
-
rule.context.options.scope = "sibling";
|
|
513
|
-
return this;
|
|
514
|
-
}
|
|
515
|
-
// ==================== REQUIRED: BASED ON FIELD EMPTY STATE ====================
|
|
516
|
-
/**
|
|
517
|
-
* Value is required if another field is empty
|
|
518
|
-
*/
|
|
519
|
-
requiredIfEmpty(field, errorMessage) {
|
|
520
|
-
const rule = this.addRule(requiredIfEmptyRule, errorMessage);
|
|
521
|
-
rule.context.options.field = field;
|
|
522
|
-
rule.context.options.scope = "global";
|
|
523
|
-
return this;
|
|
524
|
-
}
|
|
525
|
-
/**
|
|
526
|
-
* Value is required if another sibling field is empty
|
|
527
|
-
*/
|
|
528
|
-
requiredIfEmptySibling(field, errorMessage) {
|
|
529
|
-
const rule = this.addRule(requiredIfEmptyRule, errorMessage);
|
|
530
|
-
rule.context.options.field = field;
|
|
531
|
-
rule.context.options.scope = "sibling";
|
|
532
|
-
return this;
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Value is required if another field is not empty
|
|
536
|
-
*/
|
|
537
|
-
requiredIfNotEmpty(field, errorMessage) {
|
|
538
|
-
const rule = this.addRule(requiredIfNotEmptyRule, errorMessage);
|
|
539
|
-
rule.context.options.field = field;
|
|
540
|
-
rule.context.options.scope = "global";
|
|
541
|
-
return this;
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Value is required if another sibling field is not empty
|
|
545
|
-
*/
|
|
546
|
-
requiredIfNotEmptySibling(field, errorMessage) {
|
|
547
|
-
const rule = this.addRule(requiredIfNotEmptyRule, errorMessage);
|
|
548
|
-
rule.context.options.field = field;
|
|
549
|
-
rule.context.options.scope = "sibling";
|
|
550
|
-
return this;
|
|
551
|
-
}
|
|
552
|
-
// ==================== REQUIRED: BASED ON MULTIPLE FIELDS EMPTY STATE ====================
|
|
553
|
-
/**
|
|
554
|
-
* Value is required if ALL specified fields are empty
|
|
555
|
-
*/
|
|
556
|
-
requiredIfAllEmpty(fields, errorMessage) {
|
|
557
|
-
const rule = this.addRule(requiredIfAllEmptyRule, errorMessage);
|
|
558
|
-
rule.context.options.fields = fields;
|
|
559
|
-
rule.context.options.scope = "global";
|
|
560
|
-
return this;
|
|
561
|
-
}
|
|
562
|
-
/**
|
|
563
|
-
* Value is required if ALL specified sibling fields are empty
|
|
564
|
-
*/
|
|
565
|
-
requiredIfAllEmptySiblings(fields, errorMessage) {
|
|
566
|
-
const rule = this.addRule(requiredIfAllEmptyRule, errorMessage);
|
|
567
|
-
rule.context.options.fields = fields;
|
|
568
|
-
rule.context.options.scope = "sibling";
|
|
569
|
-
return this;
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Value is required if ANY of the specified fields is empty
|
|
573
|
-
*/
|
|
574
|
-
requiredIfAnyEmpty(fields, errorMessage) {
|
|
575
|
-
const rule = this.addRule(requiredIfAnyEmptyRule, errorMessage);
|
|
576
|
-
rule.context.options.fields = fields;
|
|
577
|
-
rule.context.options.scope = "global";
|
|
578
|
-
return this;
|
|
579
|
-
}
|
|
580
|
-
/**
|
|
581
|
-
* Value is required if ANY of the specified sibling fields is empty
|
|
582
|
-
*/
|
|
583
|
-
requiredIfAnyEmptySiblings(fields, errorMessage) {
|
|
584
|
-
const rule = this.addRule(requiredIfAnyEmptyRule, errorMessage);
|
|
585
|
-
rule.context.options.fields = fields;
|
|
586
|
-
rule.context.options.scope = "sibling";
|
|
587
|
-
return this;
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* Value is required if ALL specified fields are NOT empty
|
|
591
|
-
*/
|
|
592
|
-
requiredIfAllNotEmpty(fields, errorMessage) {
|
|
593
|
-
const rule = this.addRule(requiredIfAllNotEmptyRule, errorMessage);
|
|
594
|
-
rule.context.options.fields = fields;
|
|
595
|
-
rule.context.options.scope = "global";
|
|
596
|
-
return this;
|
|
597
|
-
}
|
|
598
|
-
/**
|
|
599
|
-
* Value is required if ALL specified sibling fields are NOT empty
|
|
600
|
-
*/
|
|
601
|
-
requiredIfAllNotEmptySiblings(fields, errorMessage) {
|
|
602
|
-
const rule = this.addRule(requiredIfAllNotEmptyRule, errorMessage);
|
|
603
|
-
rule.context.options.fields = fields;
|
|
604
|
-
rule.context.options.scope = "sibling";
|
|
605
|
-
return this;
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Value is required if ANY of the specified fields is NOT empty
|
|
609
|
-
*/
|
|
610
|
-
requiredIfAnyNotEmpty(fields, errorMessage) {
|
|
611
|
-
const rule = this.addRule(requiredIfAnyNotEmptyRule, errorMessage);
|
|
612
|
-
rule.context.options.fields = fields;
|
|
613
|
-
rule.context.options.scope = "global";
|
|
614
|
-
return this;
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* Value is required if ANY of the specified sibling fields is NOT empty
|
|
618
|
-
*/
|
|
619
|
-
requiredIfAnyNotEmptySiblings(fields, errorMessage) {
|
|
620
|
-
const rule = this.addRule(requiredIfAnyNotEmptyRule, errorMessage);
|
|
621
|
-
rule.context.options.fields = fields;
|
|
622
|
-
rule.context.options.scope = "sibling";
|
|
623
|
-
return this;
|
|
624
|
-
}
|
|
625
|
-
/**
|
|
626
|
-
* Value is required if another field's value is in the given array
|
|
627
|
-
*/
|
|
628
|
-
requiredIfIn(field, values, errorMessage) {
|
|
629
|
-
const rule = this.addRule(requiredIfInRule, errorMessage);
|
|
630
|
-
rule.context.options.field = field;
|
|
631
|
-
rule.context.options.values = values;
|
|
632
|
-
rule.context.options.scope = "global";
|
|
633
|
-
return this;
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Value is required if another sibling field's value is in the given array
|
|
637
|
-
*/
|
|
638
|
-
requiredIfInSibling(field, values, errorMessage) {
|
|
639
|
-
const rule = this.addRule(requiredIfInRule, errorMessage);
|
|
640
|
-
rule.context.options.field = field;
|
|
641
|
-
rule.context.options.values = values;
|
|
642
|
-
rule.context.options.scope = "sibling";
|
|
643
|
-
return this;
|
|
644
|
-
}
|
|
645
|
-
/**
|
|
646
|
-
* Value is required if another field's value is NOT in the given array
|
|
647
|
-
*/
|
|
648
|
-
requiredIfNotIn(field, values, errorMessage) {
|
|
649
|
-
const rule = this.addRule(requiredIfNotInRule, errorMessage);
|
|
650
|
-
rule.context.options.field = field;
|
|
651
|
-
rule.context.options.values = values;
|
|
652
|
-
rule.context.options.scope = "global";
|
|
653
|
-
return this;
|
|
654
|
-
}
|
|
655
|
-
/**
|
|
656
|
-
* Value is required if another sibling field's value is NOT in the given array
|
|
657
|
-
*/
|
|
658
|
-
requiredIfNotInSibling(field, values, errorMessage) {
|
|
659
|
-
const rule = this.addRule(requiredIfNotInRule, errorMessage);
|
|
660
|
-
rule.context.options.field = field;
|
|
661
|
-
rule.context.options.values = values;
|
|
662
|
-
rule.context.options.scope = "sibling";
|
|
663
|
-
return this;
|
|
664
|
-
}
|
|
665
|
-
// ==================== REQUIRED: BASED ON MULTIPLE FIELDS (ALL) ====================
|
|
666
|
-
/**
|
|
667
|
-
* Value is required if all specified fields exist
|
|
668
|
-
*/
|
|
669
|
-
requiredWithAll(fields, errorMessage) {
|
|
670
|
-
const rule = this.addRule(requiredWithAllRule, errorMessage);
|
|
671
|
-
rule.context.options.fields = fields;
|
|
672
|
-
rule.context.options.scope = "global";
|
|
673
|
-
return this;
|
|
674
|
-
}
|
|
675
|
-
/**
|
|
676
|
-
* Value is required if all specified sibling fields exist
|
|
677
|
-
*/
|
|
678
|
-
requiredWithAllSiblings(fields, errorMessage) {
|
|
679
|
-
const rule = this.addRule(requiredWithAllRule, errorMessage);
|
|
680
|
-
rule.context.options.fields = fields;
|
|
681
|
-
rule.context.options.scope = "sibling";
|
|
682
|
-
return this;
|
|
683
|
-
}
|
|
684
|
-
/**
|
|
685
|
-
* Value is required if all specified fields are missing
|
|
686
|
-
*/
|
|
687
|
-
requiredWithoutAll(fields, errorMessage) {
|
|
688
|
-
const rule = this.addRule(requiredWithoutAllRule, errorMessage);
|
|
689
|
-
rule.context.options.fields = fields;
|
|
690
|
-
rule.context.options.scope = "global";
|
|
691
|
-
return this;
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* Value is required if all specified sibling fields are missing
|
|
695
|
-
*/
|
|
696
|
-
requiredWithoutAllSiblings(fields, errorMessage) {
|
|
697
|
-
const rule = this.addRule(requiredWithoutAllRule, errorMessage);
|
|
698
|
-
rule.context.options.fields = fields;
|
|
699
|
-
rule.context.options.scope = "sibling";
|
|
700
|
-
return this;
|
|
701
|
-
}
|
|
702
|
-
// ==================== REQUIRED: BASED ON MULTIPLE FIELDS (ANY) ====================
|
|
703
|
-
/**
|
|
704
|
-
* Value is required if any of the specified fields exists
|
|
705
|
-
*/
|
|
706
|
-
requiredWithAny(fields, errorMessage) {
|
|
707
|
-
const rule = this.addRule(requiredWithAnyRule, errorMessage);
|
|
708
|
-
rule.context.options.fields = fields;
|
|
709
|
-
rule.context.options.scope = "global";
|
|
710
|
-
return this;
|
|
711
|
-
}
|
|
712
|
-
/**
|
|
713
|
-
* Value is required if any of the specified sibling fields exists
|
|
714
|
-
*/
|
|
715
|
-
requiredWithAnySiblings(fields, errorMessage) {
|
|
716
|
-
const rule = this.addRule(requiredWithAnyRule, errorMessage);
|
|
717
|
-
rule.context.options.fields = fields;
|
|
718
|
-
rule.context.options.scope = "sibling";
|
|
719
|
-
return this;
|
|
720
|
-
}
|
|
721
|
-
/**
|
|
722
|
-
* Value is required if any of the specified fields is missing
|
|
723
|
-
*/
|
|
724
|
-
requiredWithoutAny(fields, errorMessage) {
|
|
725
|
-
const rule = this.addRule(requiredWithoutAnyRule, errorMessage);
|
|
726
|
-
rule.context.options.fields = fields;
|
|
727
|
-
rule.context.options.scope = "global";
|
|
728
|
-
return this;
|
|
729
|
-
}
|
|
730
|
-
/**
|
|
731
|
-
* Value is required if any of the specified sibling fields is missing
|
|
732
|
-
*/
|
|
733
|
-
requiredWithoutAnySiblings(fields, errorMessage) {
|
|
734
|
-
const rule = this.addRule(requiredWithoutAnyRule, errorMessage);
|
|
735
|
-
rule.context.options.fields = fields;
|
|
736
|
-
rule.context.options.scope = "sibling";
|
|
737
|
-
return this;
|
|
738
|
-
}
|
|
739
|
-
// ==================== PRESENT: BASED ON FIELD PRESENCE ====================
|
|
740
|
-
/**
|
|
741
|
-
* Field must be present if another field exists
|
|
742
|
-
*/
|
|
743
|
-
presentWith(field, errorMessage) {
|
|
744
|
-
const rule = this.addRule(presentWithRule, errorMessage);
|
|
745
|
-
rule.context.options.field = field;
|
|
746
|
-
rule.context.options.scope = "global";
|
|
747
|
-
return this;
|
|
748
|
-
}
|
|
749
|
-
/**
|
|
750
|
-
* Field must be present if another sibling field exists
|
|
751
|
-
*/
|
|
752
|
-
presentWithSibling(field, errorMessage) {
|
|
753
|
-
const rule = this.addRule(presentWithRule, errorMessage);
|
|
754
|
-
rule.context.options.field = field;
|
|
755
|
-
rule.context.options.scope = "sibling";
|
|
756
|
-
return this;
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Field must be present if another field is missing
|
|
760
|
-
*/
|
|
761
|
-
presentWithout(field, errorMessage) {
|
|
762
|
-
const rule = this.addRule(presentWithoutRule, errorMessage);
|
|
763
|
-
rule.context.options.field = field;
|
|
764
|
-
rule.context.options.scope = "global";
|
|
765
|
-
return this;
|
|
766
|
-
}
|
|
767
|
-
/**
|
|
768
|
-
* Field must be present if another sibling field is missing
|
|
769
|
-
*/
|
|
770
|
-
presentWithoutSibling(field, errorMessage) {
|
|
771
|
-
const rule = this.addRule(presentWithoutRule, errorMessage);
|
|
772
|
-
rule.context.options.field = field;
|
|
773
|
-
rule.context.options.scope = "sibling";
|
|
774
|
-
return this;
|
|
775
|
-
}
|
|
776
|
-
// ==================== PRESENT: BASED ON FIELD VALUE ====================
|
|
777
|
-
/**
|
|
778
|
-
* Field must be present if another field equals a specific value
|
|
779
|
-
*/
|
|
780
|
-
presentIf(field, value, errorMessage) {
|
|
781
|
-
const rule = this.addRule(presentIfRule, errorMessage);
|
|
782
|
-
rule.context.options.field = field;
|
|
783
|
-
rule.context.options.value = value;
|
|
784
|
-
rule.context.options.scope = "global";
|
|
785
|
-
return this;
|
|
786
|
-
}
|
|
787
|
-
/**
|
|
788
|
-
* Field must be present if another sibling field equals a specific value
|
|
789
|
-
*/
|
|
790
|
-
presentIfSibling(field, value, errorMessage) {
|
|
791
|
-
const rule = this.addRule(presentIfRule, errorMessage);
|
|
792
|
-
rule.context.options.field = field;
|
|
793
|
-
rule.context.options.value = value;
|
|
794
|
-
rule.context.options.scope = "sibling";
|
|
795
|
-
return this;
|
|
796
|
-
}
|
|
797
|
-
/**
|
|
798
|
-
* Field must be present unless another field equals a specific value
|
|
799
|
-
*/
|
|
800
|
-
presentUnless(field, value, errorMessage) {
|
|
801
|
-
const rule = this.addRule(presentUnlessRule, errorMessage);
|
|
802
|
-
rule.context.options.field = field;
|
|
803
|
-
rule.context.options.value = value;
|
|
804
|
-
rule.context.options.scope = "global";
|
|
805
|
-
return this;
|
|
806
|
-
}
|
|
807
|
-
/**
|
|
808
|
-
* Field must be present unless another sibling field equals a specific value
|
|
809
|
-
*/
|
|
810
|
-
presentUnlessSibling(field, value, errorMessage) {
|
|
811
|
-
const rule = this.addRule(presentUnlessRule, errorMessage);
|
|
812
|
-
rule.context.options.field = field;
|
|
813
|
-
rule.context.options.value = value;
|
|
814
|
-
rule.context.options.scope = "sibling";
|
|
815
|
-
return this;
|
|
816
|
-
}
|
|
817
|
-
// ==================== PRESENT: BASED ON FIELD EMPTY STATE ====================
|
|
818
|
-
/**
|
|
819
|
-
* Field must be present if another field is empty
|
|
820
|
-
*/
|
|
821
|
-
presentIfEmpty(field, errorMessage) {
|
|
822
|
-
const rule = this.addRule(presentIfEmptyRule, errorMessage);
|
|
823
|
-
rule.context.options.field = field;
|
|
824
|
-
rule.context.options.scope = "global";
|
|
825
|
-
return this;
|
|
826
|
-
}
|
|
827
|
-
/**
|
|
828
|
-
* Field must be present if another sibling field is empty
|
|
829
|
-
*/
|
|
830
|
-
presentIfEmptySibling(field, errorMessage) {
|
|
831
|
-
const rule = this.addRule(presentIfEmptyRule, errorMessage);
|
|
832
|
-
rule.context.options.field = field;
|
|
833
|
-
rule.context.options.scope = "sibling";
|
|
834
|
-
return this;
|
|
835
|
-
}
|
|
836
|
-
/**
|
|
837
|
-
* Field must be present if another field is not empty
|
|
838
|
-
*/
|
|
839
|
-
presentIfNotEmpty(field, errorMessage) {
|
|
840
|
-
const rule = this.addRule(presentIfNotEmptyRule, errorMessage);
|
|
841
|
-
rule.context.options.field = field;
|
|
842
|
-
rule.context.options.scope = "global";
|
|
843
|
-
return this;
|
|
844
|
-
}
|
|
845
|
-
/**
|
|
846
|
-
* Field must be present if another sibling field is not empty
|
|
847
|
-
*/
|
|
848
|
-
presentIfNotEmptySibling(field, errorMessage) {
|
|
849
|
-
const rule = this.addRule(presentIfNotEmptyRule, errorMessage);
|
|
850
|
-
rule.context.options.field = field;
|
|
851
|
-
rule.context.options.scope = "sibling";
|
|
852
|
-
return this;
|
|
853
|
-
}
|
|
854
|
-
/**
|
|
855
|
-
* Field must be present if another field's value is in the given array
|
|
856
|
-
*/
|
|
857
|
-
presentIfIn(field, values, errorMessage) {
|
|
858
|
-
const rule = this.addRule(presentIfInRule, errorMessage);
|
|
859
|
-
rule.context.options.field = field;
|
|
860
|
-
rule.context.options.values = values;
|
|
861
|
-
rule.context.options.scope = "global";
|
|
862
|
-
return this;
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* Field must be present if another sibling field's value is in the given array
|
|
866
|
-
*/
|
|
867
|
-
presentIfInSibling(field, values, errorMessage) {
|
|
868
|
-
const rule = this.addRule(presentIfInRule, errorMessage);
|
|
869
|
-
rule.context.options.field = field;
|
|
870
|
-
rule.context.options.values = values;
|
|
871
|
-
rule.context.options.scope = "sibling";
|
|
872
|
-
return this;
|
|
873
|
-
}
|
|
874
|
-
/**
|
|
875
|
-
* Field must be present if another field's value is NOT in the given array
|
|
876
|
-
*/
|
|
877
|
-
presentIfNotIn(field, values, errorMessage) {
|
|
878
|
-
const rule = this.addRule(presentIfNotInRule, errorMessage);
|
|
879
|
-
rule.context.options.field = field;
|
|
880
|
-
rule.context.options.values = values;
|
|
881
|
-
rule.context.options.scope = "global";
|
|
882
|
-
return this;
|
|
883
|
-
}
|
|
884
|
-
/**
|
|
885
|
-
* Field must be present if another sibling field's value is NOT in the given array
|
|
886
|
-
*/
|
|
887
|
-
presentIfNotInSibling(field, values, errorMessage) {
|
|
888
|
-
const rule = this.addRule(presentIfNotInRule, errorMessage);
|
|
889
|
-
rule.context.options.field = field;
|
|
890
|
-
rule.context.options.values = values;
|
|
891
|
-
rule.context.options.scope = "sibling";
|
|
892
|
-
return this;
|
|
893
|
-
}
|
|
894
|
-
// ==================== PRESENT: BASED ON MULTIPLE FIELDS (ALL) ====================
|
|
895
|
-
/**
|
|
896
|
-
* Field must be present if all specified fields exist
|
|
897
|
-
*/
|
|
898
|
-
presentWithAll(fields, errorMessage) {
|
|
899
|
-
const rule = this.addRule(presentWithAllRule, errorMessage);
|
|
900
|
-
rule.context.options.fields = fields;
|
|
901
|
-
rule.context.options.scope = "global";
|
|
902
|
-
return this;
|
|
903
|
-
}
|
|
904
|
-
/**
|
|
905
|
-
* Field must be present if all specified sibling fields exist
|
|
906
|
-
*/
|
|
907
|
-
presentWithAllSiblings(fields, errorMessage) {
|
|
908
|
-
const rule = this.addRule(presentWithAllRule, errorMessage);
|
|
909
|
-
rule.context.options.fields = fields;
|
|
910
|
-
rule.context.options.scope = "sibling";
|
|
911
|
-
return this;
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* Field must be present if all specified fields are missing
|
|
915
|
-
*/
|
|
916
|
-
presentWithoutAll(fields, errorMessage) {
|
|
917
|
-
const rule = this.addRule(presentWithoutAllRule, errorMessage);
|
|
918
|
-
rule.context.options.fields = fields;
|
|
919
|
-
rule.context.options.scope = "global";
|
|
920
|
-
return this;
|
|
921
|
-
}
|
|
922
|
-
/**
|
|
923
|
-
* Field must be present if all specified sibling fields are missing
|
|
924
|
-
*/
|
|
925
|
-
presentWithoutAllSiblings(fields, errorMessage) {
|
|
926
|
-
const rule = this.addRule(presentWithoutAllRule, errorMessage);
|
|
927
|
-
rule.context.options.fields = fields;
|
|
928
|
-
rule.context.options.scope = "sibling";
|
|
929
|
-
return this;
|
|
930
|
-
}
|
|
931
|
-
// ==================== PRESENT: BASED ON MULTIPLE FIELDS (ANY) ====================
|
|
932
|
-
/**
|
|
933
|
-
* Field must be present if any of the specified fields exists
|
|
934
|
-
*/
|
|
935
|
-
presentWithAny(fields, errorMessage) {
|
|
936
|
-
const rule = this.addRule(presentWithAnyRule, errorMessage);
|
|
937
|
-
rule.context.options.fields = fields;
|
|
938
|
-
rule.context.options.scope = "global";
|
|
939
|
-
return this;
|
|
940
|
-
}
|
|
941
|
-
/**
|
|
942
|
-
* Field must be present if any of the specified sibling fields exists
|
|
943
|
-
*/
|
|
944
|
-
presentWithAnySiblings(fields, errorMessage) {
|
|
945
|
-
const rule = this.addRule(presentWithAnyRule, errorMessage);
|
|
946
|
-
rule.context.options.fields = fields;
|
|
947
|
-
rule.context.options.scope = "sibling";
|
|
948
|
-
return this;
|
|
949
|
-
}
|
|
950
|
-
/**
|
|
951
|
-
* Field must be present if any of the specified fields is missing
|
|
952
|
-
*/
|
|
953
|
-
presentWithoutAny(fields, errorMessage) {
|
|
954
|
-
const rule = this.addRule(presentWithoutAnyRule, errorMessage);
|
|
955
|
-
rule.context.options.fields = fields;
|
|
956
|
-
rule.context.options.scope = "global";
|
|
957
|
-
return this;
|
|
958
|
-
}
|
|
959
|
-
/**
|
|
960
|
-
* Field must be present if any of the specified sibling fields is missing
|
|
961
|
-
*/
|
|
962
|
-
presentWithoutAnySiblings(fields, errorMessage) {
|
|
963
|
-
const rule = this.addRule(presentWithoutAnyRule, errorMessage);
|
|
964
|
-
rule.context.options.fields = fields;
|
|
965
|
-
rule.context.options.scope = "sibling";
|
|
966
|
-
return this;
|
|
477
|
+
const instance = this.instance;
|
|
478
|
+
instance.defaultValue = value;
|
|
479
|
+
return instance;
|
|
967
480
|
}
|
|
968
481
|
/**
|
|
969
482
|
* Mutate the data
|
|
@@ -976,211 +489,13 @@ class BaseValidator {
|
|
|
976
489
|
}
|
|
977
490
|
return mutatedData;
|
|
978
491
|
}
|
|
979
|
-
/**
|
|
980
|
-
* Value is forbidden to be present
|
|
981
|
-
*/
|
|
982
|
-
forbidden(errorMessage) {
|
|
983
|
-
this.addRule(forbiddenRule, errorMessage);
|
|
984
|
-
return this;
|
|
985
|
-
}
|
|
986
|
-
/**
|
|
987
|
-
* Value is forbidden if another field equals a specific value (global scope)
|
|
988
|
-
*/
|
|
989
|
-
forbiddenIf(field, value, errorMessage) {
|
|
990
|
-
const rule = this.addRule(forbiddenIfRule, errorMessage);
|
|
991
|
-
rule.context.options.field = field;
|
|
992
|
-
rule.context.options.value = value;
|
|
993
|
-
rule.context.options.scope = "global";
|
|
994
|
-
return this;
|
|
995
|
-
}
|
|
996
|
-
/**
|
|
997
|
-
* Value is forbidden if another field equals a specific value (sibling scope)
|
|
998
|
-
*/
|
|
999
|
-
forbiddenIfSibling(field, value, errorMessage) {
|
|
1000
|
-
const rule = this.addRule(forbiddenIfRule, errorMessage);
|
|
1001
|
-
rule.context.options.field = field;
|
|
1002
|
-
rule.context.options.value = value;
|
|
1003
|
-
rule.context.options.scope = "sibling";
|
|
1004
|
-
return this;
|
|
1005
|
-
}
|
|
1006
|
-
/**
|
|
1007
|
-
* Value is forbidden if another field does NOT equal a specific value (global scope)
|
|
1008
|
-
*/
|
|
1009
|
-
forbiddenIfNot(field, value, errorMessage) {
|
|
1010
|
-
const rule = this.addRule(forbiddenIfNotRule, errorMessage);
|
|
1011
|
-
rule.context.options.field = field;
|
|
1012
|
-
rule.context.options.value = value;
|
|
1013
|
-
rule.context.options.scope = "global";
|
|
1014
|
-
return this;
|
|
1015
|
-
}
|
|
1016
|
-
/**
|
|
1017
|
-
* Value is forbidden if another field does NOT equal a specific value (sibling scope)
|
|
1018
|
-
*/
|
|
1019
|
-
forbiddenIfNotSibling(field, value, errorMessage) {
|
|
1020
|
-
const rule = this.addRule(forbiddenIfNotRule, errorMessage);
|
|
1021
|
-
rule.context.options.field = field;
|
|
1022
|
-
rule.context.options.value = value;
|
|
1023
|
-
rule.context.options.scope = "sibling";
|
|
1024
|
-
return this;
|
|
1025
|
-
}
|
|
1026
|
-
/**
|
|
1027
|
-
* Value is forbidden if another field is empty (global scope)
|
|
1028
|
-
*/
|
|
1029
|
-
forbiddenIfEmpty(field, errorMessage) {
|
|
1030
|
-
const rule = this.addRule(forbiddenIfEmptyRule, errorMessage);
|
|
1031
|
-
rule.context.options.field = field;
|
|
1032
|
-
rule.context.options.scope = "global";
|
|
1033
|
-
return this;
|
|
1034
|
-
}
|
|
1035
|
-
/**
|
|
1036
|
-
* Value is forbidden if another field is empty (sibling scope)
|
|
1037
|
-
*/
|
|
1038
|
-
forbiddenIfEmptySibling(field, errorMessage) {
|
|
1039
|
-
const rule = this.addRule(forbiddenIfEmptyRule, errorMessage);
|
|
1040
|
-
rule.context.options.field = field;
|
|
1041
|
-
rule.context.options.scope = "sibling";
|
|
1042
|
-
return this;
|
|
1043
|
-
}
|
|
1044
|
-
/**
|
|
1045
|
-
* Value is forbidden if another field is not empty (global scope)
|
|
1046
|
-
*/
|
|
1047
|
-
forbiddenIfNotEmpty(field, errorMessage) {
|
|
1048
|
-
const rule = this.addRule(forbiddenIfNotEmptyRule, errorMessage);
|
|
1049
|
-
rule.context.options.field = field;
|
|
1050
|
-
rule.context.options.scope = "global";
|
|
1051
|
-
return this;
|
|
1052
|
-
}
|
|
1053
|
-
/**
|
|
1054
|
-
* Value is forbidden if another field is not empty (sibling scope)
|
|
1055
|
-
*/
|
|
1056
|
-
forbiddenIfNotEmptySibling(field, errorMessage) {
|
|
1057
|
-
const rule = this.addRule(forbiddenIfNotEmptyRule, errorMessage);
|
|
1058
|
-
rule.context.options.field = field;
|
|
1059
|
-
rule.context.options.scope = "sibling";
|
|
1060
|
-
return this;
|
|
1061
|
-
}
|
|
1062
|
-
/**
|
|
1063
|
-
* Value is forbidden if another field's value is in the given array (global scope)
|
|
1064
|
-
*/
|
|
1065
|
-
forbiddenIfIn(field, values, errorMessage) {
|
|
1066
|
-
const rule = this.addRule(forbiddenIfInRule, errorMessage);
|
|
1067
|
-
rule.context.options.field = field;
|
|
1068
|
-
rule.context.options.values = values;
|
|
1069
|
-
rule.context.options.scope = "global";
|
|
1070
|
-
return this;
|
|
1071
|
-
}
|
|
1072
|
-
/**
|
|
1073
|
-
* Value is forbidden if another field's value is in the given array (sibling scope)
|
|
1074
|
-
*/
|
|
1075
|
-
forbiddenIfInSibling(field, values, errorMessage) {
|
|
1076
|
-
const rule = this.addRule(forbiddenIfInRule, errorMessage);
|
|
1077
|
-
rule.context.options.field = field;
|
|
1078
|
-
rule.context.options.values = values;
|
|
1079
|
-
rule.context.options.scope = "sibling";
|
|
1080
|
-
return this;
|
|
1081
|
-
}
|
|
1082
|
-
/**
|
|
1083
|
-
* Value is forbidden if another field's value is NOT in the given array (global scope)
|
|
1084
|
-
*/
|
|
1085
|
-
forbiddenIfNotIn(field, values, errorMessage) {
|
|
1086
|
-
const rule = this.addRule(forbiddenIfNotInRule, errorMessage);
|
|
1087
|
-
rule.context.options.field = field;
|
|
1088
|
-
rule.context.options.values = values;
|
|
1089
|
-
rule.context.options.scope = "global";
|
|
1090
|
-
return this;
|
|
1091
|
-
}
|
|
1092
|
-
/**
|
|
1093
|
-
* Value is forbidden if another field's value is NOT in the given array (sibling scope)
|
|
1094
|
-
*/
|
|
1095
|
-
forbiddenIfNotInSibling(field, values, errorMessage) {
|
|
1096
|
-
const rule = this.addRule(forbiddenIfNotInRule, errorMessage);
|
|
1097
|
-
rule.context.options.field = field;
|
|
1098
|
-
rule.context.options.values = values;
|
|
1099
|
-
rule.context.options.scope = "sibling";
|
|
1100
|
-
return this;
|
|
1101
|
-
}
|
|
1102
|
-
/**
|
|
1103
|
-
* Apply different validation rules based on another field's value (global scope)
|
|
1104
|
-
*
|
|
1105
|
-
* Use this when you need to apply completely different validators
|
|
1106
|
-
* based on another field's value (not just required/optional).
|
|
1107
|
-
*
|
|
1108
|
-
* @param field - Field name to check (can be nested with dot notation)
|
|
1109
|
-
* @param options - Validation options per field value
|
|
1110
|
-
*
|
|
1111
|
-
* @example
|
|
1112
|
-
* ```ts
|
|
1113
|
-
* // Different allowed values based on user type
|
|
1114
|
-
* v.object({
|
|
1115
|
-
* userType: v.string().in(['admin', 'user']),
|
|
1116
|
-
* role: v.string().when('userType', {
|
|
1117
|
-
* is: {
|
|
1118
|
-
* admin: v.string().in(['super', 'moderator']),
|
|
1119
|
-
* user: v.string().in(['member', 'guest'])
|
|
1120
|
-
* },
|
|
1121
|
-
* otherwise: v.string().optional()
|
|
1122
|
-
* })
|
|
1123
|
-
* })
|
|
1124
|
-
*
|
|
1125
|
-
* // Different validation rules based on type
|
|
1126
|
-
* v.object({
|
|
1127
|
-
* contactType: v.string().in(['email', 'phone']),
|
|
1128
|
-
* contact: v.string().when('contactType', {
|
|
1129
|
-
* is: {
|
|
1130
|
-
* email: v.string().email(),
|
|
1131
|
-
* phone: v.string().pattern(/^\d{10}$/)
|
|
1132
|
-
* }
|
|
1133
|
-
* })
|
|
1134
|
-
* })
|
|
1135
|
-
* ```
|
|
1136
|
-
* @category Conditional Validation
|
|
1137
|
-
*/
|
|
1138
|
-
when(field, options) {
|
|
1139
|
-
const rule = this.addRule(whenRule);
|
|
1140
|
-
rule.context.options.field = field;
|
|
1141
|
-
rule.context.options.is = options.is;
|
|
1142
|
-
rule.context.options.otherwise = options.otherwise;
|
|
1143
|
-
rule.context.options.scope = "global";
|
|
1144
|
-
return this;
|
|
1145
|
-
}
|
|
1146
|
-
/**
|
|
1147
|
-
* Apply different validation rules based on sibling field's value
|
|
1148
|
-
*
|
|
1149
|
-
* Use this for nested objects where you need to check a field
|
|
1150
|
-
* within the same parent object.
|
|
1151
|
-
*
|
|
1152
|
-
* @param siblingField - Sibling field name to check
|
|
1153
|
-
* @param options - Validation options per field value
|
|
1154
|
-
*
|
|
1155
|
-
* @example
|
|
1156
|
-
* ```ts
|
|
1157
|
-
* // Array of users with role-based permissions
|
|
1158
|
-
* v.array(v.object({
|
|
1159
|
-
* userType: v.string().in(['admin', 'user']),
|
|
1160
|
-
* permissions: v.string().whenSibling('userType', {
|
|
1161
|
-
* is: {
|
|
1162
|
-
* admin: v.string().in(['read', 'write', 'delete']),
|
|
1163
|
-
* user: v.string().in(['read'])
|
|
1164
|
-
* }
|
|
1165
|
-
* })
|
|
1166
|
-
* }))
|
|
1167
|
-
* ```
|
|
1168
|
-
* @category Conditional Validation
|
|
1169
|
-
*/
|
|
1170
|
-
whenSibling(siblingField, options) {
|
|
1171
|
-
const rule = this.addRule(whenRule);
|
|
1172
|
-
rule.context.options.field = siblingField;
|
|
1173
|
-
rule.context.options.is = options.is;
|
|
1174
|
-
rule.context.options.otherwise = options.otherwise;
|
|
1175
|
-
rule.context.options.scope = "sibling";
|
|
1176
|
-
return this;
|
|
1177
|
-
}
|
|
1178
492
|
/**
|
|
1179
493
|
* Set the label for the validator that will be matching the :input attribute
|
|
1180
494
|
*/
|
|
1181
495
|
label(label) {
|
|
1182
|
-
|
|
1183
|
-
|
|
496
|
+
const instance = this.instance;
|
|
497
|
+
instance.attributesText.input = label;
|
|
498
|
+
return instance;
|
|
1184
499
|
}
|
|
1185
500
|
/**
|
|
1186
501
|
* Validate the data
|
|
@@ -1189,14 +504,17 @@ class BaseValidator {
|
|
|
1189
504
|
if (data === null && this.isNullable) {
|
|
1190
505
|
return { isValid: true, errors: [], data: null };
|
|
1191
506
|
}
|
|
1192
|
-
const
|
|
1193
|
-
const mutatedData = await this.mutate(
|
|
1194
|
-
const valueForRules = valueForMutation;
|
|
507
|
+
const valueForRules = data ?? this.getDefaultValue();
|
|
508
|
+
const mutatedData = await this.mutate(valueForRules, context);
|
|
1195
509
|
const errors = [];
|
|
1196
510
|
let isValid = true;
|
|
1197
511
|
const isFirstErrorOnly = context.configurations?.firstErrorOnly ?? true;
|
|
1198
512
|
const isEmpty = isEmptyValue(valueForRules);
|
|
1199
|
-
|
|
513
|
+
console.log("isEmpty", isEmpty, valueForRules);
|
|
514
|
+
// Prepend the required-condition rule if set, so it always runs first.
|
|
515
|
+
// requiredRule has requiresValue = false so it runs even on empty values.
|
|
516
|
+
const rulesToRun = this.requiredRule ? [this.requiredRule, ...this.rules] : this.rules;
|
|
517
|
+
for (const rule of rulesToRun) {
|
|
1200
518
|
if ((rule.requiresValue ?? true) && isEmpty)
|
|
1201
519
|
continue;
|
|
1202
520
|
this.setRuleAttributesList(rule);
|