npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@itrocks/length

v0.0.3

Published

Decorators @Length, @MinLength, @MaxLength to enforce fixed or flexible string length limits on class properties

Readme

npm version npm downloads GitHub issues discord

length

Decorators @Length, @MinLength, @MaxLength to enforce fixed or flexible string length limits on class properties.

This documentation was written by an artificial intelligence and may contain errors or approximations. It has not yet been fully reviewed by a human. If anything seems unclear or incomplete, please feel free to contact the author of this package.

Installation

npm i @itrocks/length

Usage

@itrocks/length provides three property decorators:

  • @Length(length) – enforces an exact length
  • @MinLength(length) – enforces a minimum length
  • @MaxLength(length) – enforces a maximum length

and a set of helper functions to read the configured limits:

  • lengthOf(target, property)
  • minLengthOf(target, property)
  • maxLengthOf(target, property)

The decorators themselves do not perform validation. Instead, they store metadata that other parts of your application (or other @itrocks/* packages) can use to validate values, generate schemas, or build forms.

Minimal example

import { Length } from '@itrocks/length'

class Code
{
	// Always exactly 6 characters
	@Length(6)
	value = ''
}

Here, a generic validator can read the Length decorator metadata and ensure that value is always exactly 6 characters long.

Complete example with flexible limits

This example combines MinLength and MaxLength to define a user-friendly constraint on a string property, and then uses the *Of helper functions to read the configuration:

import type { ObjectOrType }                 from '@itrocks/class-type'
import { Length, MaxLength, MinLength,
	lengthOf, maxLengthOf, minLengthOf }       from '@itrocks/length'

class User
{
	// Username must be between 3 and 20 characters
	@MinLength(3)
	@MaxLength(20)
	username = ''

	// Activation code must be exactly 8 characters
	@Length(8)
	activationCode = ''
}

function getStringLengthConstraints<T extends object>(
	type: ObjectOrType<T>,
	property: keyof T,
)
{
	return {
		length   : lengthOf(type, property),
		minLength: minLengthOf(type, property),
		maxLength: maxLengthOf(type, property),
	}
}

// Example usage
const usernameConstraints = getStringLengthConstraints(User, 'username')
// => { length: undefined, minLength: 3, maxLength: 20 }

const codeConstraints = getStringLengthConstraints(User, 'activationCode')
// => { length: 8, minLength: 0, maxLength: undefined }

In practice, you will typically let other @itrocks/* packages (like schema builders or form generators) consume these decorators instead of calling the helper functions directly.

API

function Length<T extends object>(length?: number): DecorateCaller<T>

Property decorator indicating that a field must have an exact string length.

Parameters

  • length (optional) – required length of the string. If omitted, the length constraint is left undefined and can be interpreted by your own tooling. In most situations, you will pass a positive integer.

Return value

  • DecorateCaller<T> – function from @itrocks/decorator/property used internally when the decorator is applied by TypeScript. You normally do not call it directly.

Example

class Product
{
	@Length(13)
	barcode = ''
}

function lengthOf<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): number | undefined

Reads the exact length configured with @Length() for a given property.

Parameters

  • target – the class (User) or instance (new User()) that owns the property.
  • property – name of the property to inspect.

Return value

  • number | undefined – the configured exact length, or undefined if no @Length() decorator is present on that property.

function MinLength<T extends object>(length?: number): DecorateCaller<T>

Property decorator indicating that a field must have at least the given string length.

Parameters

  • length (optional, default: 0) – minimum allowed length. If not provided, the minimum length is considered to be 0.

Return value

  • DecorateCaller<T> – function from @itrocks/decorator/property used internally when the decorator is applied.

Example

class Comment
{
	// At least 10 characters
	@MinLength(10)
	body = ''
}

function minLengthOf<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): number

Reads the minimum length configured with @MinLength() for a given property.

Parameters

  • target – the class or instance that owns the property.
  • property – name of the property to inspect.

Return value

  • number – the minimum length defined on the property. If no @MinLength() decorator is present, this function returns 0.

function MaxLength<T extends object>(length?: number): DecorateCaller<T>

Property decorator indicating that a field must not exceed the given string length.

Parameters

  • length (optional) – maximum allowed length. If omitted, the constraint is left undefined and can be interpreted by your tooling.

Return value

  • DecorateCaller<T> – function from @itrocks/decorator/property used internally when the decorator is applied.

Example

class Message
{
	// No more than 280 characters
	@MaxLength(280)
	body = ''
}

function maxLengthOf<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): number | undefined

Reads the maximum length configured with @MaxLength() for a given property.

Parameters

  • target – the class or instance that owns the property.
  • property – name of the property to inspect.

Return value

  • number | undefined – the maximum length defined on the property, or undefined if no @MaxLength() decorator is present.

Typical use cases

  • Define exact-length identifiers such as activation codes, barcodes, or short hashes using @Length().
  • Enforce user-facing rules on text fields, like minimum and maximum length for usernames, passwords, comments, or messages.
  • Drive generic validation logic that checks string length constraints based on decorator metadata instead of hard-coding them.
  • Generate JSON Schemas or other schema formats that include minLength, maxLength, or exact-length constraints.
  • Configure length limits once on your domain model and reuse them across back-end validation, front-end forms, and documentation.