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

Use from to validation from utils #124

Merged
merged 4 commits into from
Aug 30, 2023
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- bumped blockfrost-tests to 2.8.0
- !from&to breaking change [] -> 400

### Added

- Flake now exports NixOS module
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"@blockfrost/blockfrost-js": "5.2.0",
"@blockfrost/blockfrost-utils": "2.7.1",
"@blockfrost/blockfrost-utils": "2.8.0",
"@blockfrost/openapi": "0.1.58",
"@emurgo/cardano-serialization-lib-nodejs": "11.3.0",
"@emurgo/cip14-js": "^3.0.1",
Expand All @@ -47,7 +47,7 @@
"rimraf": "^4.3.1"
},
"devDependencies": {
"@blockfrost/blockfrost-tests": "1.9.2",
"@blockfrost/blockfrost-tests": "1.9.4",
"@esbuild/darwin-x64": "npm:0.16.17",
"@esbuild/linux-x64": "npm:0.16.17",
"@types/config": "3.3.0",
Expand Down
3 changes: 2 additions & 1 deletion src/routes/addresses/address/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as QueryTypes from '../../../types/queries/addresses.js';
import * as ResponseTypes from '../../../types/responses/addresses.js';
import { getDbSync } from '../../../utils/database.js';
import { handle400Custom, handle404, handleInvalidAddress } from '../../../utils/error-handler.js';
import { getAdditionalParametersFromRequest, toJSONStream } from '../../../utils/string-utils.js';
import { toJSONStream } from '../../../utils/string-utils.js';
import { getAdditionalParametersFromRequest } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { getAddressTypeAndPaymentCred } from '../../../utils/validation.js';

async function route(fastify: FastifyInstance) {
Expand Down
15 changes: 14 additions & 1 deletion src/routes/assets/asset/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { handleInvalidAsset } from '@blockfrost/blockfrost-utils/lib/fastify.js';
import {
getAdditionalParametersFromRequest,
handle400Custom,
handleInvalidAsset,
} from '@blockfrost/blockfrost-utils/lib/fastify.js';
import { validateAsset } from '@blockfrost/blockfrost-utils/lib/validation.js';
import { getSchemaForEndpoint } from '@blockfrost/openapi';
import { FastifyInstance, FastifyRequest } from 'fastify';
Expand All @@ -22,6 +26,15 @@ async function route(fastify: FastifyInstance) {
return handleInvalidAsset(reply);
}

const fromToParameters = getAdditionalParametersFromRequest(
request.query.from,
request.query.to,
);

if (fromToParameters === 'outOfRangeOrMalformedErr') {
return handle400Custom(reply, 'Invalid (malformed or out of range) from/to parameter(s).');
}

const clientDbSync = await getDbSync(fastify);

try {
Expand Down
2 changes: 2 additions & 0 deletions src/types/queries/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface RequestAssetsParameters {
count: number;
page: number;
order: Order;
from: string;
to: string;
};
}

Expand Down
61 changes: 0 additions & 61 deletions src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,6 @@ export const getEndpointFromUrl = (url: string): string => {
return endpoint;
};

export const getAdditionalParametersFromRequest = (
from?: string,
to?: string,
): unknown[] | 'outOfRangeOrMalformedErr' => {
// eslint-disable-next-line unicorn/no-useless-undefined
const parameterArray: unknown[] = Array.from({ length: 4 }).fill(undefined);

try {
const minInt = 0;
const maxInt = 2_147_483_647;

if (from !== undefined) {
const fromTokens = from.split(':');
const requestParameterIsOK = fromTokens.length <= 2;

if (requestParameterIsOK) {
const [heightString, indexString] = fromTokens;
const height = Number.parseInt(heightString, 10);
const index = Number.parseInt(indexString, 10); // NaN in case of missing index

if (
height >= minInt &&
height <= maxInt &&
(indexString === undefined || (index >= minInt && index <= maxInt))
) {
parameterArray[0] = heightString;
parameterArray[1] = indexString;
} else {
return 'outOfRangeOrMalformedErr';
}
}
}

if (to !== undefined) {
const toTokens = to.split(':');
const requestParameterIsOK = toTokens.length <= 2;

if (requestParameterIsOK) {
const [heightString, indexString] = toTokens;
const height = Number.parseInt(heightString, 10);
const index = Number.parseInt(indexString, 10); // NaN in case of missing index

if (
height >= minInt &&
height <= maxInt &&
(indexString === undefined || (index >= minInt && index <= maxInt))
) {
parameterArray[2] = heightString;
parameterArray[3] = indexString;
} else {
return 'outOfRangeOrMalformedErr';
}
}
}
} catch (error) {
console.error(error);
return 'outOfRangeOrMalformedErr';
}
return parameterArray;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const sortKeysInObject = (object: any) => {
if (object === null || typeof object !== 'object') return object;
Expand Down
59 changes: 0 additions & 59 deletions test/unit/tests/routes/string-utils.unit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
getEndpointFromUrl,
getAdditionalParametersFromRequest,
sortKeysInObject,
toJSONStream,
} from '../../../../src/utils/string-utils.js';
Expand Down Expand Up @@ -28,64 +27,6 @@ describe('stringUtils', async () => {
expect(getEndpointFromUrl('/pools?order=desc&count=5')).toStrictEqual('pools');
});

test('getAdditionalParametersFromRequest', () => {
expect(getAdditionalParametersFromRequest('1', '2')).toStrictEqual([
'1',
undefined,
'2',
undefined,
]);
expect(getAdditionalParametersFromRequest('1:3', '2')).toStrictEqual([
'1',
'3',
'2',
undefined,
]);
expect(getAdditionalParametersFromRequest('1', '2:4')).toStrictEqual([
'1',
undefined,
'2',
'4',
]);
expect(getAdditionalParametersFromRequest('1:3', '2:4')).toStrictEqual(['1', '3', '2', '4']);
expect(getAdditionalParametersFromRequest('1')).toStrictEqual([
'1',
undefined,
undefined,
undefined,
]);
expect(getAdditionalParametersFromRequest('1:3')).toStrictEqual([
'1',
'3',
undefined,
undefined,
]);
expect(getAdditionalParametersFromRequest(undefined, '2')).toStrictEqual([
undefined,
undefined,
'2',
undefined,
]);
expect(getAdditionalParametersFromRequest(undefined, '2:4')).toStrictEqual([
undefined,
undefined,
'2',
'4',
]);
expect(getAdditionalParametersFromRequest()).toStrictEqual([
undefined,
undefined,
undefined,
undefined,
]);
expect(getAdditionalParametersFromRequest('1:54545454545')).toStrictEqual(
'outOfRangeOrMalformedErr',
);
expect(getAdditionalParametersFromRequest(undefined, '-1:44')).toStrictEqual(
'outOfRangeOrMalformedErr',
);
});

test('sortKeysInObject', () => {
// eslint-disable-next-line unicorn/no-useless-undefined
expect(sortKeysInObject(undefined)).toStrictEqual(undefined);
Expand Down
Loading
Loading