Skip to content

Commit

Permalink
Merge pull request #78 from xpepermint/master
Browse files Browse the repository at this point in the history
Normalization upgrade
  • Loading branch information
xpepermint committed Oct 3, 2019
2 parents 202b62c + 68b9d2c commit 07d9fb8
Show file tree
Hide file tree
Showing 57 changed files with 443 additions and 296 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Always fork the repo and create your branch from master. If you've added code th

Please follow the [TypeScript coding guidelines](https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines).

You should prefix all private variables with `_` sign. To prevent accidental name collisions with your code Rawmodel somes prefixes names of public objects with `$` and names of private objects with `$$`. You can find this convention in outer frameworks like [AngularJS](https://docs.angularjs.org/api).

## Release process

The release manager will publish packages to NPM using these commands.
Expand Down
102 changes: 51 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import { stringParser } from '@rawmodel/parsers';

class User extends Model {
@prop({
parse: {
parser: {
resolver: stringParser(),
},
})
Expand Down Expand Up @@ -102,13 +102,13 @@ class Friend extends Model {

class User extends Model {
@prop({
parse: {
parser: {
resolver: Address,
},
})
public address: Address;
@prop({
parse: {
parser: {
array: true,
resolver: Friend,
},
Expand Down Expand Up @@ -162,15 +162,15 @@ A property can have a custom `getter` and a custom `setter`. This function share

```ts
@prop({
get(value) { return value },
set(value) { return value },
getter(value) { return value },
setter(value) { return value },
})
public name: string;
```

### Value Assignments

Model's properties are like properties on a Javascript Object. We can easily assign a value to a property through its setter method (e.g. `model.name = 'value';`). Instead of assigning properties one by one, we can use the `populate()` method as shown below.
Model's properties are like properties of a Javascript Object. We can easily assign a value to a property through its setter method (e.g. `model.name = 'value';`). Instead of assigning properties one by one, we can use the `populate()` method to assign values to multiple enumerable properties.

```ts
model.populate({
Expand Down Expand Up @@ -213,7 +213,7 @@ It's encouraged to use the `populate()` method for assigning values unless you k

### Serialization & Filtering

Model provides useful methods for object serialization and filtering. All properties are serializable by default and are thus included in the result object returned by the `serialize()` method. We can customize the output and include or exclude properties for different situations by using serialization strategies.
Model provides useful methods for object serialization and filtering. All enumerable properties are serializable by default and are thus included in the result object returned by the `serialize()` method. We can customize the output and include or exclude properties for different situations by using serialization strategies.

```ts
class User extends Model {
Expand Down Expand Up @@ -272,7 +272,7 @@ RawModel provides a simple mechanism for validating properties. All validators s
```ts
class User extends Model {
@prop({
validate: [ // property validation setup
validators: [ // property validation setup
{ // validator recipe
resolver(v) { return !!v }, // [required] validator function
code: 422, // [optional] error code
Expand All @@ -295,7 +295,7 @@ RawModel provides a mechanism for handling property-related errors. The logic is
```ts
class User extends Model {
@prop({
handle: [ // property error handling setup
handlers: [ // property error handling setup
{ // handler recipe
resolver(e) { return e.message === 'foo' }, // [required] error resolve function
code: 31000, // [optional] error code
Expand Down Expand Up @@ -366,7 +366,7 @@ const schema = {
props: [ // schema properties
{ // property definition
name: 'title', // property name
validate: [
validators: [
{
resolver: 'stringLength', // validator resolver name
code: 30001, // validation error code
Expand Down Expand Up @@ -418,14 +418,14 @@ graphql(schema, '{ hello }', root).then((response) => {
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| config.$.name | String | Yes | - | Property name.
| config.$.prop.set | Function | No | - | Custom setter.
| config.$.prop.get | Function | No | - | Custom getter.
| config.$.prop.parse | Parser | No | - | Data type parser (see supported types).
| config.$.prop.setter | Function | No | - | Custom setter.
| config.$.prop.getter | Function | No | - | Custom getter.
| config.$.prop.parser | Parser | No | - | Data type parser (see supported types).
| config.$.prop.defaultValue | Any | No | - | Prop default value.
| config.$.prop.fakeValue | Any | No | - | Prop fake value.
| config.$.prop.emptyValue | Any | No | - | Prop empty value.
| config.$.prop.validate | Array | No | - | List of validator recipes.
| config.$.prop.handle | Array | No | - | List of error handler recipes.
| config.$.prop.validators | Array | No | - | List of validator recipes.
| config.$.prop.handlers | Array | No | - | List of error handler recipes.
| config.$.prop.populatable | String[] | No | - | List of strategies for populating the property value.
| config.$.prop.serializable | String[] | No | - | List of strategies for serializing the property value.
| config.$.prop.enumerable | Boolean | No | true | Indicates that the property is enumerable.
Expand Down Expand Up @@ -456,20 +456,20 @@ class User extends Model {
@prop({
set(v) { return v; }, // [optional] custom setter
get(v) { return v; }, // [optional] custom getter
parse: { // [optional] property type casting
parser: { // [optional] property type casting
array: true, // [optional] forces to array conversion when `true`
resolver: User, // [optional] parser function or Model
},
defaultValue: 'Noname', // [optional] property default value (value or function)
fakeValue: 'Noname', // [optional] property fake value (value or function)
emptyValue: '', // [optional] property empty value (value or function)
validate: [ // [optional] value validator recipes
validators: [ // [optional] value validator recipes
{ // validator recipe (check validatable.js for more)
resolver(v) { return !!v; }, // [required] validator resolve function (supports async)
code: 422, // [optional] error code
},
],
handle: [ // [optional] error handling recipies
handlers: [ // [optional] error handling recipies
{ // handler recipe
resolver(e) { return e.message === 'foo'; }, // [required] handler resolve function (supports async)
code: 31000, // [required] error code
Expand All @@ -489,14 +489,14 @@ class User extends Model {
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| config.set | Function | No | - | Custom setter.
| config.get | Function | No | - | Custom getter.
| config.parse | Parser | No | - | Data type parser (see supported types).
| config.setter | Function | No | - | Custom setter.
| config.getter | Function | No | - | Custom getter.
| config.parser | Parser | No | - | Data type parser (see supported types).
| config.defaultValue | Any | No | - | Prop default value.
| config.fakeValue | Any | No | - | Prop fake value.
| config.emptyValue | Any | No | - | Prop empty value.
| config.validate | Array | No | - | List of validator recipes.
| config.handle | Array | No | - | List of error handler recipes.
| config.validators | Array | No | - | List of validator recipes.
| config.handlers | Array | No | - | List of error handler recipes.
| config.populatable | String[] | No | - | List of strategies for populating the property value.
| config.serializable | String[] | No | - | List of strategies for serializing the property value.
| config.enumerable | Boolean | No | true | Indicates that the property is enumerable.
Expand Down Expand Up @@ -571,6 +571,10 @@ user.flatten(); // -> [{ path, prop, value }, ...]

> Makes each model property not settable.
**Model.prototype.getAncestors()**: Model[]

> Returns a list of all parent model instances.
**Model.prototype.getContext()**: Context

> Returns model context data.
Expand All @@ -587,10 +591,6 @@ user.flatten(); // -> [{ path, prop, value }, ...]
|--------|------|----------|---------|------------
| keys | Array | Yes | - | Path to a property (e.g. `['book', 0, 'title']`).

**Model.prototype.getRoot()**: Model

> Returns the first model instance in a tree of models.
**Model.prototype.handle(error, { quiet }): Promise(Model)**

> Tries to handle the `error` against each property handlers and populates the model with possible errors.
Expand Down Expand Up @@ -638,7 +638,7 @@ try {
**Model.prototype.populate(data, strategy)**: Model

> Applies data to a model.
> Populates enumerable properties with data.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
Expand All @@ -655,7 +655,7 @@ try {
**Model.prototype.serialize(strategy)**: Object

> Converts a model into serialized data object.
> Converts a model into serialized data object. The result will include only enumerable properties.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
Expand Down Expand Up @@ -683,14 +683,14 @@ try {
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
| config.set | Function | No | - | Custom setter.
| config.get | Function | No | - | Custom getter.
| config.parse | Parser | No | - | Data type parser (see supported types).
| config.setter | Function | No | - | Custom setter.
| config.getter | Function | No | - | Custom getter.
| config.parser | Parser | No | - | Data type parser (see supported types).
| config.defaultValue | Any | No | - | Prop default value.
| config.fakeValue | Any | No | - | Prop fake value.
| config.emptyValue | Any | No | - | Prop empty value.
| config.validate | Array | No | - | List of validator recipes.
| config.handle | Array | No | - | List of error handler recipes.
| config.validators | Array | No | - | List of validator recipes.
| config.handlers | Array | No | - | List of error handler recipes.
| config.populatable | String[] | No | - | List of strategies for populating the property value.
| config.serializable | String[] | No | - | List of strategies for serializing the property value.
| config.enumerable | Boolean | No | true | Indicates that the property is enumerable.
Expand Down Expand Up @@ -794,7 +794,7 @@ try {
**Prop.prototype.serialize(strategy)**

> Returns a serialized property value.
> Returns a serialized property value. Note that only enumerable properties are serializable.
| Option | Type | Required | Default | Description
|--------|------|----------|---------|------------
Expand Down Expand Up @@ -831,20 +831,20 @@ try {
| recipe.props | Array | No | - | Hash of property definitions.
| recipe.props.$.set | String | No | - | Setter resolver name.
| recipe.props.$.get | String | No | - | Getter resolver name.
| recipe.props.$.parse | Object | No | - | Data type parser recipe.
| recipe.props.$.parse.array | Boolean | No | false | When `true` the input data will automatically be converted to array.
| recipe.props.$.parse.resolver | String | No | - | Parser resolver name
| recipe.props.$.parser | Object | No | - | Data type parser recipe.
| recipe.props.$.parser.array | Boolean | No | false | When `true` the input data will automatically be converted to array.
| recipe.props.$.parser.resolver | String | No | - | Parser resolver name
| recipe.props.$.defaultValue | Any | No | - | Default value resolver name or a value.
| recipe.props.$.fakeValue | Any | No | - | Fake value resolver name or a value.
| recipe.props.$.emptyValue | Any | No | - | Empty value resolver name or a value.
| recipe.props.$.validate | Array | No | - | List of validator recipes.
| recipe.props.$.validate.code | Integer | Yes | - | Validator error code.
| recipe.props.$.validate.resolver | String | Yes | - | Validator resolver name.
| recipe.props.$.validate.options | Object | No | - | Validator resolver arguments.
| recipe.props.$.handle | Array | No | - | List of error handler recipes.
| recipe.props.$.handle.code | Integer | Yes | - | Handler error code.
| recipe.props.$.handle.resolver | String | Yes | - | Handler resolver name.
| recipe.props.$.handle.options | Object | No | - | Handler resolver arguments.
| recipe.props.$.validators | Array | No | - | List of validator recipes.
| recipe.props.$.validators.code | Integer | Yes | - | Validator error code.
| recipe.props.$.validators.resolver | String | Yes | - | Validator resolver name.
| recipe.props.$.validators.options | Object | No | - | Validator resolver arguments.
| recipe.props.$.handlers | Array | No | - | List of error handler recipes.
| recipe.props.$.handlers.code | Integer | Yes | - | Handler error code.
| recipe.props.$.handlers.resolver | String | Yes | - | Handler resolver name.
| recipe.props.$.handlers.options | Object | No | - | Handler resolver arguments.
| recipe.props.$.populatable | Array | No | - | List of strategies for populating the property value.
| recipe.props.$.serializable | Array | No | - | List of strategies for serializing the property value.
| recipe.props.$.enumerable | Boolean | No | true | Indicates that the property is enumerable.
Expand Down Expand Up @@ -879,22 +879,22 @@ const Model = createModelClass({
handlers: {}, // see validators
props: [
name: 'firstName', // property name
get: 'customGetter', // getter name (defined in `getters`)
set: 'customSetter', // setter name (defined in `setters`)
parse: {
getter: 'customGetter', // getter name (defined in `getters`)
setter: 'customSetter', // setter name (defined in `setters`)
parser: {
array: true, // when `true` the input is converted to array
resolver: 'toString', // parser resolver name
},
defaultValue: 'none', // static default value
fakeValue: 'none', // static fake value
emptyValue: '', // static empty value
validate: [
validators: [
{
code: 30001, // validator error code
resolver: 'isPresent', // validator resolver name
},
],
handle: [], // see validators
handlers: [], // see validators
populatable: ['input', 'db'], // populatable strategies
serializable: ['input', 'db'], // serializable strategies
enumerable: true, // property is enumerable
Expand Down
26 changes: 13 additions & 13 deletions common/config/rush/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/config/rush/version-policies.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"policyName": "patchAll",
"definitionName": "lockStepVersion",
"version": "3.2.1",
"version": "3.3.0",
"nextBump": "patch"
}
]
6 changes: 6 additions & 0 deletions packages/rawmodel-core/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"name": "@rawmodel/core",
"entries": [
{
"version": "3.3.0",
"tag": "@rawmodel/core_v3.3.0",
"date": "Thu, 03 Oct 2019 15:49:09 GMT",
"comments": {}
},
{
"version": "3.2.1",
"tag": "@rawmodel/core_v3.2.1",
Expand Down
7 changes: 6 additions & 1 deletion packages/rawmodel-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Change Log - @rawmodel/core

This log was last generated on Sun, 29 Sep 2019 10:22:42 GMT and should not be manually modified.
This log was last generated on Thu, 03 Oct 2019 15:49:09 GMT and should not be manually modified.

## 3.3.0
Thu, 03 Oct 2019 15:49:09 GMT

*Version update only*

## 3.2.1
Sun, 29 Sep 2019 10:22:42 GMT
Expand Down
Loading

0 comments on commit 07d9fb8

Please sign in to comment.