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"
}
- string
- options
- stringMinLength
- stringMaxLength
- options
- regex
- options
- regex
- stringMinLength
- stringMaxLength
- options
- url
- options
- stringMinLength
- stringMaxLength
- options
- function
- email
- options
- stringMinLength
- stringMaxLength
- options
- decodeHTML
- options
- stringMinLength
- stringMaxLength
- options
- encodeHTML
- options
- stringMinLength
- stringMaxLength
- options
- integer
- numberMin
- numberMax
- number
- options
- numberMin
- numberMax
- numberMaxDecimals
- numberMinDecimals
- options
- any
- object
- options
- properties
- options
- array
- options
- arrayType
- arrayMaxLength
- arrayMinLength
- options
- dict
- options
- dictType
- options
- boolean
- enum
- options
- enumValues
- options
- string1
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.