-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6729df
commit 65a55d4
Showing
9 changed files
with
163 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
var _ = require('lodash'); | ||
var klay = relativeRequire('klay.js'); | ||
|
||
describe('klay', function () { | ||
context('fixtures/document.js', function () { | ||
var fixture = require('./fixtures/document.js'); | ||
var model; | ||
|
||
beforeEach(function () { | ||
model = fixture(klay()); | ||
}); | ||
|
||
afterEach(function () { | ||
klay.reset(); | ||
}); | ||
|
||
it('should pass valid html document', function () { | ||
var obj = { | ||
id: '12345678-1234-1234-1234-123412341234', | ||
parentId: '12345678-1234-1234-1234-123412341234', | ||
type: 'html', | ||
metadata: {title: 'my document', version: 'html5'}, | ||
source: {raw: '<html></html>', text: ''}, | ||
createdAt: '2016-07-27T04:51:22.820Z', | ||
updatedAt: '2016-07-27T04:51:22.820Z', | ||
}; | ||
|
||
var validated = model.validate(obj); | ||
validated.should.have.property('conforms', true); | ||
validated.should.have.property('value').eql(_.defaults({ | ||
createdAt: new Date('2016-07-27T04:51:22.820Z'), | ||
updatedAt: new Date('2016-07-27T04:51:22.820Z'), | ||
}, obj)); | ||
}); | ||
|
||
it('should pass valid json document', function () { | ||
var obj = { | ||
id: '12345678-1234-1234-1234-123412341234', | ||
parentId: '12345678-1234-1234-1234-123412341234', | ||
type: 'json', | ||
metadata: {type: 'array', size: '120'}, | ||
source: {prop1: 'foobar', prop2: 'something'}, | ||
createdAt: '2016-07-27T04:51:22.820Z', | ||
updatedAt: '2016-07-27T04:51:22.820Z', | ||
}; | ||
|
||
var validated = model.validate(obj); | ||
validated.should.have.property('conforms', true); | ||
validated.should.have.property('value').eql(_.defaults({ | ||
metadata: {type: 'array', size: 120}, | ||
createdAt: new Date('2016-07-27T04:51:22.820Z'), | ||
updatedAt: new Date('2016-07-27T04:51:22.820Z'), | ||
}, obj)); | ||
}); | ||
|
||
it('should fail invalid html document', function () { | ||
var obj = { | ||
id: '12345678-1234-1234-1234-123412341234', | ||
parentId: '12345678-1234-1234-1234-123412341234', | ||
type: 'html', | ||
metadata: {title: 'foo', version: 'v1', html5: true}, | ||
source: {raw: 'something'}, | ||
createdAt: '2016-07-27T04:51:22.820Z', | ||
updatedAt: '2016-07-27T04:51:22.820Z', | ||
}; | ||
|
||
var validated = model.validate(obj); | ||
validated.should.have.property('conforms', false); | ||
validated.should.have.property('errors').eql([ | ||
{message: 'unexpected properties: html5', path: 'metadata'}, | ||
{message: 'expected value to be defined', path: 'source.text'}, | ||
]); | ||
|
||
validated.should.have.property('value').eql(_.defaults({ | ||
source: {raw: 'something', text: undefined}, | ||
createdAt: new Date('2016-07-27T04:51:22.820Z'), | ||
updatedAt: new Date('2016-07-27T04:51:22.820Z'), | ||
}, obj)); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module.exports = function (klay) { | ||
klay.use({defaults: {required: true, strict: true}}); | ||
var types = klay.builders; | ||
|
||
var HtmlMetadata = types.object({ | ||
title: types.string(), | ||
version: types.string(), | ||
}); | ||
|
||
var JsonMetadata = types.object({ | ||
type: types.enum(['object', 'array']), | ||
size: types.integer(), | ||
}); | ||
|
||
var HtmlSource = types.object({ | ||
raw: types.string(), | ||
text: types.string(), | ||
}); | ||
|
||
var JsonSource = types.object().strict(false); | ||
|
||
var Document = { | ||
id: types.uuid(), | ||
parentId: types.uuid(), | ||
type: types.enum(['html', 'json']), | ||
metadata: types.conditional(). | ||
option(HtmlMetadata, 'type', 'html'). | ||
option(JsonMetadata, 'type', 'json'), | ||
source: types.conditional(). | ||
option(HtmlSource, 'type', 'html'). | ||
option(JsonSource, 'type', 'json'), | ||
createdAt: types.date(), | ||
updatedAt: types.date(), | ||
}; | ||
|
||
return types.object(Document); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters