Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Mappings schema validation #286

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions report/schemas.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import Ajv from 'ajv';
import type { Emitter } from 'mitt';
import { ErrorObject } from 'ajv';
import { FuncKeywordDefinition } from 'ajv';
import { JSONSchemaType } from 'ajv';
import { KeywordDefinition } from 'ajv';

Expand Down Expand Up @@ -1424,11 +1425,7 @@ export type Mappings = Partial<Record<ContractNetwork, Record<ContractAddress, M
// @alpha
export namespace Mappings {
const // (undocumented)
_isMappingsValid: {
keyword: string;
validate: (schema: boolean, data: any) => boolean;
errors: boolean;
};
_isMappingsValid: FuncKeywordDefinition;
const // (undocumented)
innerSchema: JSONSchema<Record<ContractAddress, Mapping[]>>;
const // (undocumented)
Expand Down Expand Up @@ -3387,12 +3384,12 @@ export namespace WorldConfiguration {
// src/platform/events/blockchain.ts:21:3 - (ae-forgotten-export) The symbol "BidMetadata" needs to be exported by the entry point index.d.ts
// src/platform/events/blockchain.ts:163:3 - (ae-forgotten-export) The symbol "RentalMetadata" needs to be exported by the entry point index.d.ts
// src/platform/item/emote/adr74/emote-data-adr74.ts:7:3 - (ae-incompatible-release-tags) The symbol "representations" is marked as @public, but its signature references "EmoteRepresentationADR74" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:253:3 - (ae-incompatible-release-tags) The symbol "getMappings" is marked as @public, but its signature references "Mappings" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:254:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "ContractNetwork" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:254:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "ContractAddress" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:254:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "Mapping" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:255:3 - (ae-incompatible-release-tags) The symbol "includesNft" is marked as @public, but its signature references "ContractNetwork" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:255:3 - (ae-incompatible-release-tags) The symbol "includesNft" is marked as @public, but its signature references "ContractAddress" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:252:3 - (ae-incompatible-release-tags) The symbol "getMappings" is marked as @public, but its signature references "Mappings" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:253:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "ContractNetwork" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:253:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "ContractAddress" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:253:3 - (ae-incompatible-release-tags) The symbol "addMapping" is marked as @public, but its signature references "Mapping" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:254:3 - (ae-incompatible-release-tags) The symbol "includesNft" is marked as @public, but its signature references "ContractNetwork" which is marked as @alpha
// src/platform/item/linked-wearable-mappings.ts:254:3 - (ae-incompatible-release-tags) The symbol "includesNft" is marked as @public, but its signature references "ContractAddress" which is marked as @alpha
// src/platform/item/third-party-props.ts:7:3 - (ae-incompatible-release-tags) The symbol "merkleProof" is marked as @public, but its signature references "MerkleProof" which is marked as @alpha
// src/platform/item/third-party-props.ts:9:3 - (ae-incompatible-release-tags) The symbol "mappings" is marked as @public, but its signature references "Mappings" which is marked as @alpha
// src/platform/scene/feature-toggles.ts:11:3 - (ae-forgotten-export) The symbol "EnabledDisabled" needs to be exported by the entry point index.d.ts
Expand Down
17 changes: 8 additions & 9 deletions src/platform/item/linked-wearable-mappings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { generateLazyValidator, JSONSchema, ValidateFunction } from '../../validation'
import { KeywordDefinition } from 'ajv'
import { ThirdPartyProps } from './third-party-props'
import { FuncKeywordDefinition, KeywordDefinition } from 'ajv'

/**
* MappingType
Expand Down Expand Up @@ -203,17 +202,17 @@ export namespace Mapping {
* @alpha
*/
export namespace Mappings {
export const _isMappingsValid = {
export const _isMappingsValid: FuncKeywordDefinition = {
keyword: '_isMappingsValid',
validate: function (schema: boolean, data: any) {
const itemAsThirdParty = data as ThirdPartyProps
validate: function (data: Mappings) {
try {
createMappingsHelper(itemAsThirdParty.mappings)
createMappingsHelper(data)
} catch (_) {
return false
}
return true
},
schema: false,
errors: false
}

Expand All @@ -227,8 +226,7 @@ export namespace Mappings {
},
minProperties: 1,
required: [],
additionalProperties: false,
_isMappingsValid: true
additionalProperties: false
}

const properties = Object.values(ContractNetwork).reduce((acc, network) => {
Expand All @@ -240,7 +238,8 @@ export namespace Mappings {
type: 'object',
properties,
minProperties: 1,
additionalProperties: false
additionalProperties: false,
_isMappingsValid: true
}

export const validate: ValidateFunction<Mappings> = generateLazyValidator(schema, [
Expand Down
12 changes: 12 additions & 0 deletions test/platform/item/linked-wearable-mappings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe('Third Party Mappings tests', () => {
expect(Mappings.validate(mappings)).toEqual(true)
expect(Mappings.validate(null)).toEqual(false)
expectValidationFailureWithErrors(Mappings.validate, {}, ['must NOT have fewer than 1 properties'])
expectValidationFailureWithErrors(
Mappings.validate,
{
amoy: {
'0x1d9fb685c257E74f869BA302e260C0b68f5eBB37': [
{ type: MappingType.ANY },
{ type: MappingType.SINGLE, id: '0' }
]
}
},
['must pass "_isMappingsValid" keyword validation']
)
})
})

Expand Down
Loading