Skip to content

Commit

Permalink
refactor: replace ajv with is-my-json-valid
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Feb 13, 2024
1 parent 8a94f69 commit f739324
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
6 changes: 6 additions & 0 deletions .changeset/weak-guests-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@near-js/accounts": patch
"near-api-js": patch
---

replace ajv with is-my-json-valid
7 changes: 3 additions & 4 deletions packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
"@near-js/transactions": "workspace:*",
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"ajv": "8.11.2",
"ajv-formats": "2.1.1",
"bn.js": "5.2.1",
"borsh": "1.0.0",
"depd": "2.0.0",
"is-my-json-valid": "^2.20.6",
"lru_map": "0.4.1",
"near-abi": "0.1.1"
},
Expand All @@ -36,9 +35,9 @@
"bs58": "4.0.0",
"jest": "26.0.1",
"near-hello": "0.5.1",
"near-workspaces": "3.4.0",
"ts-jest": "26.5.6",
"typescript": "4.9.4",
"near-workspaces": "3.4.0"
"typescript": "4.9.4"
},
"files": [
"lib"
Expand Down
32 changes: 7 additions & 25 deletions packages/accounts/src/contract.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { getTransactionLastResult, Logger } from '@near-js/utils';
import { ArgumentTypeError, PositionalArgsError } from '@near-js/types';
import { LocalViewExecution } from './local-view-execution';
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import validator from 'is-my-json-valid'
import BN from 'bn.js';
import depd from 'depd';
import { AbiFunction, AbiFunctionKind, AbiRoot, AbiSerializationType } from 'near-abi';

import { Account } from './account';
import { UnsupportedSerializationError, UnknownArgumentError, ArgumentSchemaError, ConflictingOptions } from './errors';


// Makes `function.name` return given name
function nameFunction(name: string, body: (args?: any[]) => any) {
return {
Expand All @@ -20,7 +18,7 @@ function nameFunction(name: string, body: (args?: any[]) => any) {
}[name];
}

function validateArguments(args: object, abiFunction: AbiFunction, ajv: Ajv, abiRoot: AbiRoot) {
function validateArguments(args: object, abiFunction: AbiFunction, abiRoot: AbiRoot) {
if (!isObject(args)) return;

if (abiFunction.params && abiFunction.params.serialization_type !== AbiSerializationType.Json) {
Expand All @@ -36,8 +34,9 @@ function validateArguments(args: object, abiFunction: AbiFunction, ajv: Ajv, abi
const arg = args[p.name];
const typeSchema = p.type_schema;
typeSchema.definitions = abiRoot.body.root_schema.definitions;
const validate = ajv.compile(typeSchema);
if (!validate(arg)) {
const validate = validator(typeSchema)
const valid = validate(arg)
if (!valid) {
throw new ArgumentSchemaError(p.name, validate.errors);
}
}
Expand All @@ -50,22 +49,6 @@ function validateArguments(args: object, abiFunction: AbiFunction, ajv: Ajv, abi
}
}

function createAjv() {
// Strict mode is disabled for now as it complains about unknown formats. We need to
// figure out if we want to support a fixed set of formats. `uint32` and `uint64`
// are added explicitly just to reduce the amount of warnings as these are very popular
// types.
const ajv = new Ajv({
strictSchema: false,
formats: {
uint32: true,
uint64: true
}
});
addFormats(ajv);
return ajv;
}

const isUint8Array = (x: any) =>
x && x.byteLength !== undefined && x.byteLength === x.length;

Expand Down Expand Up @@ -171,7 +154,6 @@ export class Contract {
.map((methodAbi) => ({ name: methodAbi.name, abi: methodAbi }));
}

const ajv = createAjv();
viewMethodsWithAbi.forEach(({ name, abi }) => {
Object.defineProperty(this, name, {
writable: false,
Expand All @@ -182,7 +164,7 @@ export class Contract {
}

if (abi) {
validateArguments(args, abi, ajv, abiRoot);
validateArguments(args, abi, abiRoot);
}

if (useLocalViewExecution) {
Expand Down Expand Up @@ -228,7 +210,7 @@ export class Contract {
}

if (abi) {
validateArguments(args[0].args, abi, ajv, abiRoot);
validateArguments(args[0].args, abi, abiRoot);
}

return this._changeMethod({ methodName: name, ...args[0] });
Expand Down
4 changes: 2 additions & 2 deletions packages/accounts/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorObject } from 'ajv';
import { ValidationError } from 'is-my-json-valid';

export class UnsupportedSerializationError extends Error {
constructor(methodName: string, serializationType: string) {
Expand All @@ -13,7 +13,7 @@ export class UnknownArgumentError extends Error {
}

export class ArgumentSchemaError extends Error {
constructor(argName: string, errors: ErrorObject[]) {
constructor(argName: string, errors: ValidationError[]) {
super(`Argument '${argName}' does not conform to the specified ABI schema: '${JSON.stringify(errors)}'`);
}
}
Expand Down
2 changes: 0 additions & 2 deletions packages/near-api-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
"@near-js/types": "workspace:*",
"@near-js/utils": "workspace:*",
"@near-js/wallet-account": "workspace:*",
"ajv": "8.11.2",
"ajv-formats": "2.1.1",
"bn.js": "5.2.1",
"borsh": "1.0.0",
"depd": "2.0.0",
Expand Down
62 changes: 43 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f739324

Please sign in to comment.