tsukiko • Docs
tsukiko / ObjectParser
Represents a parser for object values with configurable validation options.
• S extends ObjectParserConfig
The object parser configuration type.
new ObjectParser<
S
>(types
):ObjectParser
<S
>
Creates a new instance of ObjectParser.
• types: S
The configuration for the object parser.
ObjectParser
<S
>
protected
optional
defaultValue:ObjectParserInfer
<S
>
protected
isOptional:boolean
=false
protected
isStrict:boolean
=false
protected
langType:langType
=DEFAULT_LANG
protected
rules:ParserFunction
[]
check(
input
):input is ObjectParserInfer<S>
Checks if the input is valid according to the parser's rules.
• input: unknown
The input to check.
input is ObjectParserInfer<S>
True if the input is valid, false otherwise.
default(
value
):Parser
<ObjectParserInfer
<S
>>
Sets a default value for the parser.
• value: ObjectParserInfer
<S
>
The default value.
The current Parser instance.
protected
defaultHandle(input
):ObjectParserInfer
<S
>
• input: ObjectParserInfer
<S
>
describe(
description
):ObjectParser
<S
>
Sets a description for the parser.
• description: string
The description to set.
ObjectParser
<S
>
The current Parser instance.
empty():
ObjectParser
<S
>
Configures the parser to allow undefined but not null.
ObjectParser
<S
>
The current Parser instance.
protected
error(lang
,data
?):TsuError
• lang: "not_string"
| "not_a_email"
| "not_a_domain"
| "not_a_url"
| "illegal_match_string"
| "illegal_starts_with"
| "illegal_ends_with"
| "too_long"
| "too_short"
| "not_number"
| "not_integer_number"
| "not_odd_number"
| "not_even_number"
| "not_natural_number"
| "not_positive_number"
| "not_negative_number"
| "not_percentage"
| "too_bigger"
| "too_bigger_has"
| "too_smaller"
| "too_smaller_has"
| "not_multiple_number"
| "is_a_NaN"
| "not_boolean"
| "not_true"
| "not_false"
| "not_null"
| "not_undefined"
| "not_never"
| "not_an_array"
| "array_error"
| "too_many_items"
| "too_few_items"
| "not_a_tuple"
| "illegal_tuple_length"
| "tuple_error"
| "not_an_object"
| "object_is_null"
| "object_is_an_array"
| "object_not_instance_of_constructor"
| "object_keys_too_many"
| "object_keys_too_few"
| "object_error"
| "object_key_error"
| "intersection_error"
| "union_error"
| "literal_only"
| "literal_number_error"
| "literal_string_error"
| "literal_boolean_error"
| "custom_error"
| "not_a_function"
| "not_a_constructor"
| "not_an_async_function"
| "not_a_generator_function"
| "not_an_async_generator_function"
| "not_an_arrow_function"
| "function_args_count_mismatch"
| "function_name_mismatch"
| "not_a_class"
| "class_args_count_mismatch"
| "class_name_mismatch"
| "class_prototype_error"
• data?
index<
T
>(value
,key
):Parser
<Record
<string
,ParserInfer
<T
>>>
Configures the parser to handle additional properties with a specified value parser and key parser.
• T extends Parser
<unknown
>
• value: T
The parser for additional property values.
• key: StringParser
= ...
The parser for additional property keys. Defaults to a new StringParser instance.
Parser
<Record
<string
, ParserInfer
<T
>>>
A new Parser instance for the indexed object.
instance(
Constructor
):ObjectParser
<S
>
Adds a constructor to the list of allowed instance types.
• Constructor: Constructor
The constructor function to add.
ObjectParser
<S
>
The current ObjectParser instance.
lang(
value
):ObjectParser
<S
>
• value: langType
= DEFAULT_LANG
ObjectParser
<S
>
max(
value
):ObjectParser
<S
>
Sets the maximum number of properties allowed in the object.
• value: number
The maximum number of properties.
ObjectParser
<S
>
The current ObjectParser instance.
min(
value
):ObjectParser
<S
>
Sets the minimum number of properties required in the object.
• value: number
The minimum number of properties.
ObjectParser
<S
>
The current ObjectParser instance.
optional():
Parser
<undefined
|ObjectParserInfer
<S
>>
Makes the parser optional.
Parser
<undefined
| ObjectParserInfer
<S
>>
The current Parser instance.
parse(
input
):ObjectParserInfer
<S
>
Parses the input and returns the result.
• input: unknown
The input to parse.
The parsed result.
If parsing fails.
parseAsync(
input
):Promise
<unknown
>
Asynchronously parses the input.
• input: unknown
The input to parse.
Promise
<unknown
>
A promise that resolves with the parsed result or rejects with an error.
parseSafe(
input
):ParserSafeReturn
<ObjectParserInfer
<S
>>
Safely parses the input and returns a result object.
• input: unknown
The input to parse.
ParserSafeReturn
<ObjectParserInfer
<S
>>
An object containing the parsing result or error.
range(
min
,max
):ObjectParser
<S
>
Sets both the minimum and maximum number of properties allowed in the object.
• min: number
The minimum number of properties.
• max: number
The maximum number of properties.
ObjectParser
<S
>
The current ObjectParser instance.
schema(
bringSchema
):JsonSchema
Generates a JSON schema for the parser.
• bringSchema: boolean
= true
Whether to include the $schema property. Defaults to true.
The generated JSON schema.
protected
setMeta(metadata
):void
• metadata: Partial
<SchemaMetadata
>
void
strict(
isStrict
):ObjectParser
<S
>
Sets whether the parser should be strict in its object property checking.
• isStrict: boolean
= true
Whether to enable strict mode. Defaults to true.
ObjectParser
<S
>
The current ObjectParser instance.
title(
title
):ObjectParser
<S
>
Sets a title for the parser.
• title: string
The title to set.
ObjectParser
<S
>
The current Parser instance.