ExodusMovement / schemasafe

A reasonably safe JSON Schema validator with draft-04/06/07/2019-09/2020-12 support.
https://npmjs.com/@exodus/schemasafe
MIT License
161 stars 12 forks source link

`opts.includeErrors` does nothing in default case for `validator()` #168

Closed jason-curtis closed 11 months ago

jason-curtis commented 12 months ago

According to the README, this should create a validator that returns errors: const validate = validator(schema, { includeErrors: true })

Instead, that validator only returns true or false.

Failing Jest test case:

import { validator, ParseResult } from '@exodus/schemasafe';

describe('validate', () => {
    it('validates with errors', () => {
        const schema = {
            type: 'object',
            required: ['hello'],
            properties: {
                hello: {
                    type: 'string'
                }
            }
        }
        const validate = validator(schema, { includeErrors: true })
        expect(validate({ hello: 100 })).toEqual({ valid: true, errors: [{ keywordLocation: '#/properties/hello/type', instanceLocation: '#/hello' }] })
    })
});

error returned:

    expect(received).toEqual(expected) // deep equality

    Expected: {"errors": [{"instanceLocation": "#/hello", "keywordLocation": "#/properties/hello/type"}], "valid": true}
    Received: false
ChALkeR commented 11 months ago

The example at the linked section of the README works.

Validator mode outputs errors on a separate property, not via returning them, to ensure that a simple if (validate(x)) mistake is never made by the consumer (as it might have caused a security vuln if validate return value wasn't a simple bool).

See Error handling for more documentation.