# EnforcerResult

An EnforcerResult is an object that allows destructuring from an iterable or a plain object.

The object has the properties:

  • error - An EnforcerException object. If there is no error then this value will be undefined.

  • value - The success value. This value will be undefined if an error occurred.

  • warning - An EnforcerException object with warnings that do not prevent successful operation. If there is no warning exception then this value will be undefined.

The iterable has the same values as the object in order of:

  1. value

  2. error

  3. warning

# Read Entire Object

Probably the most well known method for reading a result, you can get the entire object and then access the properties off of that object.

const result = new Enforcer.v2_0.Schema({ type: 'string' })

# Destructure by Properties

Default destructuring requires the original property names to be used. You can specify as many or as few properties as you'd like to extract. It is also possible to specify the name of the extracted property.

Specify all Properties

const { error, value, warning } = new Enforcer.v2_0.Schema({ type: 'string' })

Specify some Properties

const { value } = new Enforcer.v2_0.Schema({ type: 'string' })

Specify Properties with Different Name

In this example:

  • error is renamed to e
  • value is renamed to schema
  • warning is not renamed
const { error: e, value: schema, warning } = new Enforcer.v2_0.Schema({ type: 'string' })

# Destructure by Index

You can choose the names of the value, but the order is important. The order is 1) the value, 2) the error, 3) the warning.

It is not a requirement to get all three values.

Get All Three Properties

const [ schema, err, warning ] = new Enforcer.v2_0.Schema({ type: 'string' })

Get Just the Value

const [ schema ] = new Enforcer.v2_0.Schema({ type: 'string' })

Get Just the Error

const [ , err ] = new Enforcer.v2_0.Schema({ type: 'string' })

Get Just the Warning

const [ , , warning ] = new Enforcer.v2_0.Schema({ type: 'string' })

Run the Iterator Multiple Times

The iterator loops, allowing you to destructure by index indefinitely.

const [ schema, error, warning, schema2, error2, warning2, schema3 ] = new Enforcer.v2_0.Schema({ type: 'string' })
console.log(schema === schema2 && schema === schema3) // true
console.log(error === error2) // true
console.log(warning === warning2) // true