2.0.4 • Published 3 months ago
@miqro/parser v2.0.4
@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
- 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
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
3 months ago
2.0.3
8 months ago
2.0.2
11 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
2 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