@miqro/parser
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

@miqro/parser

validate and parse objects in node.

npm install @miqro/parser

to use in a browser bundle the module with something like webpack or esbuild.

import {Parser, Schema} from "@miqro/parser";

interface MyData {
  id: number;
  name: string;
}

const MyDataSchema: Schema<MyData> = {
  type: "object",
  properties: {
    id: "number",
    name: "string"
  }
}

const parser = new Parser();

const parsed = parser.parse({
  id: "1",
  name: "some name"
}, MyDataSchema);

if (parsed) {
  console.log(typeof parsed.id) // "number"
}

built-in parsers

  • string
    • options
      • stringMinLength
      • stringMaxLength
  • regex
    • options
      • regex
      • stringMinLength
      • stringMaxLength
  • url
    • options
      • stringMinLength
      • stringMaxLength
  • function
  • email
    • options
      • stringMinLength
      • stringMaxLength
  • decodeHTML
    • options
      • stringMinLength
      • stringMaxLength
  • encodeHTML
    • options
      • stringMinLength
      • stringMaxLength
  • integer
    • numberMin
    • numberMax
  • number
    • options
      • numberMin
      • numberMax
      • numberMaxDecimals
      • numberMinDecimals
  • any
  • object
    • options
      • properties
  • array
    • options
      • arrayType
      • arrayMaxLength
      • arrayMinLength
  • dict
    • options
      • dictType
  • boolean
  • enum
    • options
      • enumValues
  • string1

operators or ( | ), array ( [] ), forceArray ( []! ) and optional ( ? )

const parsed = parser.parse({
  forceArray: "123",
  attr: "123",
  attr2: "true",
  attr3: "text"
}, {
  optional: "string?",
  forceArray: "number[]!?",
  attr: "boolean|number|string",
  attr2: "boolean|number|string",
  attr3: "boolean|number|string",
});

Important Notice

internally array ( [] ) and forceArray ( []! ) are aliases, so when calling this.registerParser("custom", ...) only custom[] and custom[]! will be defined.

Readme

Keywords

none

Package Sidebar

Install

npm i @miqro/parser

Weekly Downloads

0

Version

2.0.4

License

ISC

Unpacked Size

75.7 kB

Total Files

10

Last publish

Collaborators

  • claukers