@commodo/fields
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -26,6 +26,6 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "object", { | ||
Object.defineProperty(exports, "fields", { | ||
enumerable: true, | ||
get: function () { | ||
return _object.default; | ||
return _fields.default; | ||
} | ||
@@ -52,3 +52,3 @@ }); | ||
var _object = _interopRequireDefault(require("./object")); | ||
var _fields = _interopRequireDefault(require("./fields")); | ||
@@ -55,0 +55,0 @@ var _createField = _interopRequireDefault(require("./createField")); |
{ | ||
"name": "@commodo/fields", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"main": "index.js", | ||
@@ -31,3 +31,3 @@ "repository": { | ||
}, | ||
"gitHead": "eb004ce52b56daa0a6a9607b54652f5955db4026" | ||
"gitHead": "226415c5af6a8d08e071700cf862b638a57b86ed" | ||
} |
# @commodo/fields | ||
Creates a new function, whose instances are decorated with defined fields and additional methods. | ||
✍️ TODO | ||
## Usage | ||
```js | ||
import { withFields, string, number, boolean, fields } from "@commodo/fields"; | ||
import { compose } from "ramda"; | ||
const User = compose( | ||
withFields({ | ||
email: string(), | ||
previousEmails: string({ list: true }), | ||
age: number({ | ||
validation: value => { | ||
if (value < 30) { | ||
throw Error("User too young.") | ||
} | ||
} | ||
}), | ||
verified: boolean(), | ||
company: fields({ instanceOf: Company }) | ||
}) | ||
)(function() {}); | ||
const Company = compose( | ||
withFields({ | ||
name: string() | ||
}) | ||
)(function() {}); | ||
const user = new User(); | ||
user.populate({ | ||
email: "user3@email.com", | ||
previousEmails: ["user2@email.com", "user1@email.com"], | ||
age: 25, | ||
verified: true, | ||
company: { | ||
name: "Awesome Company" | ||
} | ||
}); | ||
// Throws an error with message "User too young". | ||
async user.validate(); | ||
``` | ||
## Reference | ||
#### `withFields(fields : { [string] : FieldFactory }): WithFieldsFunction` | ||
Creates a new function, whose instances contain defined fields and are decorated with `populate` and `validate` methods. | ||
### `WithFieldsFunction` | ||
Except fields, instances of `WithFieldsFunction` are decorated with a couple of useful methods. | ||
#### `populate(data: Object): void` | ||
Populates fields with given data. | ||
#### `validate(): Promise<void>` | ||
Validates all fields. | ||
#### `getFields(): Array<Field>` | ||
Returns all fields. | ||
#### `getField(name: string): ?Field` | ||
Returns a single field. | ||
#### `clean(): void` | ||
Sets instance as clean. | ||
#### `isDirty(): boolean` | ||
Checks if instance is dirty. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56872
72