Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Apr 19, 2021
1 parent d940c4d commit 9d25bb3
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 196 deletions.
34 changes: 17 additions & 17 deletions elements/lisk-chain/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,39 +118,39 @@ export const validateGenesisBlockHeader = (block: GenesisBlock, accountSchema: S
// Custom header validation not possible with validator
if (!header.generatorPublicKey.equals(GENESIS_BLOCK_GENERATOR_PUBLIC_KEY)) {
errors.push({
message: 'should be equal to constant',
message: 'must be equal to constant',
keyword: 'const',
dataPath: 'header.generatorPublicKey',
instancePath: '/header/generatorPublicKey',
schemaPath: 'properties.generatorPublicKey',
params: { allowedValue: GENESIS_BLOCK_GENERATOR_PUBLIC_KEY },
});
}

if (header.reward !== GENESIS_BLOCK_REWARD) {
errors.push({
message: 'should be equal to constant',
message: 'must be equal to constant',
keyword: 'const',
dataPath: 'header.reward',
instancePath: '/header/reward',
schemaPath: 'properties.reward',
params: { allowedValue: GENESIS_BLOCK_REWARD },
});
}

if (!header.signature.equals(GENESIS_BLOCK_SIGNATURE)) {
errors.push({
message: 'should be equal to constant',
message: 'must be equal to constant',
keyword: 'const',
dataPath: 'header.signature',
instancePath: '/header/signature',
schemaPath: 'properties.signature',
params: { allowedValue: GENESIS_BLOCK_SIGNATURE },
});
}

if (!header.transactionRoot.equals(GENESIS_BLOCK_TRANSACTION_ROOT)) {
errors.push({
message: 'should be equal to constant',
message: 'must be equal to constant',
keyword: 'const',
dataPath: 'header.transactionRoot',
instancePath: '/header/transactionRoot',
schemaPath: 'properties.transactionRoot',
params: { allowedValue: GENESIS_BLOCK_TRANSACTION_ROOT },
});
Expand All @@ -159,27 +159,27 @@ export const validateGenesisBlockHeader = (block: GenesisBlock, accountSchema: S
errors.push({
message: 'Payload length must be zero',
keyword: 'const',
dataPath: 'payload',
instancePath: '/payload',
schemaPath: 'properties.payload',
params: { allowedValue: [] },
});
}

if (!objects.bufferArrayUniqueItems(header.asset.initDelegates as Buffer[])) {
errors.push({
dataPath: '.initDelegates',
instancePath: '/initDelegates',
keyword: 'uniqueItems',
message: 'should NOT have duplicate items',
message: 'must NOT have duplicate items',
params: {},
schemaPath: '#/properties/initDelegates/uniqueItems',
});
}

if (!objects.bufferArrayOrderByLex(header.asset.initDelegates as Buffer[])) {
errors.push({
message: 'should be lexicographically ordered',
message: 'must be lexicographically ordered',
keyword: 'initDelegates',
dataPath: 'header.asset.initDelegates',
instancePath: '/header/asset/initDelegates',
schemaPath: 'properties.initDelegates',
params: { initDelegates: header.asset.initDelegates },
});
Expand All @@ -199,19 +199,19 @@ export const validateGenesisBlockHeader = (block: GenesisBlock, accountSchema: S

if (!objects.bufferArrayEqual(accountAddresses, copiedAddresses)) {
errors.push({
message: 'should be length and lexicographically ordered',
message: 'must be length and lexicographically ordered',
keyword: 'accounts',
dataPath: 'header.asset.accounts',
instancePath: '/header/asset/accounts',
schemaPath: 'properties.accounts',
params: { orderKey: 'address' },
});
}

if (!objects.bufferArrayUniqueItems(accountAddresses)) {
errors.push({
dataPath: '.accounts',
instancePath: '/accounts',
keyword: 'uniqueItems',
message: 'should NOT have duplicate items',
message: 'must NOT have duplicate items',
params: {},
schemaPath: '#/properties/accounts/uniqueItems',
});
Expand Down
14 changes: 7 additions & 7 deletions elements/lisk-chain/test/unit/process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ describe('chain/process block', () => {
expect.objectContaining({
message: 'should be lexicographically ordered',
keyword: 'initDelegates',
dataPath: 'header.asset.initDelegates',
instancePath: 'header/asset/initDelegates',
schemaPath: 'properties.initDelegates',
}),
);
Expand Down Expand Up @@ -825,7 +825,7 @@ describe('chain/process block', () => {
expect((error as LiskValidationError).errors).toHaveLength(1);
expect((error as LiskValidationError).errors[0]).toEqual(
expect.objectContaining({
dataPath: '.initDelegates',
instancePath: 'initDelegates',
keyword: 'uniqueItems',
message: 'should NOT have duplicate items',
params: {},
Expand Down Expand Up @@ -855,7 +855,7 @@ describe('chain/process block', () => {
expect((error as LiskValidationError).errors).toHaveLength(1);
expect((error as LiskValidationError).errors[0]).toEqual(
expect.objectContaining({
dataPath: '.initDelegates',
instancePath: 'initDelegates',
keyword: 'minItems',
message: 'should NOT have fewer than 1 items',
params: {
Expand Down Expand Up @@ -965,7 +965,7 @@ describe('chain/process block', () => {
expect.objectContaining({
message: 'should be length and lexicographically ordered',
keyword: 'accounts',
dataPath: 'header.asset.accounts',
instancePath: 'header/asset/accounts',
schemaPath: 'properties.accounts',
params: { orderKey: 'address' },
}),
Expand Down Expand Up @@ -999,7 +999,7 @@ describe('chain/process block', () => {
expect.objectContaining({
message: 'should be length and lexicographically ordered',
keyword: 'accounts',
dataPath: 'header.asset.accounts',
instancePath: 'header/asset/accounts',
schemaPath: 'properties.accounts',
params: { orderKey: 'address' },
}),
Expand Down Expand Up @@ -1030,7 +1030,7 @@ describe('chain/process block', () => {
expect.objectContaining({
message: 'should be length and lexicographically ordered',
keyword: 'accounts',
dataPath: 'header.asset.accounts',
instancePath: 'header/asset/accounts',
schemaPath: 'properties.accounts',
params: { orderKey: 'address' },
}),
Expand Down Expand Up @@ -1062,7 +1062,7 @@ describe('chain/process block', () => {
expect((error as LiskValidationError).errors).toHaveLength(1);
expect((error as LiskValidationError).errors[0]).toEqual(
expect.objectContaining({
dataPath: '.accounts',
instancePath: 'accounts',
keyword: 'uniqueItems',
message: 'should NOT have duplicate items',
params: {},
Expand Down
8 changes: 4 additions & 4 deletions elements/lisk-transaction-pool/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@
export class TransactionPoolError extends Error {
public message: string;
public id: Buffer;
public dataPath: string;
public instancePath: string;
public actual?: string | number;
public expected?: string | number;
public constructor(
message = '',
id = Buffer.alloc(0),
dataPath = '',
instancePath = '',
actual?: string | number,
expected?: string | number,
) {
super();
this.message = message;
this.id = id;
this.name = 'TransactionPoolError';
this.dataPath = dataPath;
this.instancePath = instancePath;
this.actual = actual;
this.expected = expected;
}

public toString(): string {
const defaultMessage = `TransactionPool: ${this.id.toString('hex')} failed to process at ${
this.dataPath
this.instancePath
}: ${this.message}`;
const withActual = this.actual
? // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
Expand Down
2 changes: 1 addition & 1 deletion elements/lisk-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@types/node": "12.20.6",
"@types/semver": "7.1.0",
"@types/validator": "12.0.1",
"ajv": "6.12.0",
"ajv": "8.1.0",
"debug": "4.3.1",
"semver": "7.1.3",
"validator": "12.2.0"
Expand Down
14 changes: 7 additions & 7 deletions elements/lisk-validator/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

export interface ErrorObject {
keyword: string;
dataPath?: string;
instancePath?: string;
schemaPath?: string;
params: ErrorParams;
// Added to validation errors of propertyNames keyword schema
Expand Down Expand Up @@ -56,18 +56,18 @@ interface KeywordDataFormatters {

const errorFormatterMap: KeywordDataFormatters = {
type: error =>
`Property '${error.dataPath ?? ''}' should be of type '${errorParamToString(
`Property '${error.instancePath ?? ''}' should be of type '${errorParamToString(
error.params.type,
)}'`,
additionalProperties: error =>
`Property '${error.dataPath ?? ''}' has extraneous property '${errorParamToString(
`Property '${error.instancePath ?? ''}' has extraneous property '${errorParamToString(
error.params.additionalProperty,
)}'`,
minLength: error => `Property '${error.dataPath ?? ''}' ${errorParamToString(error.message)}`,
maxLength: error => `Property '${error.dataPath ?? ''}' ${errorParamToString(error.message)}`,
format: error => `Property '${error.dataPath ?? ''}' ${errorParamToString(error.message)}`,
minLength: error => `Property '${error.instancePath ?? ''}' ${errorParamToString(error.message)}`,
maxLength: error => `Property '${error.instancePath ?? ''}' ${errorParamToString(error.message)}`,
format: error => `Property '${error.instancePath ?? ''}' ${errorParamToString(error.message)}`,
required: error => `Missing property, ${errorParamToString(error.message)}`,
dataType: error => `Property '${error.dataPath ?? ''}' ${errorParamToString(error.message)}`,
dataType: error => `Property '${error.instancePath ?? ''}' ${errorParamToString(error.message)}`,
};

const defaultErrorFormatter: KeywordFormatterFunction = error =>
Expand Down
60 changes: 20 additions & 40 deletions elements/lisk-validator/src/keywords/data_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
* Removal or modification of this copyright notice is prohibited.
*/

import {
AnySchemaObject,
SchemaObjCxt,
ValidateFunction,
ErrorObject,
KeywordDefinition,
} from 'ajv';
import * as createDebug from 'debug';
import { LiskValidationError, ErrorObject } from '../errors';
import { LiskValidationError } from '../errors';
import {
isBoolean,
isBytes,
Expand All @@ -32,40 +39,16 @@ export const metaSchema = {
enum: ['bytes', 'uint32', 'sint32', 'uint64', 'sint64', 'string', 'boolean'],
};

type ValidateFunction = (
data: string,
dataPath?: string,
parentData?: object,
parentDataProperty?: string | number,
rootData?: object,
) => boolean;

interface AjvContext {
root: {
schema: object;
};
schemaPath: string;
}

interface KVPair {
[key: string]: unknown;
}
interface ValidateFunctionContext {
errors?: ErrorObject[];
(
data: Buffer | bigint | string | number,
dataPath?: string,
parentData?: object,
parentDataProperty?: string | number,
rootData?: object,
): boolean;

interface DataValidateFunction {
errors?: Partial<ErrorObject>[];
(...args: Parameters<ValidateFunction>): boolean | Promise<unknown>;
}

const compile = (
value: string,
parentSchema: object,
it: Partial<AjvContext>,
): ValidateFunction => {
const compile = (value: unknown, parentSchema: AnySchemaObject, it: SchemaObjCxt) => {
debug('compile: value: %s', value);
debug('compile: parent schema: %j', parentSchema);
const typePropertyPresent = Object.keys(parentSchema).includes('type');
Expand All @@ -76,19 +59,13 @@ const compile = (
keyword: 'dataType',
message: 'Either "dataType" or "type" can be presented in schema',
params: { dataType: value },
dataPath: '',
schemaPath: it.schemaPath ?? '',
instancePath: '',
schemaPath: it.schemaPath.str ?? '',
},
]);
}

const validate: ValidateFunctionContext = (
data: Buffer | bigint | string | number,
_dataPath?: string,
_parentData?: object,
_parentDataProperty?: string | number,
_rootData?: object,
): boolean => {
const validate: DataValidateFunction = (data: Buffer | bigint | string | number): boolean => {
if (value === 'boolean') {
return isBoolean(data);
}
Expand All @@ -104,6 +81,7 @@ const compile = (
{
keyword: 'dataType',
message: 'minLength not satisfied',
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
params: { dataType: value, minLength: parent.minLength, length },
},
];
Expand All @@ -117,6 +95,7 @@ const compile = (
{
keyword: 'dataType',
message: 'maxLength exceeded',
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
params: { dataType: value, maxLength: parent.maxLength, length },
},
];
Expand Down Expand Up @@ -147,7 +126,8 @@ const compile = (
return validate;
};

export const dataTypeKeyword = {
export const dataTypeKeyword: KeywordDefinition = {
keyword: 'dataType',
compile,
errors: 'full',
modifying: false,
Expand Down
Loading

0 comments on commit 9d25bb3

Please sign in to comment.