![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
@ovotech/json-schema
Advanced tools
A lightweight a json-schema. Depends only on yaml package and node-fetch.
A lightweight a json-schema.
Dependencies:
Supported JSON Schema drafts
yarn add @ovotech/json-schema
import { validate, Schema } from '@ovotech/json-schema';
const schema: Schema = {
type: 'string',
format: 'email',
};
const value = '12horses';
validate({ schema, value }).then((result) => console.log(result.valid, result.errors));
Should output
false
[ '[value] should match email format' ]
It is comparatively slower than the venerable Ajv but is a lot simpler and smaller. It also passes the official json-schema-org/JSON-Schema-Test-Suite.
It does not rely on any shared state or static parameters returning all the errors for a given validation operation in one result. Which was one of the reasons to develop an alternative to Ajv in the first place.
It was made as a lightweight dependency to @ovotech/laminar framework with nice error messages.
If we assume we have those 2 http resources at the given URLs, You can compile the schema once, downloading the relevant URLs, and then use the CompiledSchema
to perform any further validation without downloading and parsing the files again.
import { validate, compile } from '@ovotech/json-schema';
import nock from 'nock';
const mainSchema = `
{
"type": "object",
"properties": {
"size": {
"type": "number"
},
"color": { "$ref": "https://example.com/color" }
}
}
`;
const colorSchema = `
enum:
- red
- green
- blue
`;
nock('https://example.com')
.get('/schema')
.reply(200, mainSchema, { 'Content-Type': 'application/json' })
.get('/color')
.reply(200, colorSchema, { 'Content-Type': 'application/yaml' });
compile('https://example.com/schema').then((schema) => {
console.log(schema);
const correct = { size: 10, color: 'red' };
const incorrect = { size: 'big', color: 'orange' };
const correctResult = validate({ schema, value: correct });
console.log(correctResult.valid, correctResult.errors);
const incorrectResult = validate({ schema, value: incorrect });
console.log(incorrectResult.valid, incorrectResult.errors);
});
You can also provide paths to local files to download the schema from. It it ends with "yaml" or "yml" it would be loaded as YAML, otherwise it would be parsed as JSON.
import { validateCompiled, validate, compile } from '@ovotech/json-schema';
import { join } from 'path';
const schema = join(__dirname, 'color-schema.yaml');
validate({ schema, value: 'orange' }).then((result) => {
console.log('validate', result.valid, result.errors);
});
compile({ schema }).then((compiledSchema) => {
const result = validateCompiled({ schema: compiledSchema, value: 'red' });
console.log('compile', result.valid, result.errors);
});
You can customize error messages with formatErrors
option. It gets the full validaiton with its errors, and should return an array of string error messages.
const formatErrors: FormatErrors = (validation) =>
validation.errors.map((error) => ` - ${error.code} : ${error.name} \n`);
validate({ schema, value, formatErrors }).then((result) => console.log(result.valid, result.errors));
The possible error codes are:
export type InvalidCode =
| 'not'
| 'enum'
| 'type'
| 'multipleOf'
| 'minimum'
| 'exclusiveMinimum'
| 'maximum'
| 'exclusiveMaximum'
| 'pattern'
| 'format'
| 'false'
| 'maxLength'
| 'minLength'
| 'contains'
| 'additionalProperties'
| 'unevaluatedProperties'
| 'unevaluatedItems'
| 'required'
| 'minProperties'
| 'maxProperties'
| 'dependencies'
| 'uniqueItems'
| 'minItems'
| 'maxItems'
| 'oneOf'
| 'anyOf';
Alternatively, you can set formatErrors
to false and receive the raw error message objects.
validate({ schema, value, formatErrors: false }).then((result) => console.log(result.valid, result.errors));
### API
**validate** validate given data with a schema. The schema can be a path to a yaml / json file, or a url to one, as well as plain old js object with the said schema.
**compile** Compile a schema by downloading any dependencies, resolving json refs or loading yaml / json files from URLs or file paths. The result can be passed to validate to skip the downloading.
**validateCompiled** You can pass the compiled schema and it will process the schema synchronously, without the use of Promises.
**ensureValid** Ensure that a given value is of a typescript type, using json-schema
**coerce** Coerce a given value, using the provided json schema.
- With type: 'json'
This is used to convert json validated with json schema into javascript objects.
Namely it converts all the strings with format date and date-time into Date objects.
- With type: 'query'
To convert a value coming from a URL query string to the type you want it to be,
for example '12' with type: 'integer' will be converted to 12 so the validation can succeed.
Additionally, we assign default values where appropriate.
### Develop
yarn yarn test
FAQs
A lightweight a json-schema. Depends only on yaml package and node-fetch.
The npm package @ovotech/json-schema receives a total of 2,953 weekly downloads. As such, @ovotech/json-schema popularity was classified as popular.
We found that @ovotech/json-schema demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 304 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.