9

I'm looking for a ajv-like json schema validator for deno. Wondering is there any alternative?

Eric Miao
  • 91
  • 1
  • 2
  • Here's the most relevant Github Issue: https://github.com/ajv-validator/ajv/issues/1850 – Ole Jul 29 '22 at 10:03

4 Answers4

5

You have some options to use none-deno modules.

The easiest way is to use a service like esm.sh and import it like:

import Ajv from 'https://esm.sh/ajv@8.6.1';
import addFormats from 'https://esm.sh/ajv-formats@2.1.0';
const ajv = new Ajv({allErrors: true});
addFormats(ajv);

esm.sh even provides .d.ts definitions if available, so you can import types as well.

import Ajv, {ValidateFunction} from 'https://esm.sh/ajv@8.6.1';
const validate: ValidateFunction = new Ajv().compile(schema);

In some cases, you can even import the raw typescript file directly from git. But Ajv imports json files directly, which deno does not support atm.

Nemo64
  • 2,535
  • 3
  • 20
  • 24
4

You don't need an alternative, you can use ajv.

Ajv provides a bundle for browsers: https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js

All you need to do is download it, save it to your project and add: export default Ajv at the bottom of the file.

ajv.js

/* ajv 6.12.2: Another JSON Schema Validator */
!function(e){if("object"==typeof exports&&"undefined"!=typeof module) /*....... */
//# sourceMappingURL=ajv.min.js.map

export default Ajv;

index.js

import Ajv from './ajv.js'

const ajv = new Ajv({allErrors: true});

const schema = {
  "properties": {
    "foo": { "type": "string" },
    "bar": { "type": "number", "maximum": 3 }
  }
};

function test(data) {
  const valid = validate(data);
  if (valid) console.log('Valid!');
  else console.log('Invalid: ' + ajv.errorsText(validate.errors));
}


const validate = ajv.compile(schema);

test({"foo": "abc", "bar": 2});
test({"foo": 2, "bar": 4});

Remember that Deno is a JavaScript runtime, so any code which uses plain JavaScript, you'll be able to use it with very little modification, in this case just export default Ajv

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • 2
    Also note that `ajv` has a type definitions file, https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/ajv.d.ts, so you can get type checking and hints if you prefix the import statement with `// @deno-types="https://raw.githubusercontent.com/ajv-validator/ajv/master/lib/ajv.d.ts"` – andyvanee Jun 09 '20 at 17:36
  • AJV seems to not work with [Deno Deploy](https://deno.com/deploy), due to it generating code. – Ole Jul 29 '22 at 10:00
1

Till now, ajv-like schema validator is not available for Deno. However, you can try value_schema for schema validation. It has both Node.js and Deno versions.

DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98
  • 1
    Do I understand correctly that `value_schema`/`value-schema` is not able to handle JSON schemas but implements it's own way of defining schemas? Is there some way to generate the `value-schema` code for a JSON schema? To what degree can `value-schema` even offer the capabilities of a JSON schema? – karfau Nov 28 '21 at 19:09
1

jtd is a viable alternative. It is a deno native implementation of JSON Type Definition, aka RFC 8927, which is an alternative to json-schema validation (and thus ajv) but has a similar purpose.

justin.m.chase
  • 13,061
  • 8
  • 52
  • 100