New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow extending for arbitrary keywords #134
Comments
Do you have some JSON examples so far? |
For the schema or data? Not sure what you're after, sorry. Currently, we've worked around it defining the function and element nodes like this: export const func = {
'isFunction': true, // for validation
'faker': { // for faking
'type': 'function'
}
};
export const element = {
'type': 'object',
'isReactElement': true,
'faker': {
'type': 'element'
}
}; |
I'd mean, you want to produce some object using json-schema right? You can't use {
"type": "object",
"properties": {
"isSomething": { "enum": [true] }
},
"required": ["isSomething"]
} I don't like the idea of doing non-standar stuff... |
In case of a As fare as schema example, one would be:
Does that make sense? |
I suppose the use-case I'm after is not a particularly canonical one, but I suppose a different example where this could be useful is something like: {
"type": "number",
"isPrime": true
} |
I'm still confused about isPrime, isFunction, isElement properties, do you want |
I don't want it to understand them, but I would like to be able to extend In fact it would be useful in order to eventually get rid of those optional dependencies altogether, because you could just use them like any other custom data generator. |
I imagine using the the extend API after the change would look something like this: jsf.extend('isPrime', function(input) {
return generateMeAPrime();
});
// backwards compatibility for custom.statement
faker.custom = {
statement: function(length) {
return faker.name.firstName() + " has " + faker.finance.amount() + " on " + faker.finance.account(length) + ".";
}
};
jsf.extend('faker', function(input) { // example input = { custom: { statement: [16] }}
var path = Object.keys(input)[0];
var generator = getIn(faker, path); // get the generator function
return generator(input[path]));
}); Very roughly... |
I got it, but IMO stuff like As long as I know, other JSON-Schema validators can be extended to validate custom types but not custom properties, I guess What do you think? |
Well, we tried suggesting that extension to For the I can have a go at the PR if you're interested in this. It should make it a bit more clear where I'm going with it, unless you're absolutely against the idea. |
@pateketrueke What do you think? Are you interested enough for me to open a PR? |
@charypar I'm still interested on this topic, do you want to discuss about it? 🤔 |
I will have to refresh my memory, it's been a while, but yes, I'd still like to look at it. |
Well, now will be possible to support any keyword: jsf.define('isSomething', (value, schema) => {
return { complexValueOrSomethingWith: value, or: schema };
}); Any registered function will receive the property value and the whole schema where is defined. |
Really a nice feature, hope it can be published soon. |
Rather than opening a PR out of the blue, I thought I'd ask first:
In our project, we use
json-schema-faker
to generate fake data according to a schema, however, because the data are used as React component props, we have some extensions in the schema itself for objects that are not strictly JSON.In order to extend the basic JSON schema, because extending with custom
type
is against the spec, we define custom keywords for custom things (e.g.{ isFunction: true }
instead of{ type: 'function' }
).While I know this is controversial and we probably shouldn't, the overwhelming majority of prop data is covered by JSON schema and the interoperability of it is extremely appealing.
JSON schema faker currently doesn't support extending for custom keywords, but the support is "almost there". If
external
type detection looked at available extensions incontainer
and theexternal
type then delegated to any extension here (probably in order from last one defined to first one defined), it should be quite easy to do this. Unless I'm missing something.Is that something you'd be willing to accept as a PR?
The text was updated successfully, but these errors were encountered: