tdegrunt / jsonschema

JSON Schema validation
Other
1.83k stars 263 forks source link

Add types for basic meta-data annotations #335

Open remcohaszing opened 3 years ago

remcohaszing commented 3 years ago

The types were determined from https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9

awwright commented 3 years ago

This library doesn't actually use these properties for anything, however. Is there a particular benefit to listing properties that don't affect the behavior?

remcohaszing commented 3 years ago

This library might not use these properties directly, but it does implement a specification that defines them.

Some functions specify a schema is passed to a callback. This schema should be a valid JSON schema, it shouldn’t just specify the properties consumed internally.

For example:

import { Schema, Validator } from 'jsonschema';

const schema: Schema = {
  type: 'object',
  properties: {
    string: {
      type: 'string',
      // This should be ok
      default: 'Hello'
    }
  },
};

const v = new Validator();
const res = v.validate(value, schema, {
  rewrite(instance, schema) {
    if (instance === undefined) {
      // This should be ok
      return schema.default;
    }
    return instance;
  }
});

I can think of similar use cases for the other properties I added.

Besides that I also like the simplicity of the existing Schema type in this package and I’m using it on other places, since I’m already depending on this package anyway.

awwright commented 3 years ago

Alright, thanks for the answer.

At the very least, how about I work this in to #318