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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sortable-properties

v2.0.0

Published

Small set of utilities based on TypeScript decorators and class-validator that make validation of sortable properties easy.

Downloads

1

Readme

sortable-properties

Small set of utilities based on TypeScript decorators and class-validator that make validation of sortable properties easy.

Github Npm

Why

Let's say you are building REST API using TypeScript. You have domain models (+ maybe view models or more...) and you are using class-validator to validate your requests. You want API consumers to be able to sort results by strictly defined set of keys which happens to be subset of your domain model property keys. Now you need to repeat yourself and store that sortable keys in separate array and pass it to IsIn class-validator's decorator. There is much space for errors that are hard to catch and it doesn't sound maintainable.

Solution is very simple. Take advantage of TypeScript decorators and reflect-metadata API to create property decorators that mark some of properties as sortable.

Or even more simple, just use this package which does exactly that and in addition provides specialized validator to make validation of sortable properties easier.

Install

Install package with npm using

npm i sortable-properties

How to use it

Decorate model properties you want to be sortable with Sortable decorator imported from sortable-properties.

import { Sortable } from 'sortable-properties';

export class Person {
  @Sortable()
  firstName: string;

  @Sortable()
  lastName: string;

  @Sortable()
  age: number;

  imagePath: string;
}

Now, in your request model decorate property that takes care of sorting with IsSortableProperty decorator imported from sortable-properties. IsSortableProperty takes class (type, constructor...) that you want sortable properties from as first argument and validationOptions as second argument which is standard validationOptions object from class-validator.

import { IsSortableProperty } from 'sortable-properties';

export class QueryPersonsDto {
  @IsSortableProperty(Person) // equivalent to @IsIn(['firstName', 'lastName', 'age'])
  sortBy?: string;

  // other, not so relevant, properties
  // (listed only to make example more complete, not required)
  @IsIn(['asc', 'desc'])
  order: string;

  limit: number;
  offset: number;
}

You might have more complex use-case and you maybe need to mix sortable properties with some other keys. You can use getSortableProperties function imported from sortable-properties to get all properties decorated with Sortable decorator and then concatenate it with you custom keys array and use IsIn validator from class-validator. It takes class (type, constructor...) that you want sortable properties from as only argument.

import { IsIn } from 'class-validator';
import { getSortableProperties } from 'sortable-properties';

export class QueryPersonsDto {
  @IsIn([...getSortableProperties(Person), 'customKey1', 'customKey2'])
  sortBy?: string;
}