Skip to content

Commit

Permalink
Adds Partial Output To ValidationContext
Browse files Browse the repository at this point in the history
Adds the value that will be returned by `validate` to the
ValidationContext, so inline validators can base their output and
validity on previously processed properties.

Adds `is.asyncFunc` and `is.promise` to the TypeScript definitions.

Updates documentation for the ValidationContext, and to all TypeScript
interfaces.

Reduces verbosity when returning IValueOrError, so consumers don't have
to set a bunch of null properties. Blueprint guarantees them for us, so
no reason to set them explicitly.
  • Loading branch information
losandes committed May 25, 2019
1 parent f6774b2 commit 0701b4c
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 33 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ When defining the schema for your blueprint, you can define the property type wi
* _@param {any}_ **value** - the value being validated (i.e. `input[key]`)
* _@param {any}_ **input** - the object that is being validated
* _@param {any}_ **root** - the root object that is being validated (different than input when the input is nested in another object)
* _@param {any}_ **output** - the current state of the `value` property for the `IValueOrError` that is returned by `validate`. You can use this to validate the values of other properties that were already processed, and to mutate the output (the latter is not recommended).
* _@param {object}_ **schema** - the schema for this validation context
In this example, we require the given property based on the value of another property:
Expand Down
9 changes: 9 additions & 0 deletions dist/blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
this.value = input.value;
this.input = input.input;
this.root = input.root;
this.output = input.output;
this.schema = input.schema;
};

Expand Down Expand Up @@ -189,6 +190,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
value: input && input[key],
input: input,
root: root || input,
output: output.value,
schema: schema
});
var result = validator(context, makeDefaultErrorMessage(context));
Expand All @@ -201,6 +203,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
output.value[key] = result ? result.value : input[key];
return output;
}, {
/* output */
validationErrors: [],
value: tryMakeFromProto(input)
}); // /reduce
Expand Down Expand Up @@ -557,6 +560,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
func: undefined,
promise: undefined,
asyncFunction: undefined,
asyncFunc: undefined,
object: undefined,
array: undefined,
string: undefined,
Expand All @@ -573,6 +577,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
func: undefined,
promise: undefined,
asyncFunction: undefined,
asyncFunc: undefined,
object: undefined,
array: undefined,
string: undefined,
Expand Down Expand Up @@ -658,10 +663,14 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
return is.promise(obj);
};

is.asyncFunc = is.asyncFunction; // consistency for typescript

is.not.asyncFunction = function (obj) {
return is.asyncFunction(obj) === false;
};

is.not.asyncFunc = is.not.asyncFunction; // consistency for typescript

is.object = function (obj) {
return is.getType(obj) === 'object';
};
Expand Down
10 changes: 6 additions & 4 deletions dist/blueprint.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions examples-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ import {

const isCharVal: Validator = (context: IValidationContext): IValueOrError => {
if (typeof context.value === 'string' && context.value.length === 1) {
return {
err: null,
messages: null,
value: context.value
}
return { value: context.value }
}

return {
err: new Error('BOOM!'),
messages: ['BOOM!'],
messages: ['BOOM!', 'BOOM AGAIN!'],
value: context.value
}
}
Expand Down Expand Up @@ -151,9 +147,9 @@ import {
{ value }: IValidationContext<IAmValidationType>
): IValueOrError<IAmValidationType> => {
if (value && is.string(value.prop1) && is.number(value.prop2)) {
return { value, err: null, messages: null };
return { value };
}
return { value: null, err: new Error('Boom!'), messages: null };
return { err: new Error('Boom!') };
}
});

Expand Down
Loading

0 comments on commit 0701b4c

Please sign in to comment.