question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access root data in code generation custom keyword

See original GitHub issue

What version of Ajv you are you using? 7.2.3

What problem do you want to solve? I’m trying to validate whether array items are used as object keys.

Let’s say I have a schema like this

{
	"keys": {
		"type": "array",
		"arrayItemsAreKeysOf": "/specialObject" // attempting to implement this custom keyword
	},
	"specialObject": {
		"type": "object"
	}
}

This is valid:

{
	"keys": ["one", "two"],
	"specialObject": {
		"one": true,
		"two": true
	}
}

This is invalid:

{
	"keys": ["one", "two", "three"],
	"specialObject": {
		"one": true,
		"two": true
	}
}

What do you think is the correct solution to problem? In ajv v6 I had a custom “validate” function to check this:

ajv.addKeyword('arrayItemsAreKeysOf', {
	validate(schema, data, parentSchema, dataPath, parentData, propertyName, rootData) {
		const validArrayItems = R.pipe(
			R.path(schema.split('/')),
			R.keys,
		)(rootData);
		return R.all(R.includes(R.__, validArrayItems))(data);
	},
	errors: false,
});

(If you’re unfamiliar with Ramda, this takes the keys of a given path (/specialObject) from rootData and then checks if all items of data (keys array in examples) are present)

And it worked fine. I tried rewriting this for ajv v7+ using the code generation keyword, but I’m not sure if it’s possible to access rootData there.

Will you be able to implement it? Maybe it’s already possible to do what I’m trying to achieve?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
domnantascommented, Mar 29, 2021

I have rewritten the above v6 code to v7, took rootData from dataCtx:

ajv.addKeyword('arrayItemsArePropertiesOf', {
	validate(schema, data, parentSchema, dataCtx) {
		const validArrayItems = R.pipe(
			R.path(schema.split('/')),
			R.keys,
		)(dataCtx.rootData);

		return R.all(R.includes(R.__, validArrayItems))(data);
	},
});

So rootData is only accessible in validation functions and not in code generation functions, right?

0reactions
domnantascommented, Mar 30, 2022

To make it slightly more clear for anyone stumbling on this, this is how you access the rootData in the code function:

ajv.addKeyword({
  keyword: "even",
  code(cxt: KeywordCxt) {
    cxt.fail(_`console.log(data)`) // data here returns the "rootData" 
  },
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Embedded Coder Dictionary - MathWorks
On the C Code tab, select Code Interface > Embedded Coder Dictionary. The Embedded Coder Dictionary window displays code generation definitions that are...
Read more >
ROOTUsersGuide - CERN
ROOT is an object-oriented framework aimed at solving the data analysis challenges of high-energy physics. There are two key words in this definition, ......
Read more >
I/O of custom classes - ROOT
I/O of custom classes. On this page. Generating dictionaries. Using ACLiC; Using rootcling. Embedding the rootcling call into a GNU Makefile. Using CMake....
Read more >
Generate code | IntelliJ IDEA Documentation - JetBrains
IntelliJ IDEA provides multiple ways to generate common code constructs and recurring elements, which helps you increase productivity.
Read more >
Embedded Coder™ User's Guide - Purdue Engineering
Pointers for Signals and Parameters Using Simulink Data. Objects . ... Generating Code with Custom Storage Classes . ... Referenced by: '<Root>/Constant'.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found