-
Notifications
You must be signed in to change notification settings - Fork 18
Constraints
A Constraint is a simple record used to validate values. Various types of Constraints have already been implemented in API-Flow
/* if in src/ */
import Constraint from './models/Constraint'-
Constraint.Minimum
const cond = new Constraint.Minimum(4) cond.evaluate(3.5) // false cond.evaluate(4) // true cond.evaluate(4.5) // true cond.toJSONSchema() // { minimum: 4 }
-
Constraint.ExclusiveMinimum
const cond = new Constraint.ExclusiveMinimum(4) cond.evaluate(3.5) // false cond.evaluate(4) // false cond.evaluate(4.5) // true cond.toJSONSchema() // { minimum: 4, exclusiveMinimum: true }
-
Constraint.Maximum
const cond = new Constraint.Maximum(4) cond.evaluate(3.5) // true cond.evaluate(4) // true cond.evaluate(4.5) // false cond.toJSONSchema() // { maximum: 4 }
-
Constraint.ExclusiveMaximum
const cond = new Constraint.ExclusiveMaximum(4) cond.evaluate(3.5) // true cond.evaluate(4) // false cond.evaluate(4.5) // false cond.toJSONSchema() // { maximum: 4, exclusiveMaximum }
-
Constraint.MultipleOf
const cond = new Constraint.MultipleOf(0.12) cond.evaluate(0.24) // true cond.evaluate(0.13) // false cond.toJSONSchema() // { multipleOf: 0.12 }
-
Constraint.MinimumLength
const cond = new Constraint.MinimumLength(5) cond.evaluate('hey') // false cond.evaluate('valid') // true cond.evaluate('long enough') // true cond.toJSONSchema() // { minLength: 5 }
-
Constraint.MaximumLength
const cond = new Constraint.MaximumLength(5) cond.evaluate('hey') // true cond.evaluate('valid') // true cond.evaluate('too long') // false cond.toJSONSchema() // { maxLength: 5 }
-
Constraint.Pattern
const cond = new Constraint.Pattern('^[a-f0-9]*$') cond.evaluate('deadbeef') // true cond.evaluate('not an hexadecimal string') // false cond.toJSONSchema() // { pattern: '^[a-f0-9]*$' }
-
Constraint.MinimumItems
const cond = new Constraint.MinimumItems(4) cond.evaluate([ 1, 2, 3 ]) // false cond.evaluate([ 1, 2, 3, 4 ]) // true cond.evaluate([ 1, 2, 3, 4, 5]) // true cond.toJSONSchema() // { minItems: 4 }
-
Constraint.MaximumItems
const cond = new Constraint.MaximumItems(4) cond.evaluate([ 1, 2, 3 ]) // true cond.evaluate([ 1, 2, 3, 4 ]) // true cond.evaluate([ 1, 2, 3, 4, 5 ]) // false cond.toJSONSchema() // { maxItems: 4 }
-
Constraint.UniqueItems
const cond = new Constraint.UniqueItems(true) cond.evaluate([ 1, 2, 3, 2 ]) // false cond.evaluate([ 1, 2, 3, 4, 5 ]) // true cond.toJSONSchema() // { uniqueItems: true }
-
Constraint.MinimumProperties
const cond = new Constraint.MinimumProperties(4) cond.evaluate({ a: 123, b: 321, c: 234 }) // false cond.evaluate({ a: 123, b: 321, c: 234, d: 432 }) // true cond.evaluate({ a: 123, b: 321, c: 234, d: 432, e: 345 }) // true cond.toJSONSchema() // { minProperties: 4 }
-
Constraint.MaximumProperties
const cond = new Constraint.MaximumProperties(4) cond.evaluate({ a: 123, b: 321, c: 234 }) // true cond.evaluate({ a: 123, b: 321, c: 234, d: 432 }) // true cond.evaluate({ a: 123, b: 321, c: 234, d: 432, e: 345 }) // false cond.toJSONSchema() // { maxProperties: 4 }
-
Constraint.Enum
const cond = new Constraint.Enum([ 'abc', 23, 'something' ]) cond.evaluate('abc') // true cond.evaluate(23) // true cond.evaluate('not in the list') // false cond.toJSONSchema() // { enum: [ 'abc', 23, 'something' ] }
-
Constraint.JSONSchema
const cond = new Constraint.JSONSchema({ type: 'string', enum: [ 'abc', 'def' ] }) cond.evaluate('abc') // true cond.evaluate('def') // true cond.evaluate('fgh') // **true** cond.toJSONSchema() // { type: 'string', enum: [ 'abc', 'def' ] }
Note: JSON Schema validation is not yet implemented in API-Flow, and therefore the
evaluatemethod will return true regardless of what is passed to it. -
Constraint.XMLSchema
const cond = new Constraint.XMLSchema("...") cond.evaluate('abc') // true cond.evaluate('def') // true cond.evaluate('fgh') // **true** cond.toJSONSchema() // { x-xml: "..." }
Note: XML Schema validation is not yet implemented in API-Flow, and therefore the
evaluatemethod will return true regardless of what is passed to it.
It is possible to create custom constraints to extend the current model.
import Constraint from './models/Constraint'
class CubeIsSmallerThanConstraint extends Constraint.Constraint {
constructor(value) {
const instance = {
_model: new Model({
name: 'cube-smaller.constraint.models',
version: '0.1.0'
}),
name: 'x-cube-smaller',
value: value,
expression: d => {
if (typeof value !== 'number') {
return false
}
return d*d*d <= value
}
}
}
}
const cond = new CubeIsSmallerThanConstraint(1000)
cond.evaluate(5) // true
cond.evaluate(10) // true
cond.evaluate(15) // false
cond.toJSONSchema() // { 'x-cube-smaller': 1000 }