liveBook · Manning

isValid

As explained in The Joy of JavaScript

A book-local function that returns a validation result indicating whether an element satisfies the required checks.

isValid is an object-local validation hook: it applies the business rules for one element and reports whether that element satisfies them. In composable validation, its natural result is a Validation object rather than an unexplained Boolean, so failures can carry precise error information. section 5.5.4 section 5.5.7

A block-level implementation validates several independent conditions:

  • Look up the previous block through previousHash and obtain its index and timestamp. section 5.5.4
  • Check that the new timestamp follows the previous timestamp and that the index increases. section 5.5.4
  • Check for tampering, then combine the individual results; the shown Block method returns true only when every check succeeds. section 5.5.4

The same hook supports recursive validation of a complete blockchain. A HasValidation mixin enumerates an object, starts with its own isValid result, and recursively validates each child, combining results with flatMap. This lets the same interface validate a blockchain, its blocks, and their transactions. section 5.5.7

Validating a newly added block

After creating a block with the ledger height and top hash, pushing it onto the ledger, and calling block.isValid(), the result is true. Changing block.data afterward makes the tampering check fail. section 5.5.4

Figure 5.7 Traversing a blockchain’s objects, starting with the Blockchain object itself and moving down to each block’s transactions. You can think of the routine as forming a treelike structure, with the Blockchain object itself as the root, blocks as the second-level nodes, and transactions as the leaves of the tree.

Follow the tree-shaped traversal from the Blockchain root through blocks to transaction leaves. section 5.5.7

How The Joy of JavaScript covers it

How deep it goes
Descends from individual timestamp, index, and tampering checks to recursive aggregation, while deferring detailed Validation internals.
Examples
Uses Block checks for previous links, timestamps, indexes, tampering, and nested transaction validation.
What it assumes
Assumes familiarity with curried functions, object composition, and iterable JavaScript objects while introducing recursive validation structure.
What you can do after
Build object-specific validation hooks and compose them to validate an entire nested blockchain with failure propagation.
How it connects
Connects Validation objects, blockchain structures, mixins, recursion, currying, and composable interfaces.

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.