Skip to content

Commit

Permalink
chore: update 0x deps (#659)
Browse files Browse the repository at this point in the history
* chore: update 0x deps to latest and fix json schema ajv issues WIP

* fix: json schema validation tests
  • Loading branch information
kimpers committed Jun 28, 2021
1 parent 246eeff commit 2e301f9
Show file tree
Hide file tree
Showing 6 changed files with 1,504 additions and 745 deletions.
32 changes: 16 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
]
},
"devDependencies": {
"@0x/contracts-erc20": "^3.3.6",
"@0x/contracts-test-utils": "^5.3.24",
"@0x/dev-utils": "^4.2.2",
"@0x/migrations": "^8.0.1",
"@0x/tslint-config": "^4.0.0",
"@0x/typescript-typings": "^5.1.5",
"@0x/contracts-erc20": "^3.3.13",
"@0x/contracts-test-utils": "^5.4.5",
"@0x/dev-utils": "^4.2.8",
"@0x/migrations": "^8.0.11",
"@0x/tslint-config": "^4.1.4",
"@0x/typescript-typings": "^5.2.0",
"@balancer-labs/sor": "^0.3.0",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/exec": "^5.0.0",
Expand Down Expand Up @@ -137,21 +137,21 @@
},
"dependencies": {
"@0x/api-utils": "^0.0.1",
"@0x/assert": "^3.0.17",
"@0x/assert": "^3.0.28",
"@0x/asset-swapper": "^6.18.2",
"@0x/contract-addresses": "^6.3.1",
"@0x/contract-wrappers": "^13.17.0",
"@0x/contracts-zero-ex": "^0.21.1",
"@0x/json-schemas": "^5.3.1",
"@0x/contract-addresses": "^6.4.0",
"@0x/contract-wrappers": "^13.17.2",
"@0x/contracts-zero-ex": "^0.26.0",
"@0x/json-schemas": "^6.2.0",
"@0x/mesh-graphql-client": "^11.2.0",
"@0x/order-utils": "^10.4.19",
"@0x/order-utils": "^10.4.26",
"@0x/protocol-utils": "^1.7.2",
"@0x/quote-server": "^6.0.4",
"@0x/subproviders": "^6.4.2",
"@0x/quote-server": "^6.0.6",
"@0x/subproviders": "^6.5.4",
"@0x/token-metadata": "^0.1.4",
"@0x/types": "^3.3.2",
"@0x/types": "^3.3.3",
"@0x/utils": "^6.4.3",
"@0x/web3-wrapper": "^7.5.0",
"@0x/web3-wrapper": "^7.5.4",
"@ethersproject/hdnode": "^5.2.0",
"aws-sdk": "^2.908.0",
"axios": "^0.21.1",
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/sra_orders_query_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"$ref": "/addressSchema"
},
"pool": {
"type": "hexSchema"
"$ref": "/hexSchema"
},
"expiry": {
"$ref": "/wholeNumberSchema"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/quote_server_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class QuoteServerClient {
headers: { 'Content-Type': 'application/json' },
});
const validator = schemaValidator.validate(response.data, schemas.submitReceiptSchema);
if (validator.errors.length > 0) {
if (validator.errors && validator.errors.length > 0) {
const errorsMsg = validator.errors.map((err) => err.toString()).join(',');
throw new Error(`Error from validator: ${errorsMsg}`);
}
Expand Down
39 changes: 18 additions & 21 deletions src/utils/schema_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Schema, SchemaValidator } from '@0x/json-schemas';
import { ValidationError as SchemaValidationError } from 'jsonschema';
import { AJV, SchemaValidator } from '@0x/json-schemas';

import { ValidationError, ValidationErrorCodes, ValidationErrorItem } from '../errors';
import { schemas } from '../schemas';
Expand All @@ -12,9 +11,9 @@ for (const schema of Object.values(schemas)) {
}

export const schemaUtils = {
validateSchema(instance: any, schema: Schema): void {
validateSchema(instance: any, schema: object): void {
const validationResult = schemaValidator.validate(instance, schema);
if (validationResult.errors.length === 0) {
if (!validationResult.errors || validationResult.errors.length === 0) {
return;
} else {
const validationErrorItems = validationResult.errors.map((schemaValidationError) =>
Expand All @@ -23,14 +22,12 @@ export const schemaUtils = {
throw new ValidationError(validationErrorItems);
}
},
addSchema(schema: Schema): void {
addSchema(schema: object): void {
schemaValidator.addSchema(schema);
},
};

function schemaValidationErrorToValidationErrorItem(
schemaValidationError: Omit<SchemaValidationError, 'stack'>,
): ValidationErrorItem {
function schemaValidationErrorToValidationErrorItem(schemaValidationErrorObject: AJV.ErrorObject): ValidationErrorItem {
if (
[
'type',
Expand All @@ -45,36 +42,36 @@ function schemaValidationErrorToValidationErrorItem(
'uniqueItems',
'items',
'dependencies',
].includes(schemaValidationError.name)
].includes(schemaValidationErrorObject.keyword)
) {
return {
field: schemaValidationError.property,
field: schemaValidationErrorObject.dataPath.replace('.', ''),
code: ValidationErrorCodes.IncorrectFormat,
reason: schemaValidationError.message,
reason: schemaValidationErrorObject.message || '',
};
} else if (
['minimum', 'maximum', 'minLength', 'maxLength', 'minItems', 'maxItems', 'enum', 'const'].includes(
schemaValidationError.name,
schemaValidationErrorObject.keyword,
)
) {
return {
field: schemaValidationError.property,
field: schemaValidationErrorObject.dataPath.replace('.', ''),
code: ValidationErrorCodes.ValueOutOfRange,
reason: schemaValidationError.message,
reason: schemaValidationErrorObject.message || '',
};
} else if (schemaValidationError.name === 'required') {
} else if (schemaValidationErrorObject.keyword === 'required') {
return {
field: schemaValidationError.argument,
field: (schemaValidationErrorObject.params as AJV.RequiredParams).missingProperty,
code: ValidationErrorCodes.RequiredField,
reason: schemaValidationError.message,
reason: schemaValidationErrorObject.message || '',
};
} else if (schemaValidationError.name === 'not') {
} else if (schemaValidationErrorObject.keyword === 'not') {
return {
field: schemaValidationError.property,
field: schemaValidationErrorObject.dataPath.replace('.', ''),
code: ValidationErrorCodes.UnsupportedOption,
reason: schemaValidationError.message,
reason: schemaValidationErrorObject.message || '',
};
} else {
throw new Error(`Unknnown schema validation error name: ${schemaValidationError.name}`);
throw new Error(`Unknown schema validation error name: ${schemaValidationErrorObject.keyword}`);
}
}
16 changes: 8 additions & 8 deletions test/sra_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,15 @@ describe(SUITE_NAME, () => {
code: 100,
reason: 'Validation Failed',
validationErrors: [
{
field: 'instance.quoteToken', // FIXME (xianny): bug in jsonschemas module
code: 1001,
reason: 'does not match pattern "^0x[0-9a-fA-F]{40}$"',
},
{
field: 'baseToken',
code: 1000,
reason: 'requires property "baseToken"',
reason: "should have required property 'baseToken'",
},
{
field: 'quoteToken',
code: 1001,
reason: 'should match pattern "^0x[0-9a-fA-F]{40}$"',
},
],
};
Expand Down Expand Up @@ -371,12 +371,12 @@ describe(SUITE_NAME, () => {
{
field: 'taker',
code: ValidationErrorCodes.RequiredField,
reason: 'requires property "taker"',
reason: "should have required property 'taker'",
},
{
field: 'expiry',
code: ValidationErrorCodes.RequiredField,
reason: 'requires property "expiry"',
reason: "should have required property 'expiry'",
},
],
};
Expand Down
Loading

0 comments on commit 2e301f9

Please sign in to comment.