JSON faker for all combination - javascript

Currently I have a requirement of generating test data for a JSON schema. I have found this following github very useful for this:
https://www.npmjs.com/package/json-schema-faker#overview
Now, if we want to extend it to generate all the required fields + all combinations of optional field, how can we generate this. For example:
The following JSON schema should output two JSON's:
{
"title": "Example Schema",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["name"]
}
{
"name" : <random_string>
}
AND
{
"name" : <random_string>,
"age" : <random_int>
}

You can try json-faker which is available in npm, it is easy to use

Related

Validating an object in parts using json-schema

I have a complex object that I need to validate in parts (each part is validated by a different party). I guess I can create a schema file per each part, but I really want to be able to use one file.
For example, my schema could be the following, and I want to be able to validate an object against obj1.
I'm trying to figure out a standard way of defining such a situation and what I can expect from implementations. Not looking necessarily for a specific library. But if some libraries support this and some don't, I'm interested in Javascript ones.
{
"required": [
"prop1",
"another",
"obj1",
"obj2"
],
"properties": {
"prop1": {
"type": "integer"
},
"another": {
"type": "string"
},
"obj1": {
"$ref": "#/$defs/obj1"
},
"obj2": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "string"
}
}
}
},
"$defs": {
"obj1": {
"type": "object",
"required": [
"sub1",
"sub2",
"a_number"
],
"properties": {
"sub1": {
"type": "string"
},
"sub2": {
"type": "string"
},
"a_number": {
"type": "integer"
}
}
}
}
}

#value! message instead of arguments type in Function Argument box

I'm building an Excel add-in with Custom Functions. This function has a description with types for every argument. To create a functions.json file I use YO generator provided by Microsoft.
Everything works fine until some COM add-in is installed. In that case, the Function Arguments box shows the wrong argument types.
Also, the Help page link redirects the user to the Microsoft default page instead described in the functions.json file.
The description for that function in the functions.json file looks like this:
"functions": [
{
"description": "Do something",
"helpUrl": "https://custom.help.page",
"id": "VALUE",
"name": "VALUE",
"parameters": [
{
"description": "Description here",
"name": "ArgName",
"type": "string"
},
{
"description": "Description here",
"name": "date",
"type": "string"
},
{
"description": "Description here",
"dimensionality": "matrix",
"name": "some_name",
"optional": true,
"repeating": true,
"type": "string"
}
],
"result": {
"type": "number"
}
},

AJV Multilevel/Nested JSON schema validation

Using the schema
{
"type": "object",
"required": [
"person",
"animal"
],
"person": {
"title": "person",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"animal": {
"title": "animal",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
This schema is valid when it is compared against this object
{
"person": 0,
"animal": "dog"
}
I only want it to validate for the properties within person object (as it also has required properties). For example, only the following would be valid:
{
"person": {
"name": "myName"
},
"animal": "dog"
}
How can I ensure nested objects are validated in my schema using AJV?
In your schema, you need to put animal and person inside a properties object.
Currently, as those property keys are not within a properties object, they are classed as unkown keywords and ignored.
Otherwise, yeah, you have this correct.

Using negative lookbehind with jsonschema

I'm using Node 9.2.0 and ajv 6.0.0.
I have a schema that I wish to use negative lookbehind on, it's defined like:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "release format",
"description": "A Release Format",
"type": "object",
"properties": {
"id": {"type": "integer"},
"title": {
"anyOf": [
{
"type": "string",
"pattern": "^((?!itunes).)*$"
},
{
"type": "string",
"pattern": "^((?!exclusive).)*$"
},
{
"type": "string",
"pattern": "^((?!ringtone).)*$"
}
]
}
}
}
However, it appears that when I try and validate this with AJV using the following data: {"id": 123, "title": "world exclusive"} I don't get a validation error.
The code:
const Ajv = require('ajv');
class Validator {
constructor() {
this.releaseFormatSchema = JSON.parse(fs.readFileSync('./schemas/release-format.json'));
this.schemaValidator = new Ajv({allErrors: true});
}
validate(data) {
let validate = this.schemaValidator.compile(this.releaseFormatSchema);
let valid = validate(data);
console.log(valid);
console.log(validate);
}
}
where data would be: {"id": 123, "title": "world exclusive"}. I would expect this to error, however it's currently telling me that the data is valid.
The answer was also found by #sln and #ClasG, anyOf does a union between titles patterns can match : "all except strings which contains itunes" union "all except strings which contains exclusive" union "...", which means all which not contains all the forbidden keywords. It can be fixed either
using allOf instead of anyOF
"title": {
"allOf": [
{
"type": "string",
"pattern": "^((?!itunes).)*$"
},
{
"type": "string",
"pattern": "^((?!exclusive).)*$"
},
{
"type": "string",
"pattern": "^((?!ringtone).)*$"
}
]
}
using single type/pattern :
"title": {
"type": "string",
"pattern": "^((?!itunes|exclusive|ringtone).)*$"
}

Convert JSON Schema Format

I have json schema definition like this:
(Update: basically its draft 03 format: http://json-schema.org/draft-03/schema#)
{
"$schema": "http://json-schema.org/draft-03/schema",
"product": {
"name": {
"required": true,
"type": "string"
},
"description": {
"required": true,
"type": "string"
}
},
"type": "object"
}
But I need it in this format(standard json schema structure), which is draft 04 format(http://json-schema.org/draft-04/schema#)
{
"type": "object",
"properties": {
"product": {
"type": "object",
"required": [
"name",
"description"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
},
"required": [
"product"
]
}
Is there any converter to convert the above format to this one? I just don't want to manually do it which might be error prone.
I haven't used it, so I can't vouch for it personally, but there is conversion tool.
https://github.com/geraintluff/json-schema-compatibility

Categories

Resources