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.

Using JSONSchemaType seems a huge build hog

See original GitHub issue

What version of Ajv are you using? Does the issue happen if you use the latest version?

ajv@8.6.0

Your typescript code

import Ajv, { JSONSchemaType } from 'ajv';
import addFormats from 'ajv-formats';

interface UserRequestBody {
  firstName: string;
  lastName: string;
  email: string;
  age?: number;
  address?: string;
  city?: string;
  zip?: string;
}

const ajv = new Ajv({
  coerceTypes: true,
  useDefaults: true,
  removeAdditional: true,
});
addFormats(ajv);

const userValidationSchema: JSONSchemaType<UserRequestBody> = {
  type: 'object',
  properties: {
    firstName: { type: 'string' },
    lastName: { type: 'string' },
    email: { type: 'string', format: 'email' },
    age: { type: 'number', minimum: 1, nullable: true },
    address: { type: 'string', nullable: true },
    city: { type: 'string', nullable: true },
    zip: { type: 'string', nullable: true },
  },
  required: ['firstName', 'lastName', 'email'],
};

const validateUser = ajv.compile(userValidationSchema);

export default validateUser;

Typescript compiler error messages No compilation issues but build time increases drasticly, from around 1,5 secs to 11 secs

By using --generateTrace i discovered that is was related to UncheckedJSONSchemaType type.

Just changing to

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

interface UserRequestBody {
  firstName: string;
  lastName: string;
  email: string;
  age?: number;
  address?: string;
  city?: string;
  zip?: string;
}

const ajv = new Ajv({
  coerceTypes: true,
  useDefaults: true,
  removeAdditional: true,
});
addFormats(ajv);

const userValidationSchema = {
  type: 'object',
  properties: {
    firstName: { type: 'string' },
    lastName: { type: 'string' },
    email: { type: 'string', format: 'email' },
    age: { type: 'number', minimum: 1, nullable: true },
    address: { type: 'string', nullable: true },
    city: { type: 'string', nullable: true },
    zip: { type: 'string', nullable: true },
  },
  required: ['firstName', 'lastName', 'email'],
};

const validateUser = ajv.compile<UserRequestBody>(userValidationSchema);

export default validateUser;

Describe the change that should be made to address the issue?

What can i do to assist or resolve this issue

Are you going to resolve the issue? Not sure if i can help 🤷

Issue Analytics

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

github_iconTop GitHub Comments

6reactions
aleccool213commented, Jun 24, 2021

This wouldn’t be much of an issue if it was just a build time issue but @tommarien is right that it essentially makes your IDE unusable. Users will get put off by this. I have a very powerful Macbook and it slowed down the TS compiler in VS Code to a snail 🐌

1reaction
tommariencommented, Jun 24, 2021

@epoberezkin wow, that was closed fast, maybe not advise it as best practice on the ajv site. Other question is there a type which you can use that just does the same as using the `“$schema”: “http://json-schema.org/draft-07/schema#” in your json schema?

const scheme: JSONSchema = {};

The problem is was describing impacts everything in a normal typescript oriented build, vscode intellisense is also impacted when using @typescript/es-lint. I am guessing there is something that can be optimised, maybe something recursive. maybe related to https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540

Read more comments on GitHub >

github_iconTop Results From Across the Web

The power of contracts for building data systems - YouTube
In this deep dive, Jake Thomas shares his hypothesis for why the jsonschema is the ticket to contract-driven communication, ...
Read more >
Using with TypeScript - Ajv JSON schema validator
Utility type for JTD data type. You can use JTD schema to construct the type of data using utility type JTDDataType. JSON Type...
Read more >
C# serialization with JsonSchema and System.Text.Json
Learn how code generation can build on System.Text.Json and JSON Schema to create a great experience for C# developers.
Read more >
How to enable react-jsonschema-form themes when using the ...
I have a massive JSON schema and I want to generate an HTML/JS form from it. It appears that the react-jsonschema-form is a...
Read more >
JSON Schema Serializer and Deserializer
For example, when using the mbknor-jackson-jsonSchema utility to generate a ... to appear in a JSON document without being specified in the JSON...
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