@contember/schema-utils
Advanced tools
Comparing version 0.8.0-alpha.5 to 0.8.0
@@ -13,3 +13,3 @@ "use strict"; | ||
validate(schema) { | ||
const errorBuilder = new errors_1.ErrorBuilder([], ['acl']); | ||
const errorBuilder = new errors_1.ErrorBuilder([], ['validation']); | ||
let validSchema; | ||
@@ -87,3 +87,3 @@ if (!utils_1.isObject(schema)) { | ||
} | ||
const validValidator = this.validateValidator(errorBuilder.for('validator'), rule.validator, entity); | ||
const validValidator = this.validateValidator(errorBuilder.for('validator'), rule.validator, entity, field); | ||
if (!validValidator) { | ||
@@ -113,3 +113,3 @@ return undefined; | ||
} | ||
validateValidator(errorBuilder, validator, entity) { | ||
validateValidator(errorBuilder, validator, entity, field) { | ||
if (!utils_1.isObject(validator)) { | ||
@@ -123,3 +123,3 @@ errorBuilder.add('Must be an object'); | ||
const andArgs = validatorCast.args | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity)) | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity, field)) | ||
.filter((it) => it !== undefined); | ||
@@ -129,8 +129,8 @@ return { operation: 'and', args: andArgs }; | ||
const orArgs = validatorCast.args | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity)) | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity, field)) | ||
.filter((it) => it !== undefined); | ||
return { operation: 'or', args: orArgs }; | ||
case 'conditional': | ||
const argA = this.validateValidatorArgument(errorBuilder.for('condition'), validatorCast.args[0], entity); | ||
const argB = this.validateValidatorArgument(errorBuilder.for('rule'), validatorCast.args[1], entity); | ||
const argA = this.validateValidatorArgument(errorBuilder.for('condition'), validatorCast.args[0], entity, field); | ||
const argB = this.validateValidatorArgument(errorBuilder.for('rule'), validatorCast.args[1], entity, field); | ||
if (argA === undefined || argB === undefined) { | ||
@@ -163,2 +163,12 @@ return undefined; | ||
}; | ||
case 'range': | ||
const rangeArgA = this.validateLiteralArgument(errorBuilder.for('min'), validatorCast.args[0]); | ||
const rangeArgB = this.validateLiteralArgument(errorBuilder.for('max'), validatorCast.args[1]); | ||
if (lengthArgA === undefined || lengthArgB === undefined) { | ||
return undefined; | ||
} | ||
return { | ||
operation: 'range', | ||
args: [rangeArgA, rangeArgB], | ||
}; | ||
case 'equals': | ||
@@ -174,3 +184,3 @@ const eqArg = this.validateLiteralArgument(errorBuilder, validatorCast.args[0]); | ||
case 'not': | ||
const notArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity); | ||
const notArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field); | ||
if (notArg === undefined) { | ||
@@ -187,10 +197,14 @@ return undefined; | ||
case 'inContext': | ||
const pathArg = this.validatePathArgument(errorBuilder.for('path'), validatorCast.args[0], entity); | ||
const inValidatorArg = this.validateValidatorArgument(errorBuilder.for('validator'), validatorCast.args[1], entity); | ||
if (pathArg === undefined || inValidatorArg === undefined) { | ||
const pathArgResult = this.validatePathArgument(errorBuilder.for('path'), validatorCast.args[0], entity); | ||
if (pathArgResult === undefined) { | ||
return undefined; | ||
} | ||
const [pathArg, inField] = pathArgResult; | ||
const inValidatorArg = this.validateValidatorArgument(errorBuilder.for('validator'), validatorCast.args[1], entity, inField); | ||
if (inValidatorArg === undefined) { | ||
return undefined; | ||
} | ||
return { operation: 'inContext', args: [pathArg, inValidatorArg] }; | ||
case 'every': | ||
const everyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity); | ||
const everyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field); | ||
if (everyArg === undefined) { | ||
@@ -201,3 +215,3 @@ return undefined; | ||
case 'any': | ||
const anyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity); | ||
const anyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field); | ||
if (anyArg === undefined) { | ||
@@ -208,4 +222,4 @@ return undefined; | ||
case 'filter': | ||
const filterArgA = this.validateValidatorArgument(errorBuilder.for('filter'), validatorCast.args[0], entity); | ||
const filterArgB = this.validateValidatorArgument(errorBuilder.for('validator'), validatorCast.args[1], entity); | ||
const filterArgA = this.validateValidatorArgument(errorBuilder.for('filter'), validatorCast.args[0], entity, field); | ||
const filterArgB = this.validateValidatorArgument(errorBuilder.for('validator'), validatorCast.args[1], entity, field); | ||
if (filterArgA === undefined || filterArgB === undefined) { | ||
@@ -222,3 +236,3 @@ return undefined; | ||
} | ||
validateValidatorArgument(errorBuilder, argument, entity) { | ||
validateValidatorArgument(errorBuilder, argument, entity, field) { | ||
if (!utils_1.isObject(argument)) { | ||
@@ -232,3 +246,3 @@ errorBuilder.add('Must be an object'); | ||
} | ||
const validator = this.validateValidator(errorBuilder.for('validator'), argument.validator, entity); | ||
const validator = this.validateValidator(errorBuilder.for('validator'), argument.validator, entity, field); | ||
if (!validator) { | ||
@@ -279,3 +293,3 @@ return undefined; | ||
} | ||
return { type: argument.type, path: argument.path }; | ||
return [{ type: argument.type, path: argument.path }, entity.fields[argument.path[0]]]; | ||
} | ||
@@ -282,0 +296,0 @@ } |
{ | ||
"name": "@contember/schema-utils", | ||
"version": "0.8.0-alpha.5", | ||
"version": "0.8.0", | ||
"license": "Apache-2.0", | ||
@@ -9,3 +9,3 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/schema": "^0.8.0-alpha.5" | ||
"@contember/schema": "^0.8.0" | ||
}, | ||
@@ -16,3 +16,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "b5cc6249a32f3c16618a9ba8364abf10f85acf6b" | ||
"gitHead": "b066c9e32d10ad436d0cfada0d9bd9ad3e2e7e5f" | ||
} |
@@ -10,3 +10,3 @@ import { Model, Validation } from '@contember/schema' | ||
public validate(schema: unknown): [Validation.Schema, ValidationError[]] { | ||
const errorBuilder = new ErrorBuilder([], ['acl']) | ||
const errorBuilder = new ErrorBuilder([], ['validation']) | ||
let validSchema: Validation.Schema | ||
@@ -99,3 +99,3 @@ if (!isObject(schema)) { | ||
const validValidator = this.validateValidator(errorBuilder.for('validator'), rule.validator, entity) | ||
const validValidator = this.validateValidator(errorBuilder.for('validator'), rule.validator, entity, field) | ||
if (!validValidator) { | ||
@@ -132,2 +132,3 @@ return undefined | ||
entity: Model.Entity, | ||
field: Model.AnyField, | ||
): Validation.Validator | undefined { | ||
@@ -142,3 +143,3 @@ if (!isObject(validator)) { | ||
const andArgs = validatorCast.args | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity)) | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity, field)) | ||
.filter((it): it is Validation.ValidatorArgument => it !== undefined) | ||
@@ -148,8 +149,8 @@ return { operation: 'and' as const, args: andArgs } | ||
const orArgs = validatorCast.args | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity)) | ||
.map((it, index) => this.validateValidatorArgument(errorBuilder.for(String(index)), it, entity, field)) | ||
.filter((it): it is Validation.ValidatorArgument => it !== undefined) | ||
return { operation: 'or' as const, args: orArgs } | ||
case 'conditional': | ||
const argA = this.validateValidatorArgument(errorBuilder.for('condition'), validatorCast.args[0], entity) | ||
const argB = this.validateValidatorArgument(errorBuilder.for('rule'), validatorCast.args[1], entity) | ||
const argA = this.validateValidatorArgument(errorBuilder.for('condition'), validatorCast.args[0], entity, field) | ||
const argB = this.validateValidatorArgument(errorBuilder.for('rule'), validatorCast.args[1], entity, field) | ||
if (argA === undefined || argB === undefined) { | ||
@@ -185,2 +186,12 @@ return undefined | ||
} | ||
case 'range': | ||
const rangeArgA = this.validateLiteralArgument(errorBuilder.for('min'), validatorCast.args[0]) | ||
const rangeArgB = this.validateLiteralArgument(errorBuilder.for('max'), validatorCast.args[1]) | ||
if (lengthArgA === undefined || lengthArgB === undefined) { | ||
return undefined | ||
} | ||
return { | ||
operation: 'range', | ||
args: [rangeArgA as Validation.LiteralArgument<number>, rangeArgB as Validation.LiteralArgument<number>], | ||
} | ||
case 'equals': | ||
@@ -196,3 +207,3 @@ const eqArg = this.validateLiteralArgument(errorBuilder, validatorCast.args[0]) | ||
case 'not': | ||
const notArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity) | ||
const notArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field) | ||
if (notArg === undefined) { | ||
@@ -209,3 +220,8 @@ return undefined | ||
case 'inContext': | ||
const pathArg = this.validatePathArgument(errorBuilder.for('path'), validatorCast.args[0], entity) | ||
const pathArgResult = this.validatePathArgument(errorBuilder.for('path'), validatorCast.args[0], entity) | ||
if (pathArgResult === undefined) { | ||
return undefined | ||
} | ||
const [pathArg, inField] = pathArgResult | ||
const inValidatorArg = this.validateValidatorArgument( | ||
@@ -215,4 +231,5 @@ errorBuilder.for('validator'), | ||
entity, | ||
inField, | ||
) | ||
if (pathArg === undefined || inValidatorArg === undefined) { | ||
if (inValidatorArg === undefined) { | ||
return undefined | ||
@@ -222,3 +239,3 @@ } | ||
case 'every': | ||
const everyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity) | ||
const everyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field) | ||
if (everyArg === undefined) { | ||
@@ -229,3 +246,3 @@ return undefined | ||
case 'any': | ||
const anyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity) | ||
const anyArg = this.validateValidatorArgument(errorBuilder, validatorCast.args[0], entity, field) | ||
if (anyArg === undefined) { | ||
@@ -236,4 +253,14 @@ return undefined | ||
case 'filter': | ||
const filterArgA = this.validateValidatorArgument(errorBuilder.for('filter'), validatorCast.args[0], entity) | ||
const filterArgB = this.validateValidatorArgument(errorBuilder.for('validator'), validatorCast.args[1], entity) | ||
const filterArgA = this.validateValidatorArgument( | ||
errorBuilder.for('filter'), | ||
validatorCast.args[0], | ||
entity, | ||
field, | ||
) | ||
const filterArgB = this.validateValidatorArgument( | ||
errorBuilder.for('validator'), | ||
validatorCast.args[1], | ||
entity, | ||
field, | ||
) | ||
if (filterArgA === undefined || filterArgB === undefined) { | ||
@@ -255,2 +282,3 @@ return undefined | ||
entity: Model.Entity, | ||
field: Model.AnyField, | ||
): Validation.ValidatorArgument | undefined { | ||
@@ -265,3 +293,3 @@ if (!isObject(argument)) { | ||
} | ||
const validator = this.validateValidator(errorBuilder.for('validator'), argument.validator, entity) | ||
const validator = this.validateValidator(errorBuilder.for('validator'), argument.validator, entity, field) | ||
if (!validator) { | ||
@@ -296,3 +324,3 @@ return undefined | ||
entity: Model.Entity, | ||
): Validation.PathArgument | undefined { | ||
): [Validation.PathArgument, Model.AnyField] | undefined { | ||
if (!isObject(argument)) { | ||
@@ -322,4 +350,4 @@ errorBuilder.add('Must be an object') | ||
} | ||
return { type: argument.type, path: argument.path } | ||
return [{ type: argument.type, path: argument.path }, entity.fields[argument.path[0]]] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
323884
3430
Updated@contember/schema@^0.8.0