1EdTech / openbadges-discussion

A no-code repository for having discussions related to the general technical issues of openbadges.
10 stars 3 forks source link

Demonstration of using json-schema for basic validation #21

Closed cmcavoy closed 2 years ago

cmcavoy commented 10 years ago

Built off of work from @ottonomy. Defines the badgeClass and badgeAssertion in json-schema. Basic test suite demonstrates using jjv to validate against the schemas.

If all this looks right, I'd like to take a stab at moving openbadges-specification to a module that exports these schemas, then refactor openbadges-validator to use these schema and jjv for the first level of validation.

kayaelle commented 10 years ago

This is looking great. I think we may want to add more specifics to the string types. For instance:

"evidence": {
        "description": "URL of the work that the recipient did to earn the achievement. This can be a page that links out to other pages if linking directly to the work is infeasible.",
        "type": "string",
        "format": "uri"
  }
  "issuedOn": {
        "description": "Date that the achievement was awarded.",
        "type": "string",
        "format": "date-time"
      },

Although don't think the above covers unix timestamps original was: ["string", "number"]

ottonomy commented 10 years ago

Thanks for pulling this together and distributing it with such an easy testing system.

Based on the Understanding JSON Schema book that Jason Lewis sent me, I have worked up a version of the badgeClass schema that packages a bunch of OBI object definitions inside a "definitions" object at the top level of the schema and then references those definitions, separating definition from structure a little bit.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "definitions": {
        "ImageDataUrl": { 
            "type": "string",
            "pattern": "^data:(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"
        },
        "BadgeImage": {
            "oneOf": [
                { "$ref": "#/definitions/ImageDataUrl" },
                { "type": "string", "format": "uri" }
            ]
        }

    ...

    "properties": {
        "image": { "$ref": "#/definitions/BadgeImage" }

    ...

It took me a long time because I couldn't get it to validate with jjv or get any useful error messages out of it. Switched over to using JaySchema, and it started working. See my branch at: https://github.com/ottonomy/openbadges-discussion/tree/cmcavoy-schema/openbadges-assertion-schema ( badgeClass schema | corresponding test file )

The definitions block in the schema is a little bloated because the first path I was trying was to see if we could mash together the schemas for all three OBI objects (Assertion, Badge Class, and Issuer Organization) into one schema. I was trying to get them using shared definitions, referencing each object's schema as "https://badgealliance.org/standard/1.1#/badgeClass", but I couldn't get that approach to validate -- the $ref's to definitions got lost.