Skip to content

Commit

Permalink
doc: Parser, not validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Jun 19, 2020
1 parent bf418d6 commit 38bf7b6
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions doc/Parser-not-validator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Parser, not validator

While this package provides API for generating both parsers and validators, parser should be
preferred if that fits your use case.

## Why?

Using the parser API ensures that the object is never present in your code in parsed but not
validated form, significantly reducing the number of mistakes that can come from using unvalidated
raw objects.

Apart from that, it allows some checks in the validation to be fast-tracked for clarity and
performance if the input is known to be from `JSON.parse` and can not contain, e.g. `undefined`
variables.
That fast-tracking could be also enabled via the `isJSON` option of the validator, but it should be
used only when the input is _definitely_ coming from a `JSON.parse` without a reviver.

## Differences between parser and validator

`validator(schema, options)` creates a `validate(object)` function which accepts input after it has
been parsed from JSON. It will return either `true` or `false` and errors would be reported as a
property of `validate` function: `validate.errors` (if `includeErrors` option is enabled).

`parser(schema, options)` creates a similar `parse(string)` function, which accepts a raw string
containing JSON-encoded input, with a few differences:

1. If validation succeeds, it returns the parsed object itself, like `JSON.parse(string)` does.

2. If validation fails, it will throw instead of returning `false`.

3. Validation errors (if enabled) are available as the `err.errors` of the thrown `err` object.

4. Parser operates in [strong mode](./Strong-mode.md) by default. To override it, use
`mode: 'default'` option.

Parsers can be likewise exported to js code by using `parser.toModule()`.

0 comments on commit 38bf7b6

Please sign in to comment.