Skip to content

Commit

Permalink
feat: Add defined() validation to mix (#637)
Browse files Browse the repository at this point in the history
* feat: Add defined() validation to mix

* Add docs for the new method
  • Loading branch information
talyssonoc committed Feb 9, 2020
1 parent 092a143 commit ad0f073
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Yup's API is heavily inspired by [Joi](https://github.com/hapijs/joi), but leane
- [`mixed.nullable(isNullable: boolean = true): Schema`](#mixednullableisnullable-boolean--true-schema)
- [`mixed.required(message?: string | function): Schema`](#mixedrequiredmessage-string--function-schema)
- [`mixed.notRequired(): Schema`](#mixednotrequired-schema)
- [`mixed.defined(): Schema`](#mixeddefined-schema)
- [`mixed.typeError(message: string): Schema`](#mixedtypeerrormessage-string-schema)
- [`mixed.oneOf(arrayOfValues: Array<any>, message?: string | function): Schema` Alias: `equals`](#mixedoneofarrayofvalues-arrayany-message-string--function-schema-alias-equals)
- [`mixed.notOneOf(arrayOfValues: Array<any>, message?: string | function)`](#mixednotoneofarrayofvalues-arrayany-message-string--function)
Expand Down Expand Up @@ -585,6 +586,10 @@ Mark the schema as required. All field values apart from `undefined` and `null`

Mark the schema as not required. Passing `undefined` as value will not fail validation.

#### `mixed.defined(): Schema`

Mark the schema as required but nullable. All field values apart from `undefined` meet this requirement.

#### `mixed.typeError(message: string): Schema`

Define an error message for failed type checks. The `${value}` and `${type}` interpolation can
Expand Down
1 change: 1 addition & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export let mixed = {

return msg;
},
defined: '${path} must be defined',
};

export let string = {
Expand Down
11 changes: 11 additions & 0 deletions src/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,17 @@ const proto = (SchemaType.prototype = {
),
};
},

defined(message = locale.defined) {
return this.nullable().test({
message,
name: 'defined',
exclusive: true,
test(value) {
return value !== undefined;
},
});
},
});

for (const method of ['validate', 'validateSync'])
Expand Down
37 changes: 37 additions & 0 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,4 +930,41 @@ describe('Mixed Types ', () => {
},
});
});

describe('defined', () => {
it('should fail when value is undefined', async () => {
let inst = object({
prop: string().defined(),
});

await inst
.validate({})
.should.be.rejected()
.then(function(err) {
err.message.should.equal('prop must be defined');
});
});

it('should pass when value is null', async () => {
let inst = object({
prop: string().defined(),
});

await inst
.isValid({ prop: null })
.should.eventually()
.equal(true);
});

it('should pass when value is not undefined nor null', async () => {
let inst = object({
prop: string().defined(),
});

await inst
.isValid({ prop: 'prop value' })
.should.eventually()
.equal(true);
});
});
});

0 comments on commit ad0f073

Please sign in to comment.