@muze-nl/assert
v0.6.0
Published
light optional assert library
Readme
Assert: optional assertion checking
import { assert, enable, Optional, Required, oneOf, validURL } from '@muze-nl/assert/core'
enable()
function registerClient(metadata) {
assert(metadata, {
redirect_uris: Required([validURL]),
application_type: Optional(oneOf('web', 'native')),
client_name: Optional(String)
})
// continue with metadata known to match the expected shape
}Table of Contents
Introduction
Assert is a lightweight library for optional runtime checks. It is meant for code that benefits from explicit developer feedback during development, but should not spend time validating assumptions in production unless you ask it to.
Assertions are disabled by default. assert() returns immediately until you call enable(). When enabled, failed assertions throw an error with path-aware details. If you always want to validate and handle failures yourself, use fails() or issues() directly.
This style is useful for design-by-contract checks, protocol implementations, middleware preconditions, mock servers, and other places where executable requirements make code easier to understand.
Usage
Install with npm:
npm install @muze-nl/assertUse the side-effect-free entry point when you want tree-shaking:
import { assert, enable, Required, validURL } from '@muze-nl/assert/core'The package root exports the same API and also assigns it to globalThis.assert for compatibility:
import * as assert from '@muze-nl/assert'In the browser, using a CDN:
<script src="https://cdn.jsdelivr.net/npm/@muze-nl/assert/dist/assert.min.js" crossorigin="anonymous"></script>This loads the API as window.assert.
Documentation
- Documentation index
- Tutorial
- Reference
- Creating custom assertion checks
- fails(), issues(), and formatIssues()
License
This software is licensed under the MIT open source license. See the License file.
