Skip to content

Commit

Permalink
fix: Mapping validation
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio committed Jul 24, 2024
1 parent 180485e commit a2f956e
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 33 deletions.
10 changes: 7 additions & 3 deletions src/Item/Item.router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ describe('Item router', () => {
})
})

describe.only('and the URN is not in use', () => {
describe('and the URN is not in use', () => {
let resultingItem: ResultItem

beforeEach(() => {
Expand All @@ -1178,7 +1178,11 @@ describe('Item router', () => {
})
)
itemToUpsert.mappings = {
mainnet: { '0x0': [{ type: MappingType.ANY }] },
mainnet: {
'0x74c78f5A4ab22F01d5fd08455cf0Ff5C3367535C': [
{ type: MappingType.ANY },
],
},
}
const updatedItem = {
...dbTPItem,
Expand All @@ -1187,7 +1191,7 @@ describe('Item router', () => {
eth_address: wallet.address,
mappings: itemToUpsert.mappings,
local_content_hash:
'b3520ef20163848f0fc69fc6aee1f7240c7ef4960944fcd92ce2e67a62828f6f',
'037a7a0cf5fa9bcd6b2afc9de8803a3601f50d69e2b0a1757016252f5f34a449',
}
mockThirdPartyURNExists(itemToUpsert.urn!, false)
resultingItem = {
Expand Down
2 changes: 1 addition & 1 deletion src/Item/Item.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const baseItemSchema = Object.freeze({
},
utility: { type: ['string', 'null'], maxLength: 64 },
content_hash: { type: ['string', 'null'] },
mappings: { ...Mappings.schema, nullable: true },
mappings: { ...Mappings.schema, type: ['object', 'null'] },
},
additionalProperties: false,
anyOf: [{ required: ['id'] }, { required: ['urn'] }],
Expand Down
58 changes: 32 additions & 26 deletions src/Item/hashes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,40 +171,46 @@ describe('when calculating the hashes of a TP item', () => {
}
})

describe('and the item is a third party v1 item', () => {
describe('and the item is a third party item', () => {
beforeEach(() => {
dbCollection.third_party_id =
'urn:decentraland:amoy:collections-thirdparty:dcl-tests'
})

it("should return the hash of the item's entity", () => {
return expect(
calculateItemContentHash(dbItem, dbCollection)
).resolves.toEqual(
'fa107ac8f8a5444454532548b2d906569a275573a7158a6a170f0592f9368313'
)
})
})
describe('and the item has no mappings', () => {
beforeEach(() => {
dbItem.mappings = null
})

describe('and the item is a third party v2 item', () => {
beforeEach(() => {
dbCollection.third_party_id =
'urn:decentraland:matic:collections-linked-wearables:dcl-tests'
dbCollection.urn_suffix =
'mainnet:0x74c78f5A4ab22F01d5fd08455cf0Ff5C3367535C'
dbItem.mappings = [
{
type: MappingType.ANY,
},
]
it("should return the hash of the item's entity", () => {
return expect(
calculateItemContentHash(dbItem, dbCollection)
).resolves.toEqual(
'fa107ac8f8a5444454532548b2d906569a275573a7158a6a170f0592f9368313'
)
})
})

it("should return the hash of the item's entity", () => {
return expect(
calculateItemContentHash(dbItem, dbCollection)
).resolves.toEqual(
'7733fd481132e580abff759ace6a490692c992d6c19938035e9322a960fec0a3'
)
describe('and the item has mappings', () => {
beforeEach(() => {
dbItem.mappings = {
amoy: {
'0x74c78f5A4ab22F01d5fd08455cf0Ff5C3367535C': [
{
type: MappingType.ANY,
},
],
},
}
})

it("should return the hash of the item's entity", () => {
return expect(
calculateItemContentHash(dbItem, dbCollection)
).resolves.toEqual(
'80388e7d6601cf23253b74891e7bfe377bc75d5b7e58b23d6e42e5ab65a54daa'
)
})
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/utils/urn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('when decoding the an item URN', () => {

it('should throw indicating that the URN is not item compliant', () => {
expect(() => decodeThirdPartyItemURN(urn)).toThrow(
'The given item URN is not item compliant'
'The given item URN is not TP compliant'
)
})
})
Expand All @@ -22,7 +22,7 @@ describe('when decoding the an item URN', () => {

it('should throw indicating that the URN is not Third Party compliant', () => {
expect(() => decodeThirdPartyItemURN(urn)).toThrow(
'The given item URN is not Third Party compliant'
'The given item URN is not TP compliant'
)
})
})
Expand Down
10 changes: 9 additions & 1 deletion src/utils/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import Ajv from 'ajv'
import { Mappings, RangeMapping } from '@dcl/schemas'
import addFormats from 'ajv-formats'

const mappingsValidator = (...args: any[]): Promise<any> | boolean =>
args[1] && Mappings._isMappingsValid.validate
? Mappings._isMappingsValid.validate.apply(
Mappings._isMappingsValid,
args as any
)
: Promise.resolve(true)

export function getValidator() {
const ajv = new Ajv({
removeAdditional: true,
Expand All @@ -12,7 +20,7 @@ export function getValidator() {
...RangeMapping._fromLessThanOrEqualTo,
keyword: '_fromLessThanOrEqualTo',
})
.addKeyword({ ...Mappings._isMappingsValid, keyword: '_isMappingsValid' })
.addKeyword({ ...Mappings._isMappingsValid, validate: mappingsValidator })
addFormats(ajv)
return ajv
}

0 comments on commit a2f956e

Please sign in to comment.