Files
status-react/src/schema
Icaro Motta c1dcd7a764 Introduce malli library (#17867)
This commit is the foundational step to start using malli
(https://github.com/metosin/malli) in this project.

Take in consideration we will only be able to realize malli's full power in
future iterations.

For those without context: the mobile team watched a presentation about malli
and went through a light RFC to put everyone on the same page, among other
discussions here and there in PRs.

To keep things relatively short:

1. Unit, integration and component tests will short-circuit (fail) when
   inputs/outputs don't conform to their respective function schemas (CI should
   fail too).
2. Failed schema checks will not block the app from initializing, nor throw an
   exception that would trigger the LogBox. Exceptions are only thrown in the
   scope of automated tests.
3. There's zero performance impact in production code because we only
   instrument. Instrumentation is removed from the compiled code due to the
   usage of "^boolean js.goog/DEBUG".
4. We shouldn't expect any meaningful slowdown during development.

**What are we instrumenting in this PR?**

Per our team's agreement, we're only instrumenting the bare minimum to showcase 2 examples.

- Instrument a utility function utils.money/format-amount using the macro
  approach.
- Instrument a quo component quo.components.counter.step.view/view using the
  functional approach.

Both approaches are useful, the functional approach is powerful and allow us to
instrument anonymous functions, like the ones we pass to subscriptions or event
handlers, or the higher-order function quo.theme/with-theme. The macro approach
is perfect for functions already defined with defn.

**I evaluated the schema or function in the REPL but nothing changes**

- If you evaluate the source function, you need to evaluate schema/=> or
  schema/instrument as well.
- Remember to *var quote* when using schema/instrument.
- You must call "(status-im2.setup.schema/setup!)" after any var is
  re-instrumented. It's advisable to add a keybinding in your editor to send
  this expression automatically to the CLJS REPL, or add the call at the end of
  the namespace you are working on (similar to how some devs add "(run-tests)"
  at the end of test namespaces).

**Where should schemas be defined?**

For the moment, we should focus on instrumenting quo components, so define each
function schema in the same namespace as the component's public "view" var.

To be specific:

- A schema used only to instrument a single function and not used elsewhere,
  like a quo component schema, wouldn't benefit from being defined in a separate
  namespace because that would force the developer to constantly open two files
  instead of one to check function signatures.
- A common schema reused across the repo, like ":schema.common/theme" should be
  registered in the global registry "schema.registry" so that consumers can just
  refer to it by keyword, as if it was a built-in malli schema.
- A common schema describing status-go entities like message, notification,
  community, etc can be stored either in the respective
  "src/status_im2/contexts/*" or registered globally, or even somewhere else.
  This is yet to be defined, but since I chose not to include schemas for them,
  we can postpone this guideline.
2023-11-18 11:04:48 -03:00
..
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00
2023-11-18 11:04:48 -03:00

Schemas

This document will grow to describe how we use Malli in the project and our conventions. It's still early days 🐪

Guidelines

Use var quote #' when aliasing instrumented vars

It is common in this repository to have aliases to vars. For example, view referring to var-internal, or quo.core/button referring to quo.components.buttons.button.view.

If the original var being aliased is instrumented, the alias var MUST var quote the original var. If you don't do this, the aliased var will not be instrumented.

;; bad, view-internal is instrumented, but both aliases don't use a var quote.
(schema.core/=> view-internal ?schema)
(def view (quo.theme/with-theme view-internal))
(def button quo.components.buttons.button.view/button)

;; good
(schema.core/=> view-internal ?schema)
(def view (quo.theme/with-theme #'view-internal))
(def button #'quo.components.buttons.button.view/button)

Prefix schema references with ?

Prefix schema bindings and vars with a question mark ?. This is the naming convention used by malli itself when functions receive instances of schemas and it's an unambiguous way to avoid naming clashes.

;; bad
(def message-type [:enum ...])

(defn view
  [message-type] ; Shadows `message-type` schema
  (do-something message-type))

;; good
(def ?message-type [:enum ...])

(defn view
  [message-type] ; Unambiguous naming strategy
  (do-something message-type))

Define schemas as functions when needed

Malli has many utility functions to manipulate schemas as data, and they will automatically check if the schemas were already defined in the registry.

For schemas we want to conveniently access from the global registry, like :schema.common/theme, they must be registered before Malli tries to use them.

;; bad, will fail if :schema.common/bar is not registered.
(def ^:private ?foo
  (malli.util/select-keys :schema.common/bar [:id :name]))

;; good, execution will be delayed until the schema ?foo is correctly registered.
(defn- ?foo
  []
  (malli.util/select-keys :schema.common/bar [:id :name]))