-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91f84aa
commit 6165450
Showing
6 changed files
with
62 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
## ModelBuilder | ||
|
||
The [[ModelBuilder]] interface exposes two methods for working with models. | ||
[[ModelBuilder.constructModel]] is used for converting a POJO ("Plain | ||
Old JavaScript Object") into an instance of a desired model class. | ||
[[ModelBuilder.constructMember]] is used to convert individual values to | ||
a specified type. | ||
|
||
## ModelBuilderOptions | ||
|
||
Providing options to `constructModel` or `constructMember` calls will | ||
allow you to configure validation, transform the keys of your source | ||
object, or for `constructModel`, transform the source object itself | ||
before construction. | ||
|
||
- **`validators`:** An array of [[ModelValidator]] instances that will be | ||
used to validate the constructed model. | ||
- **`keyTransform`:** A `KeyTransformFn` that will be used to modify the | ||
keys of the source object when mapping properties to the constructed | ||
model. This is useful when dealing with external data sources that | ||
provide data is in one case (e.g. `snake_case`), and your models are | ||
written in another (hopefully, `camelCase`). | ||
- **`dataTransformers`:** An array of [[DataTransformer]] instances that | ||
will modify the source object before starting construction of the model. | ||
This can be used to do just about anything, but as an example, it can | ||
be used with [[@dandi-contrib/data-pg]] and [[NestedKeyTransformer]] | ||
to convert a flat POJO with dot notated keys to a complex object with | ||
nested properties. | ||
|
||
## MetadataModelBuilder | ||
|
||
[[MetadataModelBuilder]] is the default implementation of [[ModelBuilder]] | ||
and is included in the [[@dandi/model-builder]] package. | ||
|
||
[[MetadataModelBuilder]] uses the metadata generated by using decorators | ||
from [[@dandi/model]] to define a model. Some things to note about using | ||
[[MetadataModelBuilder]]: | ||
|
||
- Nested models are supported - it will recurse into each complex type | ||
and convert any properties defined by the [[@dandi/model]] decorators. | ||
- When using the `keyTransform` option, it will _not_ transform the keys | ||
of `Map` properties (defined with [[@MapOf()]]), or JSON properties | ||
(specified by adding [[@Json()]]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/** | ||
* A service that transforms source objects before they are used for constructing and | ||
* validating models with [[ModelBuilder]] | ||
*/ | ||
export interface DataTransformer { | ||
transform(source: any): any; | ||
transform(source: any): any | ||
} |