Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 06:17

    dependabot[bot] on npm_and_yarn

    (compare)

  • 06:17
    dependabot[bot] closed #2059
  • 06:17
    dependabot[bot] commented #2059
  • 06:17
    dependabot[bot] labeled #2060
  • 06:17
    dependabot[bot] opened #2060
  • 06:17

    dependabot[bot] on npm_and_yarn

    build(deps-dev): bump @types/no… (compare)

  • Aug 09 18:48
    jlacivita commented #516
  • Aug 09 18:47
    jlacivita commented #516
  • Aug 09 09:13
    Wytrykus commented #2040
  • Aug 09 09:12
    Wytrykus commented #2040
  • Aug 09 06:49

    dependabot[bot] on npm_and_yarn

    (compare)

  • Aug 09 06:49
    dependabot[bot] closed #2056
  • Aug 09 06:49
    dependabot[bot] commented #2056
  • Aug 09 06:49

    dependabot[bot] on npm_and_yarn

    build(deps-dev): bump @types/no… (compare)

  • Aug 09 06:49
    dependabot[bot] labeled #2059
  • Aug 09 06:49
    dependabot[bot] opened #2059
  • Aug 08 15:40
    budarin closed #2058
  • Aug 08 11:04
    budarin edited #2058
  • Aug 08 11:02
    budarin reopened #2058
  • Aug 08 10:59
    budarin closed #2058
