@decaf-ts/decorator-validation 1.14.0 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/constants/errors.cjs +1 -0
- package/lib/cjs/constants/errors.cjs.map +1 -0
- package/lib/cjs/constants/index.cjs +1 -0
- package/lib/cjs/constants/index.cjs.map +1 -0
- package/lib/cjs/constants/validation.cjs +1 -0
- package/lib/cjs/constants/validation.cjs.map +1 -0
- package/lib/cjs/index.cjs +2 -1
- package/lib/cjs/index.cjs.map +1 -0
- package/lib/cjs/model/Builder.cjs +49 -48
- package/lib/cjs/model/Builder.cjs.map +1 -0
- package/lib/cjs/model/Model.cjs +31 -30
- package/lib/cjs/model/Model.cjs.map +1 -0
- package/lib/cjs/model/ModelErrorDefinition.cjs +1 -0
- package/lib/cjs/model/ModelErrorDefinition.cjs.map +1 -0
- package/lib/cjs/model/ModelRegistry.cjs +7 -6
- package/lib/cjs/model/ModelRegistry.cjs.map +1 -0
- package/lib/cjs/model/constants.cjs +2 -13
- package/lib/cjs/model/constants.cjs.map +1 -0
- package/lib/cjs/model/construction.cjs +5 -4
- package/lib/cjs/model/construction.cjs.map +1 -0
- package/lib/cjs/model/decorators.cjs +12 -11
- package/lib/cjs/model/decorators.cjs.map +1 -0
- package/lib/cjs/model/index.cjs +1 -0
- package/lib/cjs/model/index.cjs.map +1 -0
- package/lib/cjs/model/types.cjs +1 -0
- package/lib/cjs/model/types.cjs.map +1 -0
- package/lib/cjs/model/utils.cjs +1 -0
- package/lib/cjs/model/utils.cjs.map +1 -0
- package/lib/cjs/model/validation.cjs +33 -32
- package/lib/cjs/model/validation.cjs.map +1 -0
- package/lib/cjs/overrides/Metadata.cjs +1 -0
- package/lib/cjs/overrides/Metadata.cjs.map +1 -0
- package/lib/cjs/overrides/index.cjs +1 -0
- package/lib/cjs/overrides/index.cjs.map +1 -0
- package/lib/cjs/overrides/overrides.cjs +20 -19
- package/lib/cjs/overrides/overrides.cjs.map +1 -0
- package/lib/cjs/overrides/types.cjs +1 -0
- package/lib/cjs/overrides/types.cjs.map +1 -0
- package/lib/cjs/types/index.cjs +1 -0
- package/lib/cjs/types/index.cjs.map +1 -0
- package/lib/cjs/types/validation.cjs +1 -0
- package/lib/cjs/types/{validation.js.map → validation.cjs.map} +1 -1
- package/lib/cjs/utils/DateBuilder.cjs +1 -0
- package/lib/cjs/utils/DateBuilder.cjs.map +1 -0
- package/lib/cjs/utils/PathProxy.cjs +9 -8
- package/lib/cjs/utils/PathProxy.cjs.map +1 -0
- package/lib/cjs/utils/constants.cjs +2 -21
- package/lib/cjs/utils/constants.cjs.map +1 -0
- package/lib/cjs/utils/dates.cjs +9 -8
- package/lib/cjs/utils/dates.cjs.map +1 -0
- package/lib/cjs/utils/equality.cjs +1 -0
- package/lib/cjs/utils/equality.cjs.map +1 -0
- package/lib/cjs/utils/hashing.cjs +6 -3
- package/lib/cjs/utils/hashing.cjs.map +1 -0
- package/lib/cjs/utils/index.cjs +1 -0
- package/lib/cjs/utils/index.cjs.map +1 -0
- package/lib/cjs/utils/registry.cjs +1 -0
- package/lib/cjs/utils/registry.cjs.map +1 -0
- package/lib/cjs/utils/serialization.cjs +3 -2
- package/lib/cjs/utils/serialization.cjs.map +1 -0
- package/lib/cjs/utils/serializers.cjs +8 -7
- package/lib/cjs/utils/serializers.cjs.map +1 -0
- package/lib/cjs/utils/strings.cjs +1 -0
- package/lib/cjs/utils/strings.cjs.map +1 -0
- package/lib/cjs/utils/types.cjs +1 -0
- package/lib/cjs/utils/types.cjs.map +1 -0
- package/lib/cjs/validation/Validation.cjs +9 -8
- package/lib/cjs/validation/Validation.cjs.map +1 -0
- package/lib/cjs/validation/Validators/AsyncValidator.cjs +5 -4
- package/lib/cjs/validation/Validators/AsyncValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/BaseValidator.cjs +8 -7
- package/lib/cjs/validation/Validators/BaseValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/DateValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/DateValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/DiffValidator.cjs +21 -15
- package/lib/cjs/validation/Validators/DiffValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/EmailValidator.cjs +18 -12
- package/lib/cjs/validation/Validators/EmailValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/EqualsValidator.cjs +21 -15
- package/lib/cjs/validation/Validators/EqualsValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/GreaterThanOrEqualValidator.cjs +24 -18
- package/lib/cjs/validation/Validators/GreaterThanOrEqualValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/GreaterThanValidator.cjs +21 -15
- package/lib/cjs/validation/Validators/GreaterThanValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/LessThanOrEqualValidator.cjs +24 -18
- package/lib/cjs/validation/Validators/LessThanOrEqualValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/LessThanValidator.cjs +21 -15
- package/lib/cjs/validation/Validators/LessThanValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/ListValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/ListValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/MaxLengthValidator.cjs +19 -13
- package/lib/cjs/validation/Validators/MaxLengthValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/MaxValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/MaxValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/MinLengthValidator.cjs +19 -13
- package/lib/cjs/validation/Validators/MinLengthValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/MinValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/MinValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/OptionValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/OptionValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/PasswordValidator.cjs +18 -12
- package/lib/cjs/validation/Validators/PasswordValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/PatternValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/PatternValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/RequiredValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/RequiredValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/StepValidator.cjs +17 -11
- package/lib/cjs/validation/Validators/StepValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/TypeValidator.cjs +23 -17
- package/lib/cjs/validation/Validators/TypeValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/URLValidator.cjs +18 -12
- package/lib/cjs/validation/Validators/URLValidator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/Validator.cjs +5 -4
- package/lib/cjs/validation/Validators/Validator.cjs.map +1 -0
- package/lib/cjs/validation/Validators/ValidatorRegistry.cjs +4 -3
- package/lib/cjs/validation/Validators/ValidatorRegistry.cjs.map +1 -0
- package/lib/cjs/validation/Validators/constants.cjs +3 -2
- package/lib/cjs/validation/Validators/constants.cjs.map +1 -0
- package/lib/cjs/validation/Validators/decorators.cjs +5 -4
- package/lib/cjs/validation/Validators/decorators.cjs.map +1 -0
- package/lib/cjs/validation/Validators/index.cjs +1 -0
- package/lib/cjs/validation/Validators/index.cjs.map +1 -0
- package/lib/cjs/validation/Validators/utils.cjs +14 -13
- package/lib/cjs/validation/Validators/utils.cjs.map +1 -0
- package/lib/cjs/validation/decorators.cjs +70 -69
- package/lib/cjs/validation/decorators.cjs.map +1 -0
- package/lib/cjs/validation/index.cjs +1 -0
- package/lib/cjs/validation/index.cjs.map +1 -0
- package/lib/cjs/validation/types.cjs +2 -1
- package/lib/cjs/validation/types.cjs.map +1 -0
- package/lib/esm/index.js +1 -1
- package/lib/types/constants/errors.d.cts +17 -0
- package/lib/types/constants/errors.d.mts +17 -0
- package/lib/types/constants/index.d.cts +2 -0
- package/lib/types/constants/index.d.mts +2 -0
- package/lib/types/constants/validation.d.cts +16 -0
- package/lib/types/constants/validation.d.mts +16 -0
- package/lib/types/index.d.cts +20 -0
- package/lib/types/index.d.mts +20 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/types/model/Builder.d.cts +86 -0
- package/lib/types/model/Builder.d.mts +86 -0
- package/lib/types/model/Model.d.cts +286 -0
- package/lib/types/model/Model.d.mts +286 -0
- package/lib/types/model/ModelErrorDefinition.d.cts +22 -0
- package/lib/types/model/ModelErrorDefinition.d.mts +22 -0
- package/lib/types/model/ModelRegistry.d.cts +181 -0
- package/lib/types/model/ModelRegistry.d.mts +181 -0
- package/lib/types/model/constants.d.cts +65 -0
- package/lib/types/model/constants.d.mts +65 -0
- package/lib/types/model/construction.d.cts +46 -0
- package/lib/types/model/construction.d.mts +46 -0
- package/lib/types/model/decorators.d.cts +47 -0
- package/lib/types/model/decorators.d.mts +47 -0
- package/lib/types/model/index.d.cts +10 -0
- package/lib/types/model/index.d.mts +10 -0
- package/lib/types/model/types.d.cts +145 -0
- package/lib/types/model/types.d.mts +145 -0
- package/lib/types/model/utils.d.cts +13 -0
- package/lib/types/model/utils.d.mts +13 -0
- package/lib/types/model/validation.d.cts +92 -0
- package/lib/types/model/validation.d.mts +92 -0
- package/lib/types/overrides/Metadata.d.cts +132 -0
- package/lib/types/overrides/Metadata.d.mts +132 -0
- package/lib/types/overrides/index.d.cts +4 -0
- package/lib/types/overrides/index.d.mts +4 -0
- package/lib/types/overrides/overrides.d.cts +1 -0
- package/lib/types/overrides/overrides.d.mts +1 -0
- package/lib/types/overrides/types.d.cts +17 -0
- package/lib/types/overrides/types.d.mts +17 -0
- package/lib/types/types/index.d.cts +1 -0
- package/lib/types/types/index.d.mts +1 -0
- package/lib/types/types/validation.d.cts +28 -0
- package/lib/types/types/validation.d.mts +28 -0
- package/lib/types/utils/DateBuilder.d.cts +121 -0
- package/lib/types/utils/DateBuilder.d.mts +121 -0
- package/lib/types/utils/PathProxy.d.cts +43 -0
- package/lib/types/utils/PathProxy.d.mts +43 -0
- package/lib/types/utils/constants.d.cts +32 -0
- package/lib/types/utils/constants.d.mts +32 -0
- package/lib/types/utils/dates.d.cts +86 -0
- package/lib/types/utils/dates.d.mts +86 -0
- package/lib/types/utils/equality.d.cts +56 -0
- package/lib/types/utils/equality.d.mts +56 -0
- package/lib/types/utils/hashing.d.cts +82 -0
- package/lib/types/utils/hashing.d.mts +82 -0
- package/lib/types/utils/index.d.cts +11 -0
- package/lib/types/utils/index.d.mts +11 -0
- package/lib/types/utils/registry.d.cts +68 -0
- package/lib/types/utils/registry.d.mts +68 -0
- package/lib/types/utils/serialization.d.cts +12 -0
- package/lib/types/utils/serialization.d.mts +12 -0
- package/lib/types/utils/serializers.d.cts +41 -0
- package/lib/types/utils/serializers.d.mts +41 -0
- package/lib/types/utils/strings.d.cts +25 -0
- package/lib/types/utils/strings.d.mts +25 -0
- package/lib/types/utils/types.d.cts +35 -0
- package/lib/types/utils/types.d.mts +35 -0
- package/lib/types/validation/Validation.d.cts +53 -0
- package/lib/types/validation/Validation.d.mts +53 -0
- package/lib/types/validation/Validators/AsyncValidator.d.cts +72 -0
- package/lib/types/validation/Validators/AsyncValidator.d.mts +72 -0
- package/lib/types/validation/Validators/BaseValidator.d.cts +118 -0
- package/lib/types/validation/Validators/BaseValidator.d.mts +118 -0
- package/lib/types/validation/Validators/DateValidator.d.cts +60 -0
- package/lib/types/validation/Validators/DateValidator.d.mts +60 -0
- package/lib/types/validation/Validators/DiffValidator.d.cts +29 -0
- package/lib/types/validation/Validators/DiffValidator.d.mts +29 -0
- package/lib/types/validation/Validators/EmailValidator.d.cts +60 -0
- package/lib/types/validation/Validators/EmailValidator.d.mts +60 -0
- package/lib/types/validation/Validators/EqualsValidator.d.cts +29 -0
- package/lib/types/validation/Validators/EqualsValidator.d.mts +29 -0
- package/lib/types/validation/Validators/GreaterThanOrEqualValidator.d.cts +29 -0
- package/lib/types/validation/Validators/GreaterThanOrEqualValidator.d.mts +29 -0
- package/lib/types/validation/Validators/GreaterThanValidator.d.cts +29 -0
- package/lib/types/validation/Validators/GreaterThanValidator.d.mts +29 -0
- package/lib/types/validation/Validators/LessThanOrEqualValidator.d.cts +29 -0
- package/lib/types/validation/Validators/LessThanOrEqualValidator.d.mts +29 -0
- package/lib/types/validation/Validators/LessThanValidator.d.cts +29 -0
- package/lib/types/validation/Validators/LessThanValidator.d.mts +29 -0
- package/lib/types/validation/Validators/ListValidator.d.cts +66 -0
- package/lib/types/validation/Validators/ListValidator.d.mts +66 -0
- package/lib/types/validation/Validators/MaxLengthValidator.d.cts +29 -0
- package/lib/types/validation/Validators/MaxLengthValidator.d.mts +29 -0
- package/lib/types/validation/Validators/MaxValidator.d.cts +74 -0
- package/lib/types/validation/Validators/MaxValidator.d.mts +74 -0
- package/lib/types/validation/Validators/MinLengthValidator.d.cts +29 -0
- package/lib/types/validation/Validators/MinLengthValidator.d.mts +29 -0
- package/lib/types/validation/Validators/MinValidator.d.cts +74 -0
- package/lib/types/validation/Validators/MinValidator.d.mts +74 -0
- package/lib/types/validation/Validators/OptionValidator.d.cts +29 -0
- package/lib/types/validation/Validators/OptionValidator.d.mts +29 -0
- package/lib/types/validation/Validators/PasswordValidator.d.cts +28 -0
- package/lib/types/validation/Validators/PasswordValidator.d.mts +28 -0
- package/lib/types/validation/Validators/PatternValidator.d.cts +103 -0
- package/lib/types/validation/Validators/PatternValidator.d.mts +103 -0
- package/lib/types/validation/Validators/RequiredValidator.d.cts +74 -0
- package/lib/types/validation/Validators/RequiredValidator.d.mts +74 -0
- package/lib/types/validation/Validators/StepValidator.d.cts +29 -0
- package/lib/types/validation/Validators/StepValidator.d.mts +29 -0
- package/lib/types/validation/Validators/TypeValidator.d.cts +79 -0
- package/lib/types/validation/Validators/TypeValidator.d.mts +79 -0
- package/lib/types/validation/Validators/URLValidator.d.cts +61 -0
- package/lib/types/validation/Validators/URLValidator.d.mts +61 -0
- package/lib/types/validation/Validators/Validator.d.cts +65 -0
- package/lib/types/validation/Validators/Validator.d.mts +65 -0
- package/lib/types/validation/Validators/ValidatorRegistry.d.cts +41 -0
- package/lib/types/validation/Validators/ValidatorRegistry.d.mts +41 -0
- package/lib/types/validation/Validators/constants.d.cts +157 -0
- package/lib/types/validation/Validators/constants.d.mts +157 -0
- package/lib/types/validation/Validators/decorators.d.cts +12 -0
- package/lib/types/validation/Validators/decorators.d.mts +12 -0
- package/lib/types/validation/Validators/index.d.cts +26 -0
- package/lib/types/validation/Validators/index.d.mts +26 -0
- package/lib/types/validation/Validators/utils.d.cts +75 -0
- package/lib/types/validation/Validators/utils.d.mts +75 -0
- package/lib/types/validation/decorators.d.cts +313 -0
- package/lib/types/validation/decorators.d.mts +313 -0
- package/lib/types/validation/index.d.cts +9 -0
- package/lib/types/validation/index.d.mts +9 -0
- package/lib/types/validation/types.d.cts +309 -0
- package/lib/types/validation/types.d.mts +309 -0
- package/package.json +1 -1
- package/lib/cjs/constants/errors.js.map +0 -1
- package/lib/cjs/constants/index.js.map +0 -1
- package/lib/cjs/constants/validation.js.map +0 -1
- package/lib/cjs/index.js.map +0 -1
- package/lib/cjs/model/Builder.js.map +0 -1
- package/lib/cjs/model/Model.js.map +0 -1
- package/lib/cjs/model/ModelErrorDefinition.js.map +0 -1
- package/lib/cjs/model/ModelRegistry.js.map +0 -1
- package/lib/cjs/model/constants.js.map +0 -1
- package/lib/cjs/model/construction.js.map +0 -1
- package/lib/cjs/model/decorators.js.map +0 -1
- package/lib/cjs/model/index.js.map +0 -1
- package/lib/cjs/model/types.js.map +0 -1
- package/lib/cjs/model/utils.js.map +0 -1
- package/lib/cjs/model/validation.js.map +0 -1
- package/lib/cjs/overrides/Metadata.js.map +0 -1
- package/lib/cjs/overrides/index.js.map +0 -1
- package/lib/cjs/overrides/overrides.js.map +0 -1
- package/lib/cjs/overrides/types.js.map +0 -1
- package/lib/cjs/types/index.js.map +0 -1
- package/lib/cjs/utils/DateBuilder.js.map +0 -1
- package/lib/cjs/utils/PathProxy.js.map +0 -1
- package/lib/cjs/utils/constants.js.map +0 -1
- package/lib/cjs/utils/dates.js.map +0 -1
- package/lib/cjs/utils/equality.js.map +0 -1
- package/lib/cjs/utils/hashing.js.map +0 -1
- package/lib/cjs/utils/index.js.map +0 -1
- package/lib/cjs/utils/registry.js.map +0 -1
- package/lib/cjs/utils/serialization.js.map +0 -1
- package/lib/cjs/utils/serializers.js.map +0 -1
- package/lib/cjs/utils/strings.js.map +0 -1
- package/lib/cjs/utils/types.js.map +0 -1
- package/lib/cjs/validation/Validation.js.map +0 -1
- package/lib/cjs/validation/Validators/AsyncValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/BaseValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/DateValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/DiffValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/EmailValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/EqualsValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/GreaterThanOrEqualValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/GreaterThanValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/LessThanOrEqualValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/LessThanValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/ListValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/MaxLengthValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/MaxValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/MinLengthValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/MinValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/OptionValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/PasswordValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/PatternValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/RequiredValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/StepValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/TypeValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/URLValidator.js.map +0 -1
- package/lib/cjs/validation/Validators/Validator.js.map +0 -1
- package/lib/cjs/validation/Validators/ValidatorRegistry.js.map +0 -1
- package/lib/cjs/validation/Validators/constants.js.map +0 -1
- package/lib/cjs/validation/Validators/decorators.js.map +0 -1
- package/lib/cjs/validation/Validators/index.js.map +0 -1
- package/lib/cjs/validation/Validators/utils.js.map +0 -1
- package/lib/cjs/validation/decorators.js.map +0 -1
- package/lib/cjs/validation/index.js.map +0 -1
- package/lib/cjs/validation/types.js.map +0 -1
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Model } from "./Model.js";
|
|
2
|
+
import { ObjectAccumulator } from "typed-object-accumulator";
|
|
3
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
4
|
+
import { ComparisonValidatorOptions } from "../validation/types.js";
|
|
5
|
+
import { ExtendedMetadata } from "../overrides/types.js";
|
|
6
|
+
type BuildableModel = Model & Record<PropertyKey, any>;
|
|
7
|
+
export interface DecorateOption<M extends Model> {
|
|
8
|
+
decorate(...decorators: PropertyDecorator[]): ModelBuilder<M>;
|
|
9
|
+
}
|
|
10
|
+
export declare class AttributeBuilder<M extends BuildableModel, N extends keyof M, T> implements DecorateOption<M> {
|
|
11
|
+
protected parent: ModelBuilder<M>;
|
|
12
|
+
readonly attr: N;
|
|
13
|
+
readonly declaredType: T;
|
|
14
|
+
constructor(parent: ModelBuilder<M>, attr: N, declaredType: T);
|
|
15
|
+
private decorators;
|
|
16
|
+
decorate(...decorators: PropertyDecorator[]): ModelBuilder<M>;
|
|
17
|
+
undecorate(...decorators: PropertyDecorator[]): ModelBuilder<M>;
|
|
18
|
+
required(messageOrMeta?: string | Record<string, any>): ModelBuilder<M>;
|
|
19
|
+
min(valueOrMeta: number | Date | string | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
20
|
+
max(valueOrMeta: number | Date | string | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
21
|
+
step(valueOrMeta: number | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
22
|
+
minlength(valueOrMeta: number | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
23
|
+
maxlength(valueOrMeta: number | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
24
|
+
pattern(valueOrMeta: RegExp | string | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
25
|
+
email(messageOrMeta?: string | Record<string, any>): ModelBuilder<M>;
|
|
26
|
+
url(messageOrMeta?: string | Record<string, any>): ModelBuilder<M>;
|
|
27
|
+
type(valueOrMeta: Constructor | (() => Constructor) | (Constructor | (() => Constructor))[] | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
28
|
+
date(formatOrMeta?: string | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
29
|
+
password(valueOrMeta?: RegExp | string | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
30
|
+
list(clazzOrMeta: Constructor | (() => Constructor) | (Constructor | (() => Constructor))[] | Record<string, any>, collection?: "Array" | "Set", message?: string): ModelBuilder<M>;
|
|
31
|
+
set(clazzOrMeta: Constructor | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
32
|
+
enum(valueOrMeta: any[] | Record<any, any> | Record<string, any>, message?: string): ModelBuilder<M>;
|
|
33
|
+
option(value: any[] | Record<any, any>, message?: string): ModelBuilder<M>;
|
|
34
|
+
private static isMetadataPayload;
|
|
35
|
+
private static asMeta;
|
|
36
|
+
private static resolveMessage;
|
|
37
|
+
private static patternFromString;
|
|
38
|
+
private resolveComparison;
|
|
39
|
+
equals(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
40
|
+
eq(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
41
|
+
different(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
42
|
+
diff(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
43
|
+
lessThan(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
44
|
+
lt(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
45
|
+
lessThanOrEqual(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
46
|
+
lte(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
47
|
+
greaterThan(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
48
|
+
gt(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
49
|
+
greaterThanOrEqual(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
50
|
+
gte(propertyOrMeta: string | Record<string, any>, options?: Omit<ComparisonValidatorOptions, "async" | "description">): ModelBuilder<M>;
|
|
51
|
+
description(desc: string): ModelBuilder<M>;
|
|
52
|
+
/**
|
|
53
|
+
* Applies the attribute metadata and decorators to the provided constructor.
|
|
54
|
+
*/
|
|
55
|
+
build(constructor: Constructor<M>): void;
|
|
56
|
+
}
|
|
57
|
+
declare class ListAttributeBuilder<M extends BuildableModel, N extends keyof M> {
|
|
58
|
+
private readonly parent;
|
|
59
|
+
private readonly attribute;
|
|
60
|
+
private readonly collection;
|
|
61
|
+
constructor(parent: ModelBuilder<M>, attribute: AttributeBuilder<M, N, any>, collection: "Array" | "Set");
|
|
62
|
+
ofPrimitives(clazz: Constructor | (() => Constructor) | (Constructor | (() => Constructor))[], message?: string): ModelBuilder<M>;
|
|
63
|
+
ofModel<MM extends BuildableModel>(): ModelBuilder<MM>;
|
|
64
|
+
}
|
|
65
|
+
export declare class ModelBuilder<M extends BuildableModel = BuildableModel> extends ObjectAccumulator<M> {
|
|
66
|
+
private attributes;
|
|
67
|
+
private _name?;
|
|
68
|
+
private _description?;
|
|
69
|
+
private _classDecorators;
|
|
70
|
+
private _parent?;
|
|
71
|
+
setName(name: string): this;
|
|
72
|
+
description(desc: string): this;
|
|
73
|
+
decorateClass(decorator: ClassDecorator): this;
|
|
74
|
+
private attribute;
|
|
75
|
+
string<N extends keyof M>(attr: N): AttributeBuilder<M, N, StringConstructor>;
|
|
76
|
+
number<N extends keyof M>(attr: N): AttributeBuilder<M, N, NumberConstructor>;
|
|
77
|
+
date<N extends keyof M>(attr: N): AttributeBuilder<M, N, DateConstructor>;
|
|
78
|
+
bigint<N extends keyof M>(attr: N): AttributeBuilder<M, N, BigIntConstructor>;
|
|
79
|
+
instance<N extends keyof M>(clazz: Constructor<any>, attr: N): AttributeBuilder<M, N, Constructor<any>>;
|
|
80
|
+
model<MM extends Model, N extends keyof M>(attr: N): ModelBuilder<MM>;
|
|
81
|
+
listOf<N extends keyof M>(attr: N, collection?: "Array" | "Set"): ListAttributeBuilder<M, N>;
|
|
82
|
+
build(): Constructor<M>;
|
|
83
|
+
static builder<M extends BuildableModel = BuildableModel>(): ModelBuilder<M>;
|
|
84
|
+
static from<N extends Model, M extends ExtendedMetadata<N>>(meta: M, name?: string): Constructor<N>;
|
|
85
|
+
}
|
|
86
|
+
export {};
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { BuilderRegistry } from "../utils/registry.cjs";
|
|
2
|
+
import { ModelErrorDefinition } from "./ModelErrorDefinition.cjs";
|
|
3
|
+
import { Comparable, Comparison, Hashable, ModelArg, ModelBuilderFunction, ModelConstructor, Serializable, Validatable } from "./types.cjs";
|
|
4
|
+
import { ConditionalAsync } from "../types/index.cjs";
|
|
5
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
6
|
+
/**
|
|
7
|
+
* @summary Abstract class representing a Validatable Model object
|
|
8
|
+
* @description Meant to be used as a base class for all Model classes
|
|
9
|
+
*
|
|
10
|
+
* Model objects must:
|
|
11
|
+
* - Have all their required properties marked with '!';
|
|
12
|
+
* - Have all their optional properties marked as '?':
|
|
13
|
+
*
|
|
14
|
+
* @param {ModelArg<Model>} model base object from which to populate properties from
|
|
15
|
+
*
|
|
16
|
+
* @class Model
|
|
17
|
+
* @category Model
|
|
18
|
+
* @abstract
|
|
19
|
+
* @implements Validatable
|
|
20
|
+
* @implements Serializable
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* class ClassName {
|
|
24
|
+
* @required()
|
|
25
|
+
* requiredPropertyName!: PropertyType;
|
|
26
|
+
*
|
|
27
|
+
* optionalPropertyName?: PropertyType;
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
export declare abstract class Model<Async extends boolean = false> implements Validatable<Async>, Serializable, Hashable, Comparable {
|
|
31
|
+
protected constructor(arg?: ModelArg<Model> | undefined);
|
|
32
|
+
isAsync(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* @description Validates the model object against its defined validation rules
|
|
35
|
+
* @summary Validates the object according to its decorated properties, returning any validation errors
|
|
36
|
+
*
|
|
37
|
+
* @param {any[]} [exceptions] - Properties in the object to be ignored for the validation. Marked as 'any' to allow for extension but expects strings
|
|
38
|
+
* @return {ModelErrorDefinition | undefined} - Returns a ModelErrorDefinition object if validation errors exist, otherwise undefined
|
|
39
|
+
*/
|
|
40
|
+
hasErrors(...exceptions: any[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* @description Determines if this model is equal to another object
|
|
43
|
+
* @summary Compare object equality recursively, checking all properties unless excluded
|
|
44
|
+
*
|
|
45
|
+
* @param {any} obj - Object to compare to
|
|
46
|
+
* @param {string[]} [exceptions] - Property names to be excluded from the comparison
|
|
47
|
+
* @return {boolean} - True if objects are equal, false otherwise
|
|
48
|
+
*/
|
|
49
|
+
equals(obj: any, ...exceptions: string[]): boolean;
|
|
50
|
+
compare<M extends Model>(this: M, other: M, ...exceptions: (keyof M)[]): Comparison<M> | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* @description Converts the model to a serialized string representation
|
|
53
|
+
* @summary Returns the serialized model according to the currently defined {@link Serializer}
|
|
54
|
+
*
|
|
55
|
+
* @return {string} - The serialized string representation of the model
|
|
56
|
+
*/
|
|
57
|
+
serialize(): string;
|
|
58
|
+
/**
|
|
59
|
+
* @description Provides a human-readable string representation of the model
|
|
60
|
+
* @summary Override the implementation for js's 'toString()' to provide a more useful representation
|
|
61
|
+
*
|
|
62
|
+
* @return {string} - A string representation of the model including its class name and JSON representation
|
|
63
|
+
* @override
|
|
64
|
+
*/
|
|
65
|
+
toString(): string;
|
|
66
|
+
/**
|
|
67
|
+
* @description Generates a hash string for the model object
|
|
68
|
+
* @summary Defines a default implementation for object hash, relying on a basic implementation based on Java's string hash
|
|
69
|
+
*
|
|
70
|
+
* @return {string} - A hash string representing the model
|
|
71
|
+
*/
|
|
72
|
+
hash(): string;
|
|
73
|
+
/**
|
|
74
|
+
* @description Converts a serialized string back into a model instance
|
|
75
|
+
* @summary Deserializes a Model from its string representation
|
|
76
|
+
*
|
|
77
|
+
* @param {string} str - The serialized string to convert back to a model
|
|
78
|
+
* @return {any} - The deserialized model instance
|
|
79
|
+
* @throws {Error} If it fails to parse the string, or if it fails to build the model
|
|
80
|
+
*/
|
|
81
|
+
static deserialize(str: string): any;
|
|
82
|
+
/**
|
|
83
|
+
* @description Copies properties from a source object to a model instance
|
|
84
|
+
* @summary Repopulates the Object properties with the ones from the new object
|
|
85
|
+
*
|
|
86
|
+
* @template T
|
|
87
|
+
* @param {T} self - The target model instance to update
|
|
88
|
+
* @param {T | Record<string, any>} [obj] - The source object containing properties to copy
|
|
89
|
+
* @return {T} - The updated model instance
|
|
90
|
+
*/
|
|
91
|
+
static fromObject<T extends Model<any>>(self: T, obj?: T | Record<string, any>): T;
|
|
92
|
+
/**
|
|
93
|
+
* @description Copies and rebuilds properties from a source object to a model instance, handling nested models
|
|
94
|
+
* @summary Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
|
|
95
|
+
*
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {T} self - The target model instance to update
|
|
98
|
+
* @param {T | Record<string, any>} [obj] - The source object containing properties to copy
|
|
99
|
+
* @return {T} - The updated model instance with rebuilt nested models
|
|
100
|
+
*
|
|
101
|
+
* @mermaid
|
|
102
|
+
* sequenceDiagram
|
|
103
|
+
* participant C as Client
|
|
104
|
+
* participant M as Model.fromModel
|
|
105
|
+
* participant B as Model.build
|
|
106
|
+
* participant R as Reflection
|
|
107
|
+
*
|
|
108
|
+
* C->>M: fromModel(self, obj)
|
|
109
|
+
* M->>M: Get attributes from self
|
|
110
|
+
* loop For each property
|
|
111
|
+
* M->>M: Copy property from obj to self
|
|
112
|
+
* alt Property is a model
|
|
113
|
+
* M->>M: Check if property is a model
|
|
114
|
+
* M->>B: build(property, modelType)
|
|
115
|
+
* B-->>M: Return built model
|
|
116
|
+
* else Property is a complex type
|
|
117
|
+
* M->>R: Get property decorators
|
|
118
|
+
* R-->>M: Return decorators
|
|
119
|
+
* M->>M: Filter type decorators
|
|
120
|
+
* alt Property is Array/Set with list decorator
|
|
121
|
+
* M->>M: Process each item in collection
|
|
122
|
+
* loop For each item
|
|
123
|
+
* M->>B: build(item, itemModelType)
|
|
124
|
+
* B-->>M: Return built model
|
|
125
|
+
* end
|
|
126
|
+
* else Property is another model type
|
|
127
|
+
* M->>B: build(property, propertyType)
|
|
128
|
+
* B-->>M: Return built model
|
|
129
|
+
* end
|
|
130
|
+
* end
|
|
131
|
+
* end
|
|
132
|
+
* M-->>C: Return updated self
|
|
133
|
+
*/
|
|
134
|
+
static fromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
135
|
+
/**
|
|
136
|
+
* @description Configures the global model builder function
|
|
137
|
+
* @summary Sets the Global {@link ModelBuilderFunction} used for building model instances
|
|
138
|
+
*
|
|
139
|
+
* @param {ModelBuilderFunction} [builder] - The builder function to set as the global builder
|
|
140
|
+
* @return {void}
|
|
141
|
+
*/
|
|
142
|
+
static setBuilder(builder?: ModelBuilderFunction): void;
|
|
143
|
+
/**
|
|
144
|
+
* @description Retrieves the currently configured global model builder function
|
|
145
|
+
* @summary Returns the current global {@link ModelBuilderFunction} used for building model instances
|
|
146
|
+
*
|
|
147
|
+
* @return {ModelBuilderFunction | undefined} - The current global builder function or undefined if not set
|
|
148
|
+
*/
|
|
149
|
+
static getBuilder(): ModelBuilderFunction | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* @description Provides access to the current model registry
|
|
152
|
+
* @summary Returns the current {@link ModelRegistryManager} instance, creating one if it doesn't exist
|
|
153
|
+
*
|
|
154
|
+
* @return {ModelRegistry<any>} - The current model registry, defaults to a new {@link ModelRegistryManager} if not set
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
private static getRegistry;
|
|
158
|
+
/**
|
|
159
|
+
* @description Configures the model registry to be used by the Model system
|
|
160
|
+
* @summary Sets the current model registry to a custom implementation
|
|
161
|
+
*
|
|
162
|
+
* @param {BuilderRegistry<any>} modelRegistry - The new implementation of Registry to use
|
|
163
|
+
* @return {void}
|
|
164
|
+
*/
|
|
165
|
+
static setRegistry(modelRegistry: BuilderRegistry<any>): void;
|
|
166
|
+
/**
|
|
167
|
+
* @description Registers a model constructor with the model registry
|
|
168
|
+
* @summary Registers new model classes to make them available for serialization and deserialization
|
|
169
|
+
*
|
|
170
|
+
* @template T
|
|
171
|
+
* @param {ModelConstructor<T>} constructor - The model constructor to register
|
|
172
|
+
* @param {string} [name] - Optional name to register the constructor under, defaults to constructor.name
|
|
173
|
+
* @return {void}
|
|
174
|
+
*
|
|
175
|
+
* @see ModelRegistry
|
|
176
|
+
*/
|
|
177
|
+
static register<T extends Model>(constructor: ModelConstructor<T>, name?: string): void;
|
|
178
|
+
/**
|
|
179
|
+
* @description Retrieves a registered model constructor by name
|
|
180
|
+
* @summary Gets a registered Model {@link ModelConstructor} from the model registry
|
|
181
|
+
*
|
|
182
|
+
* @template T
|
|
183
|
+
* @param {string} name - The name of the model constructor to retrieve
|
|
184
|
+
* @return {ModelConstructor<T> | undefined} - The model constructor if found, undefined otherwise
|
|
185
|
+
*
|
|
186
|
+
* @see ModelRegistry
|
|
187
|
+
*/
|
|
188
|
+
static get<T extends Model>(name: string): ModelConstructor<T> | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* @description Creates a model instance from a plain object
|
|
191
|
+
* @summary Builds a model instance using the model registry, optionally specifying the model class
|
|
192
|
+
*
|
|
193
|
+
* @template T
|
|
194
|
+
* @param {Record<string, any>} obj - The source object to build the model from
|
|
195
|
+
* @param {string} [clazz] - When provided, it will attempt to find the matching constructor by name
|
|
196
|
+
* @return {T} - The built model instance
|
|
197
|
+
* @throws {Error} If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
198
|
+
*
|
|
199
|
+
* @see ModelRegistry
|
|
200
|
+
*/
|
|
201
|
+
static build<T extends Model>(obj?: Record<string, any>, clazz?: string): T;
|
|
202
|
+
/**
|
|
203
|
+
* @description Retrieves all attribute names from a model class or instance
|
|
204
|
+
* @summary Gets all attributes defined in a model, traversing the prototype chain to include inherited attributes
|
|
205
|
+
*
|
|
206
|
+
* @template V
|
|
207
|
+
* @param {Constructor<V> | V} model - The model class or instance to get attributes from
|
|
208
|
+
* @return {string[]} - Array of attribute names defined in the model
|
|
209
|
+
*/
|
|
210
|
+
static getAttributes<V extends Model>(model: Constructor<V> | V): string[];
|
|
211
|
+
/**
|
|
212
|
+
* @description Compares two model instances for equality
|
|
213
|
+
* @summary Determines if two model instances are equal by comparing their properties
|
|
214
|
+
*
|
|
215
|
+
* @template M
|
|
216
|
+
* @param {M} obj1 - First model instance to compare
|
|
217
|
+
* @param {M} obj2 - Second model instance to compare
|
|
218
|
+
* @param {any[]} [exceptions] - Property names to exclude from comparison
|
|
219
|
+
* @return {boolean} - True if the models are equal, false otherwise
|
|
220
|
+
*/
|
|
221
|
+
static equals<M extends Model>(obj1: M, obj2: M, ...exceptions: any[]): boolean;
|
|
222
|
+
/**
|
|
223
|
+
* @description Validates a model instance against its validation rules
|
|
224
|
+
* @summary Checks if a model has validation errors, optionally ignoring specified properties
|
|
225
|
+
*
|
|
226
|
+
* @template M
|
|
227
|
+
* @param {M} model - The model instance to validate
|
|
228
|
+
* @param {boolean} async - A flag indicating whether validation should be asynchronous.
|
|
229
|
+
* @param {string[]} [propsToIgnore] - Properties to exclude from validation
|
|
230
|
+
* @return {ModelErrorDefinition | undefined} - Returns validation errors if any, otherwise undefined
|
|
231
|
+
*/
|
|
232
|
+
static hasErrors<M extends Model, Async extends boolean = false>(model: M, async: Async, ...propsToIgnore: string[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
|
|
233
|
+
/**
|
|
234
|
+
* @description Converts a model instance to a serialized string
|
|
235
|
+
* @summary Serializes a model instance using the configured serializer or the default one
|
|
236
|
+
*
|
|
237
|
+
* @template M
|
|
238
|
+
* @param {M} model - The model instance to serialize
|
|
239
|
+
* @return {string} - The serialized string representation of the model
|
|
240
|
+
*/
|
|
241
|
+
static serialize<M extends Model<boolean>>(model: M): any;
|
|
242
|
+
/**
|
|
243
|
+
* @description Generates a hash string for a model instance
|
|
244
|
+
* @summary Creates a hash representation of a model using the configured algorithm or the default one
|
|
245
|
+
*
|
|
246
|
+
* @template M
|
|
247
|
+
* @param {M} model - The model instance to hash
|
|
248
|
+
* @return {string} - The hash string representing the model
|
|
249
|
+
*/
|
|
250
|
+
static hash<M extends Model<boolean>>(model: M): any;
|
|
251
|
+
/**
|
|
252
|
+
* @description Determines if an object is a model instance or has model metadata
|
|
253
|
+
* @summary Checks whether a given object is either an instance of the Model class or
|
|
254
|
+
* has model metadata attached to it. This function is essential for serialization and
|
|
255
|
+
* deserialization processes, as it helps identify model objects that need special handling.
|
|
256
|
+
* It safely handles potential errors during metadata retrieval.
|
|
257
|
+
*
|
|
258
|
+
* @param {Record<string, any>} target - The object to check
|
|
259
|
+
* @return {boolean} True if the object is a model instance or has model metadata, false otherwise
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* // Check if an object is a model
|
|
264
|
+
* const user = new User({ name: "John" });
|
|
265
|
+
* const isUserModel = isModel(user); // true
|
|
266
|
+
*
|
|
267
|
+
* // Check a plain object
|
|
268
|
+
* const plainObject = { name: "John" };
|
|
269
|
+
* const isPlainObjectModel = isModel(plainObject); // false
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
static isModel(target: Record<string, any>): boolean;
|
|
273
|
+
/**
|
|
274
|
+
* @description Checks if a property of a model is itself a model or has a model type
|
|
275
|
+
* @summary Determines whether a specific property of a model instance is either a model instance
|
|
276
|
+
* or has a type that is registered as a model
|
|
277
|
+
*
|
|
278
|
+
* @template M
|
|
279
|
+
* @param {M} target - The model instance to check
|
|
280
|
+
* @param {string} attribute - The property name to check
|
|
281
|
+
* @return {boolean | string | undefined} - Returns true if the property is a model instance,
|
|
282
|
+
* the model name if the property has a model type, or undefined if not a model
|
|
283
|
+
*/
|
|
284
|
+
static isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
|
|
285
|
+
static describe<M extends Model>(model: Constructor<M>, key?: keyof M): string | undefined;
|
|
286
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { BuilderRegistry } from "../utils/registry.js";
|
|
2
|
+
import { ModelErrorDefinition } from "./ModelErrorDefinition.js";
|
|
3
|
+
import { Comparable, Comparison, Hashable, ModelArg, ModelBuilderFunction, ModelConstructor, Serializable, Validatable } from "./types.js";
|
|
4
|
+
import { ConditionalAsync } from "../types/index.js";
|
|
5
|
+
import { Constructor } from "@decaf-ts/decoration";
|
|
6
|
+
/**
|
|
7
|
+
* @summary Abstract class representing a Validatable Model object
|
|
8
|
+
* @description Meant to be used as a base class for all Model classes
|
|
9
|
+
*
|
|
10
|
+
* Model objects must:
|
|
11
|
+
* - Have all their required properties marked with '!';
|
|
12
|
+
* - Have all their optional properties marked as '?':
|
|
13
|
+
*
|
|
14
|
+
* @param {ModelArg<Model>} model base object from which to populate properties from
|
|
15
|
+
*
|
|
16
|
+
* @class Model
|
|
17
|
+
* @category Model
|
|
18
|
+
* @abstract
|
|
19
|
+
* @implements Validatable
|
|
20
|
+
* @implements Serializable
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* class ClassName {
|
|
24
|
+
* @required()
|
|
25
|
+
* requiredPropertyName!: PropertyType;
|
|
26
|
+
*
|
|
27
|
+
* optionalPropertyName?: PropertyType;
|
|
28
|
+
* }
|
|
29
|
+
*/
|
|
30
|
+
export declare abstract class Model<Async extends boolean = false> implements Validatable<Async>, Serializable, Hashable, Comparable {
|
|
31
|
+
protected constructor(arg?: ModelArg<Model> | undefined);
|
|
32
|
+
isAsync(): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* @description Validates the model object against its defined validation rules
|
|
35
|
+
* @summary Validates the object according to its decorated properties, returning any validation errors
|
|
36
|
+
*
|
|
37
|
+
* @param {any[]} [exceptions] - Properties in the object to be ignored for the validation. Marked as 'any' to allow for extension but expects strings
|
|
38
|
+
* @return {ModelErrorDefinition | undefined} - Returns a ModelErrorDefinition object if validation errors exist, otherwise undefined
|
|
39
|
+
*/
|
|
40
|
+
hasErrors(...exceptions: any[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* @description Determines if this model is equal to another object
|
|
43
|
+
* @summary Compare object equality recursively, checking all properties unless excluded
|
|
44
|
+
*
|
|
45
|
+
* @param {any} obj - Object to compare to
|
|
46
|
+
* @param {string[]} [exceptions] - Property names to be excluded from the comparison
|
|
47
|
+
* @return {boolean} - True if objects are equal, false otherwise
|
|
48
|
+
*/
|
|
49
|
+
equals(obj: any, ...exceptions: string[]): boolean;
|
|
50
|
+
compare<M extends Model>(this: M, other: M, ...exceptions: (keyof M)[]): Comparison<M> | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* @description Converts the model to a serialized string representation
|
|
53
|
+
* @summary Returns the serialized model according to the currently defined {@link Serializer}
|
|
54
|
+
*
|
|
55
|
+
* @return {string} - The serialized string representation of the model
|
|
56
|
+
*/
|
|
57
|
+
serialize(): string;
|
|
58
|
+
/**
|
|
59
|
+
* @description Provides a human-readable string representation of the model
|
|
60
|
+
* @summary Override the implementation for js's 'toString()' to provide a more useful representation
|
|
61
|
+
*
|
|
62
|
+
* @return {string} - A string representation of the model including its class name and JSON representation
|
|
63
|
+
* @override
|
|
64
|
+
*/
|
|
65
|
+
toString(): string;
|
|
66
|
+
/**
|
|
67
|
+
* @description Generates a hash string for the model object
|
|
68
|
+
* @summary Defines a default implementation for object hash, relying on a basic implementation based on Java's string hash
|
|
69
|
+
*
|
|
70
|
+
* @return {string} - A hash string representing the model
|
|
71
|
+
*/
|
|
72
|
+
hash(): string;
|
|
73
|
+
/**
|
|
74
|
+
* @description Converts a serialized string back into a model instance
|
|
75
|
+
* @summary Deserializes a Model from its string representation
|
|
76
|
+
*
|
|
77
|
+
* @param {string} str - The serialized string to convert back to a model
|
|
78
|
+
* @return {any} - The deserialized model instance
|
|
79
|
+
* @throws {Error} If it fails to parse the string, or if it fails to build the model
|
|
80
|
+
*/
|
|
81
|
+
static deserialize(str: string): any;
|
|
82
|
+
/**
|
|
83
|
+
* @description Copies properties from a source object to a model instance
|
|
84
|
+
* @summary Repopulates the Object properties with the ones from the new object
|
|
85
|
+
*
|
|
86
|
+
* @template T
|
|
87
|
+
* @param {T} self - The target model instance to update
|
|
88
|
+
* @param {T | Record<string, any>} [obj] - The source object containing properties to copy
|
|
89
|
+
* @return {T} - The updated model instance
|
|
90
|
+
*/
|
|
91
|
+
static fromObject<T extends Model<any>>(self: T, obj?: T | Record<string, any>): T;
|
|
92
|
+
/**
|
|
93
|
+
* @description Copies and rebuilds properties from a source object to a model instance, handling nested models
|
|
94
|
+
* @summary Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
|
|
95
|
+
*
|
|
96
|
+
* @template T
|
|
97
|
+
* @param {T} self - The target model instance to update
|
|
98
|
+
* @param {T | Record<string, any>} [obj] - The source object containing properties to copy
|
|
99
|
+
* @return {T} - The updated model instance with rebuilt nested models
|
|
100
|
+
*
|
|
101
|
+
* @mermaid
|
|
102
|
+
* sequenceDiagram
|
|
103
|
+
* participant C as Client
|
|
104
|
+
* participant M as Model.fromModel
|
|
105
|
+
* participant B as Model.build
|
|
106
|
+
* participant R as Reflection
|
|
107
|
+
*
|
|
108
|
+
* C->>M: fromModel(self, obj)
|
|
109
|
+
* M->>M: Get attributes from self
|
|
110
|
+
* loop For each property
|
|
111
|
+
* M->>M: Copy property from obj to self
|
|
112
|
+
* alt Property is a model
|
|
113
|
+
* M->>M: Check if property is a model
|
|
114
|
+
* M->>B: build(property, modelType)
|
|
115
|
+
* B-->>M: Return built model
|
|
116
|
+
* else Property is a complex type
|
|
117
|
+
* M->>R: Get property decorators
|
|
118
|
+
* R-->>M: Return decorators
|
|
119
|
+
* M->>M: Filter type decorators
|
|
120
|
+
* alt Property is Array/Set with list decorator
|
|
121
|
+
* M->>M: Process each item in collection
|
|
122
|
+
* loop For each item
|
|
123
|
+
* M->>B: build(item, itemModelType)
|
|
124
|
+
* B-->>M: Return built model
|
|
125
|
+
* end
|
|
126
|
+
* else Property is another model type
|
|
127
|
+
* M->>B: build(property, propertyType)
|
|
128
|
+
* B-->>M: Return built model
|
|
129
|
+
* end
|
|
130
|
+
* end
|
|
131
|
+
* end
|
|
132
|
+
* M-->>C: Return updated self
|
|
133
|
+
*/
|
|
134
|
+
static fromModel<T extends Model>(self: T, obj?: T | Record<string, any>): T;
|
|
135
|
+
/**
|
|
136
|
+
* @description Configures the global model builder function
|
|
137
|
+
* @summary Sets the Global {@link ModelBuilderFunction} used for building model instances
|
|
138
|
+
*
|
|
139
|
+
* @param {ModelBuilderFunction} [builder] - The builder function to set as the global builder
|
|
140
|
+
* @return {void}
|
|
141
|
+
*/
|
|
142
|
+
static setBuilder(builder?: ModelBuilderFunction): void;
|
|
143
|
+
/**
|
|
144
|
+
* @description Retrieves the currently configured global model builder function
|
|
145
|
+
* @summary Returns the current global {@link ModelBuilderFunction} used for building model instances
|
|
146
|
+
*
|
|
147
|
+
* @return {ModelBuilderFunction | undefined} - The current global builder function or undefined if not set
|
|
148
|
+
*/
|
|
149
|
+
static getBuilder(): ModelBuilderFunction | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* @description Provides access to the current model registry
|
|
152
|
+
* @summary Returns the current {@link ModelRegistryManager} instance, creating one if it doesn't exist
|
|
153
|
+
*
|
|
154
|
+
* @return {ModelRegistry<any>} - The current model registry, defaults to a new {@link ModelRegistryManager} if not set
|
|
155
|
+
* @private
|
|
156
|
+
*/
|
|
157
|
+
private static getRegistry;
|
|
158
|
+
/**
|
|
159
|
+
* @description Configures the model registry to be used by the Model system
|
|
160
|
+
* @summary Sets the current model registry to a custom implementation
|
|
161
|
+
*
|
|
162
|
+
* @param {BuilderRegistry<any>} modelRegistry - The new implementation of Registry to use
|
|
163
|
+
* @return {void}
|
|
164
|
+
*/
|
|
165
|
+
static setRegistry(modelRegistry: BuilderRegistry<any>): void;
|
|
166
|
+
/**
|
|
167
|
+
* @description Registers a model constructor with the model registry
|
|
168
|
+
* @summary Registers new model classes to make them available for serialization and deserialization
|
|
169
|
+
*
|
|
170
|
+
* @template T
|
|
171
|
+
* @param {ModelConstructor<T>} constructor - The model constructor to register
|
|
172
|
+
* @param {string} [name] - Optional name to register the constructor under, defaults to constructor.name
|
|
173
|
+
* @return {void}
|
|
174
|
+
*
|
|
175
|
+
* @see ModelRegistry
|
|
176
|
+
*/
|
|
177
|
+
static register<T extends Model>(constructor: ModelConstructor<T>, name?: string): void;
|
|
178
|
+
/**
|
|
179
|
+
* @description Retrieves a registered model constructor by name
|
|
180
|
+
* @summary Gets a registered Model {@link ModelConstructor} from the model registry
|
|
181
|
+
*
|
|
182
|
+
* @template T
|
|
183
|
+
* @param {string} name - The name of the model constructor to retrieve
|
|
184
|
+
* @return {ModelConstructor<T> | undefined} - The model constructor if found, undefined otherwise
|
|
185
|
+
*
|
|
186
|
+
* @see ModelRegistry
|
|
187
|
+
*/
|
|
188
|
+
static get<T extends Model>(name: string): ModelConstructor<T> | undefined;
|
|
189
|
+
/**
|
|
190
|
+
* @description Creates a model instance from a plain object
|
|
191
|
+
* @summary Builds a model instance using the model registry, optionally specifying the model class
|
|
192
|
+
*
|
|
193
|
+
* @template T
|
|
194
|
+
* @param {Record<string, any>} obj - The source object to build the model from
|
|
195
|
+
* @param {string} [clazz] - When provided, it will attempt to find the matching constructor by name
|
|
196
|
+
* @return {T} - The built model instance
|
|
197
|
+
* @throws {Error} If clazz is not found, or obj is not a {@link Model} meaning it has no {@link ModelKeys.ANCHOR} property
|
|
198
|
+
*
|
|
199
|
+
* @see ModelRegistry
|
|
200
|
+
*/
|
|
201
|
+
static build<T extends Model>(obj?: Record<string, any>, clazz?: string): T;
|
|
202
|
+
/**
|
|
203
|
+
* @description Retrieves all attribute names from a model class or instance
|
|
204
|
+
* @summary Gets all attributes defined in a model, traversing the prototype chain to include inherited attributes
|
|
205
|
+
*
|
|
206
|
+
* @template V
|
|
207
|
+
* @param {Constructor<V> | V} model - The model class or instance to get attributes from
|
|
208
|
+
* @return {string[]} - Array of attribute names defined in the model
|
|
209
|
+
*/
|
|
210
|
+
static getAttributes<V extends Model>(model: Constructor<V> | V): string[];
|
|
211
|
+
/**
|
|
212
|
+
* @description Compares two model instances for equality
|
|
213
|
+
* @summary Determines if two model instances are equal by comparing their properties
|
|
214
|
+
*
|
|
215
|
+
* @template M
|
|
216
|
+
* @param {M} obj1 - First model instance to compare
|
|
217
|
+
* @param {M} obj2 - Second model instance to compare
|
|
218
|
+
* @param {any[]} [exceptions] - Property names to exclude from comparison
|
|
219
|
+
* @return {boolean} - True if the models are equal, false otherwise
|
|
220
|
+
*/
|
|
221
|
+
static equals<M extends Model>(obj1: M, obj2: M, ...exceptions: any[]): boolean;
|
|
222
|
+
/**
|
|
223
|
+
* @description Validates a model instance against its validation rules
|
|
224
|
+
* @summary Checks if a model has validation errors, optionally ignoring specified properties
|
|
225
|
+
*
|
|
226
|
+
* @template M
|
|
227
|
+
* @param {M} model - The model instance to validate
|
|
228
|
+
* @param {boolean} async - A flag indicating whether validation should be asynchronous.
|
|
229
|
+
* @param {string[]} [propsToIgnore] - Properties to exclude from validation
|
|
230
|
+
* @return {ModelErrorDefinition | undefined} - Returns validation errors if any, otherwise undefined
|
|
231
|
+
*/
|
|
232
|
+
static hasErrors<M extends Model, Async extends boolean = false>(model: M, async: Async, ...propsToIgnore: string[]): ConditionalAsync<Async, ModelErrorDefinition | undefined>;
|
|
233
|
+
/**
|
|
234
|
+
* @description Converts a model instance to a serialized string
|
|
235
|
+
* @summary Serializes a model instance using the configured serializer or the default one
|
|
236
|
+
*
|
|
237
|
+
* @template M
|
|
238
|
+
* @param {M} model - The model instance to serialize
|
|
239
|
+
* @return {string} - The serialized string representation of the model
|
|
240
|
+
*/
|
|
241
|
+
static serialize<M extends Model<boolean>>(model: M): any;
|
|
242
|
+
/**
|
|
243
|
+
* @description Generates a hash string for a model instance
|
|
244
|
+
* @summary Creates a hash representation of a model using the configured algorithm or the default one
|
|
245
|
+
*
|
|
246
|
+
* @template M
|
|
247
|
+
* @param {M} model - The model instance to hash
|
|
248
|
+
* @return {string} - The hash string representing the model
|
|
249
|
+
*/
|
|
250
|
+
static hash<M extends Model<boolean>>(model: M): any;
|
|
251
|
+
/**
|
|
252
|
+
* @description Determines if an object is a model instance or has model metadata
|
|
253
|
+
* @summary Checks whether a given object is either an instance of the Model class or
|
|
254
|
+
* has model metadata attached to it. This function is essential for serialization and
|
|
255
|
+
* deserialization processes, as it helps identify model objects that need special handling.
|
|
256
|
+
* It safely handles potential errors during metadata retrieval.
|
|
257
|
+
*
|
|
258
|
+
* @param {Record<string, any>} target - The object to check
|
|
259
|
+
* @return {boolean} True if the object is a model instance or has model metadata, false otherwise
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* // Check if an object is a model
|
|
264
|
+
* const user = new User({ name: "John" });
|
|
265
|
+
* const isUserModel = isModel(user); // true
|
|
266
|
+
*
|
|
267
|
+
* // Check a plain object
|
|
268
|
+
* const plainObject = { name: "John" };
|
|
269
|
+
* const isPlainObjectModel = isModel(plainObject); // false
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
static isModel(target: Record<string, any>): boolean;
|
|
273
|
+
/**
|
|
274
|
+
* @description Checks if a property of a model is itself a model or has a model type
|
|
275
|
+
* @summary Determines whether a specific property of a model instance is either a model instance
|
|
276
|
+
* or has a type that is registered as a model
|
|
277
|
+
*
|
|
278
|
+
* @template M
|
|
279
|
+
* @param {M} target - The model instance to check
|
|
280
|
+
* @param {string} attribute - The property name to check
|
|
281
|
+
* @return {boolean | string | undefined} - Returns true if the property is a model instance,
|
|
282
|
+
* the model name if the property has a model type, or undefined if not a model
|
|
283
|
+
*/
|
|
284
|
+
static isPropertyModel<M extends Model>(target: M, attribute: string): boolean | string | undefined;
|
|
285
|
+
static describe<M extends Model>(model: Constructor<M>, key?: keyof M): string | undefined;
|
|
286
|
+
}
|