validate and parse objects in node.
npm install @miqro/parservalidate and parse objects in node.
``npm install @miqro/parser`
to use in a browser bundle the module with something like webpack or esbuild.
`typescript
import {Parser, Schema} from "@miqro/parser";
interface MyData {
id: number;
name: string;
}
const MyDataSchema: Schema
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
- 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
`typescript`
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.