@commodo/fields
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "@commodo/fields", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "index.js", | ||
@@ -31,3 +31,3 @@ "repository": { | ||
}, | ||
"gitHead": "226415c5af6a8d08e071700cf862b638a57b86ed" | ||
"gitHead": "5bf6039e3ddaa2e35136b78daea931651a96c91d" | ||
} |
# @commodo/fields | ||
Creates a new function, whose instances are decorated with defined fields and additional methods. | ||
Creates a new function, whose instances are decorated with defined fields and a couple of useful methods (more information in the following sections). | ||
@@ -46,7 +46,66 @@ ## Usage | ||
## Fields | ||
There are four types of fields you can define: | ||
1. `string` - accepts `null` and string values | ||
2. `number` - accepts `null` and number values | ||
3. `boolean` - accepts `null` and boolean values | ||
4. `fields` - accepts `null`, a plain object or an instance of another `withFields` function | ||
In the following examples, all types of fields are utilized: | ||
``` | ||
const User = compose( | ||
withFields({ | ||
email: string(), | ||
age: number(), | ||
verified: boolean(), | ||
company: fields({ instanceOf: Company }) | ||
}) | ||
)(function() {}); | ||
``` | ||
Note that if the data type of a value is different than field type, an error will immediately be thrown: | ||
``` | ||
const User = compose( | ||
withFields({ | ||
email: string() | ||
}) | ||
)(function() {}); | ||
const user = new User(); | ||
// Throws a WithFieldsError (code: "FIELD_DATA_TYPE_ERROR"): | ||
user.email = true; | ||
``` | ||
Each field can accept a few options: | ||
#### `list: boolean` | ||
If set to `true`, field will accept an `null` or an array of values. When setting field value, if a single item in the passed array is of incorrect data type, an error will be thrown. | ||
#### `validation: Function` | ||
A function for validating the assigned value. Not for data-type validation (since it's already done upon assigning a value), but for checking if the value complies with custom logic, for example if the assigned value is greater than 20. | ||
#### `value: any` | ||
Except options, fields can also be enhanced with a couple of provided higher order functions: | ||
#### `onGet: Function => Function` | ||
#### `onSet: Function => Function` | ||
#### `skipOnPopulate: Function => Function` | ||
#### `setOnce: Function => Function` | ||
#### `readOnly: Function => Function` | ||
When enabled, field will no longer accept any value. Can be paired with `onGet` higher order function, to achieve "dynamic" fields (more on this in the next section). | ||
## Reference | ||
#### `withFields(fields : { [string] : FieldFactory }): WithFieldsFunction` | ||
Creates a new function, whose instances contain defined fields and are decorated with `populate` and `validate` methods. | ||
Creates a new function, whose instances contain defined fields and are decorated with a couple of useful methods. | ||
### FieldFactory | ||
### `WithFieldsFunction` | ||
@@ -53,0 +112,0 @@ |
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
58777
131