1.0.0-beta.30 • Published 4 years ago

@iota/validators v1.0.0-beta.30

Weekly downloads
2,016
License
MIT
Repository
github
Last release
4 years ago

@iota/validators

Collection of guards and validators, useful in IOTA development.

Installation

Instal using npm:

npm install @iota/validators

or using yarn:

yarn add @iota/validators

API Reference

validators.isAddress(address)

Summary: Validates the checksum of the given address.

ParamTypeDescription
addressstringAddress with a checksum

This method takes an address with a checksum and validates that the checksum is correct.

Related methods

To generate a new address with a checksum, use the getNewAddress() method.

Returns: boolean - valid - Whether the checksum is valid
Example

let valid = Validator.isAddress('9FNJWLMBECSQDKHQAGDHDPXBMZFMQIMAFAUIQTDECJVGKJBKHLEBVU9TWCTPRJGYORFDSYENIQKBVSYKW9NSLGS9UW');

validators~isTrits(input)

ParamType
inputany

Checks if input is an Int8Array of trit values; -1, 0, 1.

validators~isNullTrits(trits)

ParamType
tritsInt8Array

Checks if trits are NULL.

validators~isTrytes(trytes, length)

ParamTypeDefault
trytesstring
lengthstring | number"'1,'"

Checks if input is correct trytes consisting of 9A-Z; optionally validate length

validators~isTrytesOfExactLength(trytes, length)

ParamType
trytesstring
lengthnumber

validators~isTrytesOfMaxLength(trytes, length)

ParamType
trytesstring
lengthnumber

validators~isEmpty(hash)

ParamType
hashstring

Checks if input contains 9s only.

validators~isHash(hash)

ParamType
hashstring

Checks if input is correct hash (81 trytes) or address with checksum (90 trytes)

validators~isInput(address)

ParamType
addressstring

Checks if input is valid input object. Address can be passed with or without checksum. It does not validate the checksum.

validators~isTag(tag)

ParamType
tagstring

Checks that input is valid tag trytes.

validators~isTransfer(transfer)

ParamType
transferTransfer

Checks if input is valid transfer object.

validators~isUri(uri)

ParamType
uristring

Checks that a given URI is valid

Valid Examples:

  • udp://[2001:db8:a0b:12f0::1]:14265
  • udp://[2001:db8:a0b:12f0::1]
  • udp://8.8.8.8:14265
  • udp://domain.com
  • udp://domain2.com:14265

validators~validate()

Throws:

  • Error error

Runs each validator in sequence, and throws on the first occurence of invalid data. Validators are passed as arguments and executed in given order. You might want place validate() in promise chains before operations that require valid inputs, taking advantage of built-in promise branching.

Example

try {
  validate([
    value, // Given value
    isTrytes, // Validator function
    'Invalid trytes' // Error message
  ])
} catch (err) {
  console.log(err.message) // 'Invalid trytes'
}
1.0.0-beta.30

4 years ago

1.0.0-beta.23

4 years ago

1.0.0-beta.21

5 years ago

1.0.0-beta.19

5 years ago

1.0.0-beta.16

5 years ago

1.0.0-beta.15

5 years ago

1.0.0-beta.12

5 years ago

1.0.0-beta.11

5 years ago

1.0.0-beta.10

5 years ago

1.0.0-beta.9

5 years ago

1.0.0-beta.8

5 years ago

1.0.0-beta.7

5 years ago

1.0.0-beta.6

5 years ago

1.0.0-beta.5

6 years ago

1.0.0-beta.4

6 years ago

1.0.0-beta.3

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-alpha.1

6 years ago