ajv-validator / ajv-validator/ajv

Reusable Global Helper Functions

Aperta
#1,959 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

**What version of Ajv you are you using?**
Latest

**What problem do you want to solve?**
Decrease compiled validator code size

**What do you think is the correct solution to problem?**
With definition of a custom `CodeKeywordDefinition` is it possible using `gen` scope to define a reusable function that would be declared once in a global scope of a schema validator compilation instead of inline reinjection the same declaration clone per each keyword reference.

E.g. for a schema
```js
{
id: 'someSchema',
properties: {
a: {customKeyword: 'options'},
b: {customKeyword: 'options'},
}
}
```
instead of compilation (pseudo code)
```js
const someSchemaValidator = (data, ...) => {
// a declaration
function customKeywordHelper0() {.......}
if (customKeywordHelper0(data.a)) {
errors = [{path: '/a', keyword: 'customKeyword'}]
}
// b declaration
function customKeywordHelper1() {.......}
if (customKeywordHelper1(data.b)) {
errors = [{path: '/b', keyword: 'customKeyword'}]
}
}
```
It would be nice to have
```js
const someSchemaValidator = (data, ...) => {
// global helper reusable declaration
function customKeywordHelper() {.......}

// a declaration
if (customKeywordHelper(data.a)) {
errors = [{path: '/a', keyword: 'customKeyword'}]
}
// b declaration
if (customKeywordHelper(data.b)) {
errors = [{path: '/b', keyword: 'customKeyword'}]
}
}
```

**Will you be able to implement it?**
I'm curious whether it is feasible in terms of current implementation. Maybe there's something `it.root.gen` available for this purpose?
If not I can assist after a high level introduction into the most appropriate way to implement this since I'm not really familiar with the internal design for now .

P.S. Do I see it correct as `CodeKeywordDefinition` is something the former `inline` keywords definition are evolved with time to?

Thank you.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.