The spectral theorem is that, if $A: D(A) \to {\cal H}$ is a selfadjoint operator, where $D(A) \subset {\cal H}$ is a dense subspace, then there exists a unique projector-valued measure $P^{(A)}$ on the Borel sets of $\mathbb{R}$
such that $$A = \int_{\mathbb R} \lambda dP^{(A)}(\lambda)\:.$$
As a consequence (this is a corollary or a definition depending on the procedure)
$$f(A) = \int_{\mathbb R} f(\lambda) dP^{(A)}(\lambda) \tag{1}$$
for every $f: {\mathbb R} \to {\mathbb C}$ Borel measurable. Taking $f(x) =1$ for all $x\in {\mathbb R}$ we have
$$I = \int_{\mathbb R} dP^{(A)}(\lambda)\:.$$
For selfadjoint operators admitting a Hilbert basis of eingenvectors $\psi_{\lambda, d_\lambda}$, $\lambda \in \sigma_p(A)$ and $d_\lambda$ accounting for the dimension of the eigenspace with eigenvalue $\lambda$, the identity above reads (referring to the strong operator-topology)
$$f(A) = \sum_{\lambda, d_\lambda} f(\lambda) |\psi_{\lambda, d_\lambda}\rangle\langle \psi_{\lambda, d_\lambda} |\:, \tag{2}$$
with the special case
$$I = \sum_{\lambda, d_\lambda} |\psi_{\lambda, d_\lambda}\rangle\langle \psi_{\lambda, d_\lambda} |\:. \tag{3}$$
In summary Eqs.(1) and (2) are the central identities, Eq.(3) is just a special case.
Given an orthonormal complete basis $\{\psi_n\}_{n \in \mathbb N} \subset {\cal H}$, one can always define ad hoc a selfadjoint operator $A$ (with no physical meaning in general) to implement the identities above:
$$A = \sum_{n \in \mathbb{N}} \lambda_n |\psi_{n}\rangle\langle \psi_{n} |$$ for a given arbitrary choice of real numbers $\lambda_n$.
The domain of $A$ is
$$\left\{\psi \in {\cal H} \: \left| \: \sum_{n} |\lambda_n|^2 |\langle \psi_n| \psi \rangle|^2 < +\infty\right. \right\}$$