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

Commit

Permalink
Added validators docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dgarcia360 committed Dec 27, 2019
1 parent 9da425f commit 678c821
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/validators/address.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {Address, NamespaceId} from 'nem2-sdk';

/**
* Address validator
*/
export class AddressValidator implements Validator<string> {

/**
* Validates if an address object can be created from a string.
* @param {string} value - Raw address.
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: string, context: ValidationContext): void {
try {
Address.createFromRawAddress(value);
Expand All @@ -28,7 +38,17 @@ export class AddressValidator implements Validator<string> {
}
}

/**
* Address alias validator
*/
export class AddressAliasValidator implements Validator<string> {

/**
* Validates if an address object can be created from a string.
* @param {string} value - Raw address. If starts with '@', then it is an alias.
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: string, context: ValidationContext): void {
const aliasTag = '@';
if (value.charAt(0) !== aliasTag) {
Expand Down
10 changes: 10 additions & 0 deletions src/validators/binary.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
*/
import {ExpectedError, ValidationContext, Validator} from 'clime';

/**
* Binary validator
*/
export class BinaryValidator implements Validator<number> {

/**
* Validates if value is 0 or 1.
* @param {number} value .
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: number, context: ValidationContext): void {
if (value !== 0 && value !== 1) {
throw new ExpectedError('The value must be 0 or 1.');
Expand Down
10 changes: 10 additions & 0 deletions src/validators/block.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {UInt64} from 'nem2-sdk';

/**
* Height validator
*/
export class HeightValidator implements Validator<string> {

/**
* Validates if height value is bigger than 0.
* @param {string} value - Height.
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: string, context: ValidationContext): void {
let valid = true;
if (value === '0') {
Expand Down
10 changes: 10 additions & 0 deletions src/validators/hashAlgorithm.validator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {HashType} from 'nem2-sdk';

/**
* Hash algorithm validator
*/
export class HashAlgorithmValidator implements Validator<number> {

/**
* Validates if the hash algorithm is available.
* @param {string} value - Hash algorithm code.
* @param {ValidationContext} context
* @throws {ExpectedError}
*/
validate(value: number, context: ValidationContext): void {
if (!(value in HashType)) {
throw new ExpectedError('hashAlgorithm must be one of ' +
Expand Down
14 changes: 14 additions & 0 deletions src/validators/mosaic.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ import {ValidationContext, Validator} from 'clime';
import {MosaicService} from '../service/mosaic.service';

export class MosaicValidator implements Validator<string> {

/**
* Validates if a mosaic object can be created from a string.
* @param {String} value - Mosaic in the form mosaicId::amount.
* @param {ValidationContext} context
* @throws ExpectedError if the mosaic object is not valid.
*/
validate(value: string, context: ValidationContext): void {
MosaicService.validate(value);
}
}

export class MosaicsValidator implements Validator<string> {

/**
* Validates if an array of mosaic objects can be created from a string.
* @param {String} value - Mosaics in the form mosaicId::amount, separated by commas.
* @param {ValidationContext} context
* @throws ExpectedError if one of the mosaic objects is not valid.
*/
validate(value: string, context: ValidationContext): void {
const mosaics = value.split(',');
mosaics.forEach((mosaic) => {
Expand Down
26 changes: 23 additions & 3 deletions src/validators/mosaicId.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
*
*/
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {Mosaic, MosaicId, NamespaceId, UInt64} from 'nem2-sdk';
import {MosaicId, NamespaceId} from 'nem2-sdk';

/**
* Mosaic id validator
*/
export class MosaicIdValidator implements Validator<string> {

/**
* Validates if a mosaic id object can be created from a string.
* @param {String} value - MosaicId in hexadecimal.
* @param {ValidationContext} context
* @throws ExpectedError if the mosaic id is not valid.
*/
validate(value: string, context: ValidationContext): void {
try {
const ignored = new MosaicId(value);
Expand All @@ -28,19 +38,29 @@ export class MosaicIdValidator implements Validator<string> {
}
}

/**
* Mosaic id alias validator
*/
export class MosaicIdAliasValidator implements Validator<string> {

/**
* Validates if a mosaic id object can be created from a string.
* @param {String} value - MosaicId in hexadecimal or Namespace name. If starts with '@', it is a namespace name.
* @param {ValidationContext} context
* @throws ExpectedError if the mosaic id is not valid.
*/
validate(value: string, context: ValidationContext): void {
const aliasTag = '@';
if (value.charAt(0) !== aliasTag) {
try {
const mosaic = new MosaicId(value);
const ignored = new MosaicId(value);
} catch (err) {
throw new ExpectedError('Enter a mosaic id in hexadecimal format. Example: 941299B2B7E1291C');
}
} else {
const alias = value.substring(1);
try {
const mosaic = new NamespaceId(alias);
const ignored = new NamespaceId(alias);
} catch (err) {
throw new ExpectedError('Enter valid mosaic alias. Example: @nem.xem');
}
Expand Down
10 changes: 10 additions & 0 deletions src/validators/namespaceId.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {NamespaceId, UInt64} from 'nem2-sdk';

/**
* Namespace id validator
*/
export class NamespaceIdValidator implements Validator<string> {

/**
* Validates a namespace id.
* @param {String} value - NamespaceId in hexadecimal.
* @param {ValidationContext} context
* @throws ExpectedError if the namespace id is not valid.
*/
validate(value: string, context: ValidationContext): void {
try {
const namespaceIdUInt64 = UInt64.fromHex(value);
Expand Down
10 changes: 10 additions & 0 deletions src/validators/network.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {NetworkType} from 'nem2-sdk';

/**
* Network validator
*/
export class NetworkValidator implements Validator<string> {

/**
* Validates if a network is supported.
* @param {String} value - Network type friendly name.
* @param {ValidationContext} context
* @throws ExpectedError if the network is not valid.
*/
validate(value: string, context?: ValidationContext): void {
if (!(value in NetworkType)) {
throw new ExpectedError('Enter a valid network type');
Expand Down
10 changes: 10 additions & 0 deletions src/validators/numericString.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {UInt64} from 'nem2-sdk';

/**
* Numeric string
*/
export class NumericStringValidator implements Validator<string> {

/**
* Validates if a string is composed by numbers.
* @param {String} value - Numeric string.
* @param {ValidationContext} context
* @throws ExpectedError if string is not a number.
*/
validate(value: string, context: ValidationContext): void {
try {
UInt64.fromNumericString(value);
Expand Down
10 changes: 10 additions & 0 deletions src/validators/password.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {Password} from 'nem2-sdk';

/**
* Password validator
*/
export class PasswordValidator implements Validator<string> {

/**
* Validates if a password has at least 8 chars.
* @param {String} value - Password.
* @param {ValidationContext} context
* @throws ExpectedError if string is has less than 8 chars.
*/
validate(value: string, context?: ValidationContext): void {
try {
const ignored = new Password(value);
Expand Down
10 changes: 10 additions & 0 deletions src/validators/privateKey.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
*/
import {ExpectedError, ValidationContext, Validator} from 'clime';

/**
* Private key validator
*/
export class PrivateKeyValidator implements Validator<string> {

/**
* Validates a private key format.
* @param {String} value - Private key.
* @param {ValidationContext} context
* @throws ExpectedError if value is not a valid private key.
*/
validate(value: string, context: ValidationContext): void {
if (value.length !== 64 || !/^[0-9a-fA-F]+$/.test(value)) {
throw new ExpectedError('private key should be a 64 characters hexadecimal string');
Expand Down
20 changes: 20 additions & 0 deletions src/validators/publicKey.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@
*/
import {ExpectedError, ValidationContext, Validator} from 'clime';

/**
* Private key validator
*/
export class PublicKeyValidator implements Validator<string> {

/**
* Validates a public key format.
* @param {String} value - Public key.
* @param {ValidationContext} context
* @throws ExpectedError if value is not a valid public key.
*/
validate(value: string, context: ValidationContext): void {
if (value.length !== 64 || !/^[0-9a-fA-F]+$/.test(value)) {
throw new ExpectedError('public key should be a 64 characters hexadecimal string');
}
}
}

/**
* Private keys validator
*/
export class PublicKeysValidator implements Validator<string> {

/**
* Validates multiple public key format.
* @param {String} value - Public keys, separated by a comma.
* @param {ValidationContext} context
* @throws ExpectedError if value contains an invalid public key.
*/
validate(value: string, context: ValidationContext): void {
const publicKeys = value.split(',');
publicKeys.map((publicKey: string) => {
Expand Down
22 changes: 21 additions & 1 deletion src/validators/restrictionType.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@
* limitations under the License.
*
*/
import { ExpectedError, ValidationContext, Validator } from 'clime';
import {ExpectedError, ValidationContext, Validator} from 'clime';

/**
* Account restriction direction validator
*/
export class AccountRestrictionDirectionValidator implements Validator<string> {

/**
* Validates if direction is outgoing or incoming.
* @param {String} value - Direction (Incoming or outgoing).
* @param {ValidationContext} context
* @throws ExpectedError if value is not a valid direction.
*/
validate(value: string, context: ValidationContext): void {
value = value.toLowerCase();
if ('incoming' !== value && 'outgoing' !== value) {
Expand All @@ -26,7 +36,17 @@ export class AccountRestrictionDirectionValidator implements Validator<string> {
}
}

/**
* Account restriction type validator
*/
export class AccountRestrictionTypeValidator implements Validator<string> {

/**
* Validates if restriction type is allow or block.
* @param {String} value - Restriction type (allow or block).
* @param {ValidationContext} context
* @throws ExpectedError if value is not a valid restriction type.
*/
validate(value: string, context: ValidationContext): void {
value = value.toLowerCase();
if ('allow' !== value && 'block' !== value) {
Expand Down
10 changes: 10 additions & 0 deletions src/validators/transactionType.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
import {ExpectedError, ValidationContext, Validator} from 'clime';
import {TransactionType} from 'nem2-sdk';

/**
* Transaction type validator
*/
export class TransactionTypeValidator implements Validator<string> {

/**
* Validates if transaction type is known.
* @param {String} value - Transaction type.
* @param {ValidationContext} context
* @throws ExpectedError if value is not a known transaction type.
*/
validate(value: string, context: ValidationContext): void {
let success = true;
try {
Expand Down

0 comments on commit 678c821

Please sign in to comment.