0.1.4 • Published 1 year ago

iso-639-1-ts v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

ISO-639-1

Installation

npm install iso-639-1-ts
yarn add iso-639-1-ts
pnpm add iso-639-1-ts

Examples

Express

import { Request, Response } from "express";
import { ISO6391 } from "iso-639-1-ts";

app.get("/language/:code", (req: Request, res: Response) => {
  const code = req.params.code;

  if (ISO6391.isValid(code)) {
    res.send(ISO6391.getLocalName(code));
  } else {
    res.status(400).send("Invalid language code.");
  }
});

NestJS

import { registerDecorator, ValidationOptions } from "class-validator";
import { ISO6391 } from "iso-639-1-ts";

export const IsISO6391LanguageCode = (
  validationOptions?: ValidationOptions
) => {
  return (object: any, propertyName: string) => {
    registerDecorator({
      name: "isISO6391LanguageCode",
      target: object.constructor,
      propertyName: propertyName,
      constraints: [],
      options: validationOptions,
      validator: {
        validate(value: unknown) {
          if (typeof value !== "string") {
            return false;
          }

          return ISO6391.isValid(value);
        },
        defaultMessage(): string {
          return "$property must be a valid ISO 639-1 language code.";
        },
      },
    });
  };
};
0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.0

1 year ago