Lisa Van Sau
@Lisa-V-Sau
The error message returns as:
schema is invalid: data/properties/required must be object,boolean, data/properties/keyPersons must be object,boolean, data/properties/applyingSchools must be object,boolean
    properties: {
      createdAt: {
        $ref: "customDefinitions#/definitions/dateTime",
      }
      keyPersons: [
        {
          name: {type: "string"},
          keyPersonDateOfBirth: {$ref: "customDefinitions#/definitions/dateTime"},
          keyPersonBiography: {type: "string"}, }
      ]
Evgeny Poberezkin
@epoberezkin
The schema is indeed invalid - e.g KeyPersons must be an object. Some of the errors are related to parts of the schema that are not in the picture.
Please review some tutorials on authoring JSON schemas, as this is not a problem specific to Ajv. Once you have the basics, StackOverflow has good support for JSON Schema questions.
Lisa Van Sau
@Lisa-V-Sau
Thank you for the support!
Lisa Van Sau
@Lisa-V-Sau
Hi, please can you help with this. I am receiving this error
strict mode: unknown keyword: "data"
if I disable strict mode everything works but it not longer validates my value types
this is my schema which I have broken down
{
  $id: "Schema",
  type: "object",
  properties: {
    data: {
      type: "object",
      properties: {
          name: {type: "string"}
      },
      required: ["name"]
    }
  },
  required: ["data"]
}
this is my function argument
export const validateSchema = (schema, response) => {
  const ajv = new Ajv( {allErrors: true} );
  const validate = ajv.addSchema(Schema).compile(schema);

  const valid = validate(response);
  console.log(validate.errors)

  if (!valid) {
    getSchemaError(validate.errors).then((schemaError) => {
      throw new Error(schemaError);
    });
  } else {
    cy.log("Schema validated!");
  }
};
Lisa Van Sau
@Lisa-V-Sau
Removing the following worked
addSchema(Schema)
Scott Jackson
@scottmmjackson:matrix.org
[m]

from an ajv validate on a custom made schema

error: no schema with key or ref "https://json-schema.org/draft/2020-12/schema"

Would appreciate any ideas anyone has to offer
Using ajv-cli 5.0.0, ajv 8.11.0

Scott Jackson
@scottmmjackson:matrix.org
[m]
adding --spec=draft2020 fixed it 😅 unfortunately this is not documented as part of the CLI
jtuchel
@jtuchel

Hey guys, would someone mind helping with this ( hopefully basic ) question?

https://stackoverflow.com/questions/71673483/how-to-setup-a-custom-ajv-keyword

1 reply
Evgeny Poberezkin
@epoberezkin
It’s better to use additionalProperties (without “properties”) if the intention is to apply it to all properties.
jtuchel
@jtuchel
@epoberezkin is this a reply to my question? I think I'm just not able to understand the mentioned docs in the SO question. How would I create such custom validators?
2 replies
jtuchel
@jtuchel
Sorry, just to be clear, my question was not about the sample code. Please have a look at the sidenote for the real world usecase. I just want to know how to create a custom validator because I'm struggling with the docs ( please see the code snippet in the SO question )
Scott Jackson
@scottmmjackson:matrix.org
[m]
@jtuchel: Right, I am not educated on how to make custom validators. If your real-world problem can be solved with standard JSON schema features though, I'd encourage you to use those.
And I can definitely offer suggestions for that.
jtuchel
@jtuchel

@scottmmjackson:matrix.org thanks. I don't think this can be solved with the standard features.

Given an object with two keys where one key holds an object and one key holds an array of objects. I want to inspect this object and do "validation stuff". The id of the object of the first key and each id of the objects in the array of the second key must be unique.

But these docs https://ajv.js.org/keywords.html#define-keyword-with-code-generation-function didn't help for me and as mentioned in the SO question I don't know how to create a custom keyword

Scott Jackson
@scottmmjackson:matrix.org
[m]
Yeah, this sounds like it might be beyond the scope of JSON schema's purpose. So a custom validator, if you can make it work, might be an option.
Alternatively if you know you have nodejs you could just write a simple node script to slurp your JSON file and exit with a bad code if it fails validation.
That way you separate concerns between "schema check" and "consistency check"
jtuchel
@jtuchel
But I thought the custom keywords can handle that. Because the sample is an even check on numbers. My logic is just more complex but that shouldn't matter :)
Scott Jackson
@scottmmjackson:matrix.org
[m]
Evgeny Poberezkin
@epoberezkin
The docs are pretty high level indeed, they have some simple examples, but you can check the source code of standard keywords in ajv (both for JSON Schema and for JTD) and additional keywords in ajv-keywords repo - most of them are implemented using code generation in the same way as you would implement custom validators, there is nothing special about “standard” keywords other than they are pre-added to Ajv instances (unless you import it from core, in which case there will be no pre-defined keywords).
joe Jankowiak
@jrj2211
Is it possible to create a keyword that runs before the type check is made? Using Mongo IDs, coming from the front end its a string, but if I load data on my back end and pass it back through the validator its invalid because the ID will be an object. Instead of having to declare type: ["object", "string"] anytime I want a mongo ID, can a keyword be run before the type check to either force its validity or modify the data before the type check is made?
Evgeny Poberezkin
@epoberezkin
It would be complex to change in ajv, an easier approach to ascertain the order of keyword evaluation you need is by using allOf keyword - it’s also better, as the order of evaluation becomes explicit - so you would put your keyword in the first branch, type in the second, etc.
Ismael J. Tisminetzky
@ismatim

Hi, I'm having an issue with ajv-format. Everything works so far, nothing fancy, common schema but when I try to use an additional format email, or uri I got this error:

Error: schema is invalid: data/properties/accounts/items/properties/identifier/type must be equal to one of the allowed values...

This is the way I'm using it:

import Ajv from 'ajv';
import addFormats from 'ajv-formats';

const schemaOrganizationUpdate = require('./schemas/organization/update.json');

export const ajv = addFormats(new Ajv());

const validate = ajv.compile(schemaOrganizationUpdate);

No idea why is not working for those new formats. What do you suggest?

Ismael J. Tisminetzky
@ismatim
Ok, I think the error is mine, I misunderstood that the format with type {type:"string", format:"uri"} ^^^
Evgeny Poberezkin
@epoberezkin
You probably need anyOf keyword?
jtuchel
@jtuchel

You probably need anyOf keyword?

@epoberezkin thanks for your reply. But how would I configure it?

{
  type: 'object',
  anyOf: [ MyType ]
}

does not work because it's just a type, not a value

retorquere
@retorquere:mozilla.org
[m]
Is there a way to set the error message with a format added with addFormat?
retorquere
@retorquere:mozilla.org
[m]
:point_up: Edit: Is there a way to set the error message with a format added with addFormat (using a validator function)?
Kay
@kaykhan

Hi

I have 2 interfaces, one interface references the other. I am trying to create a json validation schema, so that it allows the optional type of "industries"

export interface ICreateBrandRequest {
    name: string;
    slug: string;
    website: string;
    image_id: string;
    industries?: ICreateBrandIndustryRequest[];
}

export interface ICreateBrandIndustryRequest {
    brand_id: string;
    industry_id: string;
}

But it does not like tihis, can anyone see what im doing wrong?

export const CreateBrandIndustryRequestSchema: JSONSchemaType<ICreateBrandIndustryRequest> = {
    type: "object",
    properties: {
        brand_id: { type: "string" },
        industry_id: { type: "string" },
    },
    required: ["brand_id", "industry_id"],
    additionalProperties: true,
};

export const CreateBrandRequestSchema: JSONSchemaType<ICreateBrandRequest> = {
    type: "object",
    properties: {
        name: { type: "string" },
        website: { type: "string" },
        slug: { type: "string" },
        image_id: { type: "string" },
        industries: {
            type: "array",
            $ref: CreateBrandIndustryRequestSchema,
        },
    },
    required: ["name", "slug", "website"],
    additionalProperties: false,
}
Evgeny Poberezkin
@epoberezkin
What’s the error?
James Gardner
@james-gardner
Hi I've got an enum of values and would like to be able to accept a string representing one of those values or an array containing at most one of each. Is this within the capabilities of AJV ?
Evgeny Poberezkin
@epoberezkin
You can do it via a combination of standard JSON Schema enum and uniqueItems keywords
James Gardner
@james-gardner
*at least
thanks @epoberezkin - figured it out
Xlence Amersfoort
@matrixpete:matrix.org
[m]

I'm not sure if this is the right place, but I have a configuration issue that basically says: "An unhandled exception occurred: ajvKeywords is not a function".

Is this a camelcase conversion, like ajvKeywords is refering to ajv-keywords package? Or someone created a "ajvKeywords" something?

In my Angular application I dont't use ajv-keywords or ajvKeywords, but some used packages do.
In my package-lock.json I see 10 references to -'https://registry.npmjs.org/ajv-keywords/'_
Pointing to version 3.5.2 or 5.1.0.

Strangely enough, my local development shows no problems. But my GitLab CI/CD does, displaying:

$ npx ng build --configuration production
An unhandled exception occurred: ajvKeywords is not a function

Can this be a problem, cause by minifying code, converting somewhere 'ajv-keywords' into 'ajvKeywords', camelcase conversion?

Using $ npx ng build, the same problem occurs.

No clue where to look and or how to examine this.

Evgeny Poberezkin
@epoberezkin
Try with clean npm install. If it works, the problem must be with the CI/CD setup…
1 reply
Xlence Amersfoort
@matrixpete:matrix.org
[m]

While building the application, we're using Webpack. On the web I see "output UMD library with CamelCase and spinal-case name". Could be a hint of my problem?

https://stackoverflow.com/questions/41811485/webpack-output-umd-library-with-camelcase-and-spinal-case-name#41811547

Xlence Amersfoort
@matrixpete:matrix.org
[m]
We've solved the problem, upgraded Docker environment on Ci/CD environment. Thumb-up.
Aymen kachaii
@cycolic:matrix.org
[m]
Hello, I have a json schema with readOnly property, I want to prevent the user to put data for this property and send an ajv error.
for example when user parse status in payload i show ajv error what is the right approach ?
"status" : {
"type" : "string",
"default" : "CREATED",
"readOnly" : true
},