# abstract new Model(model)
Abstract class representing a Validatable Model object
Meant to be used as a base class for all Model classes
Model objects must:
- Have all their required properties marked with '!';
- Have all their optional properties marked as '?':
Parameters:
Name | Type | Description |
---|---|---|
model |
ModelArg.<Model>
|
base object from which to populate properties from |
- Implements:
Example
class ClassName {
Methods
# equals(obj, exceptionsopt) → {boolean}
Compare object equality recursively, checking all properties unless excluded
Determines if this model is equal to another object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
any
|
Object to compare to |
|
exceptions |
Array.<string>
|
<optional> |
Property names to be excluded from the comparison |
- True if objects are equal, false otherwise
boolean
# hasErrors(exceptionsopt) → {ModelErrorDefinition|undefined}
Validates the object according to its decorated properties, returning any validation errors
Validates the model object against its defined validation rules
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
exceptions |
Array.<any>
|
<optional> |
Properties in the object to be ignored for the validation. Marked as 'any' to allow for extension but expects strings |
- Implements:
- Returns a ModelErrorDefinition object if validation errors exist, otherwise undefined
ModelErrorDefinition
|
undefined
# hash() → {string}
Defines a default implementation for object hash, relying on a basic implementation based on Java's string hash
Generates a hash string for the model object
- A hash string representing the model
string
# serialize() → {string}
Returns the serialized model according to the currently defined Serializer
Converts the model to a serialized string representation
- Implements:
- The serialized string representation of the model
string
# toString() → {string}
Override the implementation for js's 'toString()' to provide a more useful representation
Provides a human-readable string representation of the model
- A string representation of the model including its class name and JSON representation
string
# static build(obj, clazzopt) → {T}
Builds a model instance using the model registry, optionally specifying the model class
Creates a model instance from a plain object
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Record.<string, any>
|
The source object to build the model from |
|
clazz |
string
|
<optional> |
When provided, it will attempt to find the matching constructor by name |
- See:
-
- ModelRegistry
If clazz is not found, or obj is not a Model
meaning it has no ModelKeys.ANCHOR
property
Error
- The built model instance
T
# static deserialize(str) → {any}
Deserializes a Model from its string representation
Converts a serialized string back into a model instance
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
The serialized string to convert back to a model |
If it fails to parse the string, or if it fails to build the model
Error
- The deserialized model instance
any
# static equals(obj1, obj2, exceptionsopt) → {boolean}
Determines if two model instances are equal by comparing their properties
Compares two model instances for equality
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj1 |
M
|
First model instance to compare |
|
obj2 |
M
|
Second model instance to compare |
|
exceptions |
Array.<any>
|
<optional> |
Property names to exclude from comparison |
- True if the models are equal, false otherwise
boolean
# static fromModel(self, objopt) → {T}
Repopulates the instance with properties from the new Model Object, recursively rebuilding nested models
Copies and rebuilds properties from a source object to a model instance, handling nested models
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
self |
T
|
The target model instance to update |
|
obj |
T
|
Record.<string, any>
|
<optional> |
The source object containing properties to copy |
- The updated model instance with rebuilt nested models
T
# static fromObject(self, objopt) → {T}
Repopulates the Object properties with the ones from the new object
Copies properties from a source object to a model instance
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
self |
T
|
The target model instance to update |
|
obj |
T
|
Record.<string, any>
|
<optional> |
The source object containing properties to copy |
- The updated model instance
T
# static get(name) → {ModelConstructor.<T>|undefined}
Gets a registered Model ModelConstructor
from the model registry
Retrieves a registered model constructor by name
Parameters:
Name | Type | Description |
---|---|---|
name |
string
|
The name of the model constructor to retrieve |
- See:
-
- ModelRegistry
- The model constructor if found, undefined otherwise
ModelConstructor.<T>
|
undefined
# static getAttributes(model) → {Array.<string>}
Gets all attributes defined in a model, traversing the prototype chain to include inherited attributes
Retrieves all attribute names from a model class or instance
Parameters:
Name | Type | Description |
---|---|---|
model |
Constructor.<V>
|
V
|
The model class or instance to get attributes from |
- Array of attribute names defined in the model
Array.<string>
# static getBuilder() → {ModelBuilderFunction|undefined}
Returns the current global ModelBuilderFunction
used for building model instances
Retrieves the currently configured global model builder function
- The current global builder function or undefined if not set
ModelBuilderFunction
|
undefined
# static getMetadata(model) → {string}
Gets the metadata associated with a model instance, typically the model class name
Retrieves the model metadata from a model instance
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance to get metadata from |
- The model metadata (typically the class name)
string
# static hasErrors(model, async, propsToIgnoreopt) → {ModelErrorDefinition|undefined}
Checks if a model has validation errors, optionally ignoring specified properties
Validates a model instance against its validation rules
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
model |
M
|
The model instance to validate |
|
async |
boolean
|
A flag indicating whether validation should be asynchronous. |
|
propsToIgnore |
Array.<string>
|
<optional> |
Properties to exclude from validation |
- Returns validation errors if any, otherwise undefined
ModelErrorDefinition
|
undefined
# static hash(model) → {string}
Creates a hash representation of a model using the configured algorithm or the default one
Generates a hash string for a model instance
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance to hash |
- The hash string representing the model
string
# static isModel(target) → {boolean}
Checks whether a given object is either an instance of the Model class or has model metadata attached to it. This function is essential for serialization and deserialization processes, as it helps identify model objects that need special handling. It safely handles potential errors during metadata retrieval.
Determines if an object is a model instance or has model metadata
Parameters:
Name | Type | Description |
---|---|---|
target |
Record.<string, any>
|
The object to check |
True if the object is a model instance or has model metadata, false otherwise
boolean
Example
```typescript
// Check if an object is a model
const user = new User({ name: "John" });
const isUserModel = isModel(user); // true
// Check a plain object
const plainObject = { name: "John" };
const isPlainObjectModel = isModel(plainObject); // false
```
# static isPropertyModel(target, attribute) → {boolean|string|undefined}
Determines whether a specific property of a model instance is either a model instance or has a type that is registered as a model
Checks if a property of a model is itself a model or has a model type
Parameters:
Name | Type | Description |
---|---|---|
target |
M
|
The model instance to check |
attribute |
string
|
The property name to check |
- Returns true if the property is a model instance, the model name if the property has a model type, or undefined if not a model
boolean
|
string
|
undefined
# static key(str) → {string}
Builds the key to store as Metadata under Reflections
Creates a metadata key for use with the Reflection API
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
The base key to concatenate with the model reflection prefix |
- The complete metadata key
string
# static register(constructor, nameopt) → {void}
Registers new model classes to make them available for serialization and deserialization
Registers a model constructor with the model registry
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
constructor |
ModelConstructor.<T>
|
The model constructor to register |
|
name |
string
|
<optional> |
Optional name to register the constructor under, defaults to constructor.name |
- See:
-
- ModelRegistry
void
# static serialize(model) → {string}
Serializes a model instance using the configured serializer or the default one
Converts a model instance to a serialized string
Parameters:
Name | Type | Description |
---|---|---|
model |
M
|
The model instance to serialize |
- The serialized string representation of the model
string
# static setBuilder(builderopt) → {void}
Sets the Global ModelBuilderFunction
used for building model instances
Configures the global model builder function
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
builder |
ModelBuilderFunction
|
<optional> |
The builder function to set as the global builder |
void
# static setRegistry(modelRegistry) → {void}
Sets the current model registry to a custom implementation
Configures the model registry to be used by the Model system
Parameters:
Name | Type | Description |
---|---|---|
modelRegistry |
BuilderRegistry.<any>
|
The new implementation of Registry to use |
void