Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested schema errors not comprehensive #85

Closed
cdm6a opened this issue Jun 29, 2015 · 0 comments
Closed

Nested schema errors not comprehensive #85

cdm6a opened this issue Jun 29, 2015 · 0 comments

Comments

@cdm6a
Copy link

cdm6a commented Jun 29, 2015

When I attempt to call the following I seem to be receiving an error that is not as comprehensive as I would have hoped:

const SCHEMAS = {
  Amount: require('./schemas/Amount.json'),
  PositiveFloatString: require('./schemas/PositiveFloatString.json'),
  Party: require('./schemas/Party.json)
};

function validate(object, schemaName) {
  const options = {schemas: SCHEMAS, greedy: true};
  const schema = SCHEMAS[schemaName];
  if (schema === undefined) {
    throw new Error('schema not found for: ' + schemaName);
  }
  const validate2 = schemaValidator(schema, options);
  const isValid = validate2(object);
  if (!isValid) {
    return {
      isValid: false,
      errors: validate2.errors
    };
  }
  return {
    isValid: true
  };
}

validate({
        name: 'bob smith',
        amount: {
          currency: 'USD'
        }
      }, 'Party'));

Yields:

{ 
  isValid: false,
  errors: [{ 
    field: 'data.amount',
    message: 'referenced schema does not match' 
  }] 
}

Ideally I was hoping that it would tell me something along the lines of:

errors: [{ 
    field: 'data.amount.value',
    message: 'is required' 
  }] 

I was hoping someone might be able to tell me if this is a short coming of the library or if this is a shortcoming of my implementation / json references.

My schemas are as follows:
Party.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "Party",
  "title": "Party",
  "description": "Represents a single party to a payment.",
  "type": "object",
  "properties": {
    "name": {
      "$ref": "Account"
    },
    "amount": {
      "description": "How much this party will send or receive in the payment.",
      "$ref": "Amount"
    }
  },
  "additionalProperties": false,
  "required": ["name", "amount"]
}

Amount.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "Amount",
  "title": "Amount",
  "type": "object",
  "properties": {
    "value": {
      "$ref": "PositiveFloatString"
    },
    "currency": {
      "type": "string",
      "$ref": "Currency"
    }
  },
  "additionalProperties": false,
  "required": ["value", "currency"]
}

PositiveFloatString.json

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "PositiveFloatString",
  "title": "PositiveFloatString",
  "description": "A string representation of a floating point number",
  "type": "string",
  "pattern": "^[+]?[0-9]*[.]?[0-9]+([eE][-+]?[0-9]+)?$"
}
@cdm6a cdm6a closed this as completed Jun 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant