Skip to content

Commit

Permalink
Supports schema in array
Browse files Browse the repository at this point in the history
  • Loading branch information
mokkabonna committed Mar 12, 2018
1 parent 97a7377 commit 219f0e0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
23 changes: 14 additions & 9 deletions src/resolver.js
Expand Up @@ -134,41 +134,46 @@ function resolveLink(config, instance, instanceUri, attachmentPointer) {
return resolved
}

function createLink(ldo, pointer, parentPointer) {
function createLink(ldo, pointer, parentPointer, parentKeyWord) {
return {
parentKeyWord,
lastPointer: pointer.split('/').pop(),
schemaPointer: pointer,
parentPointer: parentPointer,
schemaPointer: pointer || '',
parentPointer: parentPointer || '',
ldo: ldo
}
}

function getAllSchemaLinks(schema) {
var links = []

traverse(schema, function(subSchema, pointer, root, parentPointer) {
links = links.concat((subSchema.links || []).map(l => createLink(l, pointer, parentPointer)))
traverse(schema, function(subSchema, pointer, root, parentPointer, parentKeyWord) {
links = links.concat((subSchema.links || []).map(l => createLink(l, pointer, parentPointer, parentKeyWord)))
})

return links
}

function schemaToInstancePointer(pointer) {
return pointer.split('/').filter(p => p !== 'properties').join('/')
return pointer.split('/properties/').join('/').split('/items/').join('/')
}

function resolve(schema, instance, instanceUri) {
var links = getAllSchemaLinks(schema)

var resolvedLinks = links.reduce(function(all, config) {
if (config.lastPointer === 'items') {
var dataPointer = schemaToInstancePointer(config.parentPointer)
var subSchema = jsonPointer(schema, config.schemaPointer)
if (config.parentKeyWord === 'items' && config.lastPointer !== 'items') {
let dataPointer = schemaToInstancePointer(config.schemaPointer)
all.push(resolveLink(config, jsonPointer(instance, dataPointer), instanceUri, dataPointer))
} else if (config.lastPointer === 'items') {
let dataPointer = schemaToInstancePointer(config.parentPointer)
let arr = jsonPointer(instance, dataPointer)
if (Array.isArray(arr)) {
all = all.concat(arr.map((instanceData, i) => resolveLink(config, instanceData, instanceUri, dataPointer + '/' + i)))
}
} else {
all.push(resolveLink(config, instance, instanceUri))
all.push(resolveLink(config, instance, instanceUri, ''))
}
return all
}, [])
Expand Down
59 changes: 49 additions & 10 deletions test/specs/resolver.spec.js
Expand Up @@ -439,17 +439,56 @@ describe('resolver', function() {
targetUri: 'https://api.example.com/things',
attachmentPointer: ''
}, {
"contextUri": "https://api.example.com/things",
"contextPointer": "",
"rel": "item",
"targetUri": "https://api.example.com/things/12345",
"attachmentPointer": "/elements/0"
contextUri: 'https://api.example.com/things',
contextPointer: '',
rel: 'item',
targetUri: 'https://api.example.com/things/12345',
attachmentPointer: '/elements/0'
}, {
"contextUri": "https://api.example.com/things",
"contextPointer": "",
"rel": "item",
"targetUri": "https://api.example.com/things/67890",
"attachmentPointer": "/elements/1"
contextUri: 'https://api.example.com/things',
contextPointer: '',
rel: 'item',
targetUri: 'https://api.example.com/things/67890',
attachmentPointer: '/elements/1'
}])
})

it('resolves when item is array', function() {
schema = {
type: 'object',
required: ['elements'],
properties: {
elements: {
type: 'array',
items: [{
links: [{
anchorPointer: '',
rel: 'item',
href: 'things/{id}'
}]
}]
}
},
links: [{
rel: 'self',
href: ''
}]
}

var resolved = resolver.resolve(schema, data, 'https://api.example.com/things')

expect(resolved).to.eql([{
contextUri: 'https://api.example.com/things',
contextPointer: '',
rel: 'self',
targetUri: 'https://api.example.com/things',
attachmentPointer: ''
}, {
contextUri: 'https://api.example.com/things',
contextPointer: '',
rel: 'item',
targetUri: 'https://api.example.com/things/12345',
attachmentPointer: '/elements/0'
}])
})
})
Expand Down

0 comments on commit 219f0e0

Please sign in to comment.