Open epoberezkin opened 8 years ago
Hi,
first of all, thanks for the work you've done. This lib is great :D
Is this feature still in your roadmap?
With ajv@6.1.1, when an async custom format resolves whit false, the error contains the dataPath:
ajv.addFormat('custom', {
async: true,
type: 'string',
validate: data => Promise.resolve(false)
});
const schema = {
$async: true,
properties: {
foo: { type: 'string', format: 'custom' }
}
};
ajv.compile(schema)({ foo: 'a' }); // => data.foo should match format "custom"
The validate method does not receive the dataPath, so when it rejects with a ValidationError, the dataPath is lost:
ajv.addFormat('custom', {
async: true,
type: 'string',
validate: data => Promise.reject( new Ajv.ValidationError([{ message: 'oops' }]) )
});
const schema = { foo: { type: 'string', format: 'custom' } };
ajv.compile(schema)({ foo: 'a' }); // => dataundefined oops
Any chance of making a PR on this?
I may be missing something but it seems to me that the problem you're describing is unrelated to this issue? Also, the schema should have $async: true
in it...
You're right, $async: true
is missing in my example, but adding it does not solve the problem.
My example shows a custom format custom
rejecting with a custom error oops
. So it's related to to this issue, I think. Or maybe I misunderstand what "custom errors" stands for :(
This issue is about supporting custom errors in synchronous custom formats. Currently it is not possible at all (and nobody ever asked for it).
You are describing a different problem here: some missing information in the custom error in async format. Please submit a separate issue.
I want the feature that support custom errors in custom formats,especially when custom format function,only the error "should match format ***",can't explain more
Synchronous formats should assign to errors property of format validation function
Assigning to a function seems so weird to me and it may be prone to race conditions too.
Why not let the format function throw/reject in both sync and async cases?
I want the feature that support custom errors in custom formats,especially when custom format function,only the error "should match format ***",can't explain more
Agreed
any news?
Synchronous formats should assign to
errors
property of format validation function, asynchronous formats should throw exception (See #118)