@knorm/knorm
Advanced tools
Comparing version 1.9.1 to 1.10.0
@@ -0,1 +1,13 @@ | ||
# [1.10.0](https://github.com/knorm/knorm/compare/v1.9.1...v1.10.0) (2018-10-12) | ||
### Bug Fixes | ||
* **Field:** support new `schema` validators from custom validators ([ba56285](https://github.com/knorm/knorm/commit/ba56285)) | ||
### Features | ||
* **Field:** pass model instance as param to custom validator ([5097be4](https://github.com/knorm/knorm/commit/5097be4)) | ||
## [1.9.1](https://github.com/knorm/knorm/compare/v1.9.0...v1.9.1) (2018-10-08) | ||
@@ -2,0 +14,0 @@ |
@@ -125,5 +125,5 @@ # Validation | ||
To add custom validation, supply a `validate` function to the field config. The | ||
validator is called with the field's value as the only argument and with `this` | ||
set to the instance of the `Model`, so you're able to access other values on the | ||
instance (this is handy for validating fields based on the input of other fields). | ||
validator is called with the field's value and the [Model](/api.md#Model) | ||
instance as parameters, so you're able to access other values on the instance. | ||
This is handy for validating fields based on the input of other fields. | ||
@@ -147,3 +147,3 @@ Async validators (validators that return a `Promise`) are also supported. | ||
```js {13,14,15,16,17} | ||
```js {11,12,13,14,15,16,17,18,19,24,25,26,27,28,29,30} | ||
class User extends Model {} | ||
@@ -159,4 +159,4 @@ | ||
type: 'string', | ||
validate(value) { | ||
if (this.loginType === 'email') { | ||
validate(value, model) { | ||
if (model.loginType === 'email') { | ||
// return new validators | ||
@@ -195,6 +195,2 @@ return { | ||
::: warning NOTE | ||
JSON fields are **only** validated if they contain a `schema` validator. | ||
::: | ||
```js {6,7,8,9,10} | ||
@@ -201,0 +197,0 @@ class Upload extends Model {} |
@@ -139,3 +139,3 @@ const { isUUID, isDecimal, isEmail } = require('validator'); | ||
} | ||
if (schema && ['json', 'jsonb', 'object', 'array'].includes(type)) { | ||
if (schema) { | ||
validators.schema = this._createSchemaValidators(schema); | ||
@@ -434,2 +434,41 @@ } | ||
/** | ||
* Custom validator function, note that `async` validator functions, or | ||
* functions that return a {@link Promise}, are supported. | ||
* | ||
* Validation for the value will be failed if the function: | ||
* - throws an error | ||
* - returns `false` | ||
* - returns a `Promise` that is rejected with an error | ||
* - returns a `Promise` that is resolved with `false` | ||
* | ||
* This function may also return an object with the regular | ||
* [validators](/guides/fields.md#field-config), or resolving the `Promise` | ||
* with an object with validators, including another custom validator | ||
* function! | ||
* | ||
* @callback Field~customValidator | ||
* @param {any} value the value to validate. | ||
* @param {Model} The {@link Model} instance where the field value is set, if | ||
* one exists. This will always be set if {@link Field#validateWithCustom} is | ||
* called via {@link Model#validate}. | ||
* | ||
* @returns {Promise|boolean|object} | ||
*/ | ||
/** | ||
* Validates a value with a custom validator function. | ||
* | ||
* @param {any} value The value to validate | ||
* @param {Field~customValidator} validate The validator function. | ||
* @param {Model} modelInstance The {@link Model} instance where the field | ||
* value is set, if one exists. This will always be set if this method is | ||
* called via {@link Model#validate}. | ||
* | ||
* @returns {Promise} | ||
* | ||
* @todo **breaking change** in the validator function, do not set `this` to | ||
* the model instance. Instead, `this` should point to the {@link Field} | ||
* instance. | ||
*/ | ||
async validateWithCustom(value, validate, modelInstance) { | ||
@@ -440,3 +479,7 @@ if (value === undefined) { | ||
const returnValue = await validate.call(modelInstance, value); | ||
const returnValue = await validate.call( | ||
modelInstance, | ||
value, | ||
modelInstance | ||
); | ||
@@ -448,3 +491,4 @@ if (returnValue === false) { | ||
if (typeof returnValue === 'object') { | ||
return this.validateWithValidators(value, returnValue, modelInstance); | ||
const validators = this._createValidators(returnValue); | ||
return this.validateWithValidators(value, validators, modelInstance); | ||
} | ||
@@ -451,0 +495,0 @@ } |
{ | ||
"name": "@knorm/knorm", | ||
"version": "1.9.1", | ||
"version": "1.10.0", | ||
"description": "A JavaScript ORM written using ES6 classes", | ||
@@ -36,3 +36,3 @@ "main": "index.js", | ||
"pg": "7.5.0", | ||
"prettier": "1.12.1", | ||
"prettier": "1.14.3", | ||
"proxyquire": "2.1.0", | ||
@@ -39,0 +39,0 @@ "semantic-release": "15.9.17", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
0
100
1
505681
57
11112