2.0.4 • Published 4 months ago

@miqro/parser v2.0.4

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

@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.

2.0.4

4 months ago

2.0.3

9 months ago

2.0.2

12 months ago

1.0.2

1 year ago

1.0.3

1 year ago

2.0.1

1 year ago

1.0.1

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.3

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago