Object Schema

Create a schema that matches any data object which contains key/value pairs.

The values may be of any other type.

See at Base Schema for the inherited methods you may call like:

  • title()
  • detail()
  • required()
  • forbidden()
  • default()
  • stripEmpty()
  • raw()

1. Sanitize

1.1. deepen(separator)

A elements containing a special separator are split up on each occurrence and be converted into a deep structure. The separator may be given as string or RegExp.

// data = {'a.a': 1, 'a.b': 2, c: 3}
const schema = new ObjectSchema().deepen('.')
// result = {a: {a: 1, b: 2}, c: 3}
schema.deepen() // to remove setting

References to regular expression or string:

const ref = new Reference('.')
const schema = new ObjectSchema().deepen(ref)

1.2. flatten(separator)

That's the opposite of deepen and will flatten deep structures using the given separator which has to be a string.

// data = {a: {a: 1, b: 2}, c: 3}
const schema = new ObjectSchema().flatten('.')
// result = {'a.a': 1, 'a.b': 2, c: 3}
schema.flatten() // to remove setting

References to regular expression or string:

const ref = new Reference('.')
const schema = new ObjectSchema().flatten(ref)

1.3. copy(from, to, overwrite) / move(from, to, overwrite)

This two methods allows you to copy, move and exchange the keys. It can be given multiple times and will be run in order. So also a switch is possible with the use of a temporary key.

const schema = new ObjectSchema().copy('a', 'x') // x will be like a
const schema = new ObjectSchema().move('a', 'x') // a will be renamed to x
const schema = new ObjectSchema().copy('a', 'x', true) // x will be like a and also overwrite x if exists
const schema = new ObjectSchema().move('a', 'x', true) // a will be renamed to x and also overwrite x if exists
schema.copy() // to remove setting

The remove of the setting will always remove all copy and move entries regardless of which method is called. This can also be used with reference:

const ref1 = new Reference('a')
const ref2 = new Reference('b')
const schema = new ObjectSchema().copy(ref1, ref2)

A switch may be done like:

const schema = new ObjectSchema() // given a, b
.move('a', 'temp') // now having b, temp
.move('b', 'a') // now having a, temp
.move('temp', 'b') /// now again a, b with switched values

1.4. removeUnknown

This will remove all unchecked keys from the object. So only the specified are returned. All elements which has specific checks set via key are checked.

const schema = new ObjectSchema().removeUnknown()
.key('one', new AnySchema())
schema.removeUnknown(false) // to remove setting

This can also be used with reference:

const ref = new Reference(true)
const schema = new ObjectSchema().removeUnknown(ref)
.key('one', new AnySchema())

2. Checking Keys

2.1. denyUnknown

By default unknown keys (keys which aren't defined explicitly) are allowed but with this setting you may forbid this.

const schema = new ObjectSchema().denyUnknown()
.key('one', new AnySchema())
schema.denyUnknown(false) // to deny setting

This can also be used with reference:

const ref = new Reference(true)
const schema = new ObjectSchema().denyUnknown(ref)
.key('one', new AnySchema())

2.2. min(limit) / max(limit) / length(limit)

Specifies the number of keys the object is allowed to have.

  • limit gives the number of elements to be within the data object
const schema = new ObjectSchema().min(1).max(3)
schema.min() // to remove setting, same for max
schema.length() // to remove min and max setting

References are possible:

const ref = new Reference(5)
const schema = new ObjectSchema().length(ref)

2.3. requiredKeys(list) / forbiddenKeys(list)

These two methods allow to define the key names which are allowed or disallowed. It may be called multiple times to specify it.

const schema = new ObjectSchema().requiredKeys('a', 'b', 'c')
.forbiddenKeys(['d', 'e'])

The list of keys can be given as:

  • one or multiple string
  • Array of string

2.4. and(list)

With this logic check you ensure that all of the given keys or none of them are present in the data object.

const schema = new ObjectSchema().and('a', 'b', 'c')

The list of keys can be given as:

  • one or multiple string
  • Array of string

2.5. nand(list)

With this logic check you ensure that some of the given keys may be set but neither all of them.

const schema = new ObjectSchema().nand('a', 'b', 'c')

The list of keys can be given as:

  • one or multiple string
  • Array of string

2.6. or(list)

With this logic check you ensure that at least one of the given keys are present in the data object.

const schema = new ObjectSchema().or('a', 'b', 'c')

The list of keys can be given as:

  • one or multiple string
  • Array of string

2.7. xor(list)

With this logic check you ensure that exactly one and not multiple of the given keys are present in the data object.

const schema = new ObjectSchema().xor('a', 'b', 'c')

The list of keys can be given as:

  • one or multiple string
  • Array of string

2.8. with(key, peers)

With this logic check you ensure that if the given 'key' is set all of the other peers have to be present, too.

const schema = new ObjectSchema().with('a', ['b', 'c'])

The parameters may be:

  • first string as the key to check and one or multiple string as peers
  • first string as the key to check and Array of string peers (more clear in reading code)

2.9. without(key, peers)

With this logic check you ensure that if the given 'key' is set none of the other peers are allowed.

const schema = new ObjectSchema().without('a', ['b', 'c'])

The parameters may be:

  • first string as the key to check and one or multiple string as peers
  • first string as the key to check and Array of string peers (more clear in reading code)

3. Deeper checks

3.1. key(name, check)

Specify the schema for a specific value or for all values which keys match the given pattern. Only the first match is used and directly specified key goes first, too.

  • name: have to be a string or RegExp matching one or multiple keys
  • check: is a new Schema type instance which defines the value of this element

Example

const schema = new ObjectSchema()
.key('one', new AnySchema())
.key(/number\d/, new AnySchema())
schema.key() // to remove all keys
schema.key('one') // to only remove this key

References are not possible here.

results matching ""

    No results matching ""