Skip to content

Commit

Permalink
Merge pull request #82 from zemccartney/field-helper-test-adds
Browse files Browse the repository at this point in the history
Add tests for nested props and refs for field helper
  • Loading branch information
devinivy committed Dec 18, 2019
2 parents 0fac0ca + e3f45f5 commit fb76442
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,74 @@ describe('Schwifty', () => {
expect(c.validate('x')).to.equal({ value: 'x' });
expect(c.validate(1)).to.contain('error');
});

it('supports nested properties.', () => {

const Model = class extends Schwifty.Model {
static get joiSchema() {

return Joi.object({
a: Joi.object({
d: Joi.string().min(3),
e: Joi.string().default('e')
}),
b: Joi.string().default('b'),
c: Joi.string().required()
});
}
};

const d = Model.field('a.d');
const e = Model.field('a.e');

const dfull = Model.field('a.d').tailor('full');
const efull = Model.field('a.e').tailor('full');

expect(d.validate('123')).to.equal({ value: '123' });
expect(d.validate('12')).to.contain('error');

expect(e.validate()).to.equal({ value: undefined });
expect(e.validate('x')).to.equal({ value: 'x' });
expect(e.validate(1)).to.contain('error');

expect(dfull.validate('123')).to.equal({ value: '123' });
expect(dfull.validate('12')).to.contain('error');

expect(efull.validate()).to.equal({ value: 'e' });
expect(efull.validate('x')).to.equal({ value: 'x' });
expect(efull.validate(1)).to.contain('error');
});

it('validation throws when the schema contains an invalid ref', () => {

const Model = class extends Schwifty.Model {
static get joiSchema() {

return Joi.object({
a: Joi.number(),
b: Joi.number(),
c: Joi.ref('a'),
d: Joi.expression('{b * a}')
});
}
};

const a = Model.field('a');
const b = Model.field('b');
const c = Model.field('c');
const d = Model.field('d');

expect(a.validate(5)).to.equal({ value: 5 });
expect(b.validate(6)).to.equal({ value: 6 });
expect(() => c.validate(5)).to.throw('Invalid reference exceeds the schema root: ref:a');
expect(() => d.validate(30)).to.throw('Invalid reference exceeds the schema root: ref:b');

const schema = Joi.object({
a: Joi.string(),
c
});
expect(schema.validate({ a: '123', c: '123' })).to.equal({ value: { a: '123', c: '123' } });
});
});

describe('static getter jsonAttributes', () => {
Expand Down

0 comments on commit fb76442

Please sign in to comment.