graphql
Version:
A Query Language and Runtime which can target any service.
82 lines (60 loc) • 2.22 kB
JavaScript
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
;
/**
* The result of schema validation. `isValid` is true if validation is
* successful. `errors` is null if no errors occurred, and is a non-empty array
* if any validation errors occurred.
*/
var _createClass = require('babel-runtime/helpers/create-class')['default'];
var _classCallCheck = require('babel-runtime/helpers/class-call-check')['default'];
var _Object$defineProperty = require('babel-runtime/core-js/object/define-property')['default'];
var _interopRequireDefault = require('babel-runtime/helpers/interop-require-default')['default'];
_Object$defineProperty(exports, '__esModule', {
value: true
});
exports.validateSchema = validateSchema;
var _utilsInvariant = require('../utils/invariant');
var _utilsInvariant2 = _interopRequireDefault(_utilsInvariant);
var _error = require('../error');
var _schema2 = require('./schema');
var _allRules = require('./allRules');
/**
* Checks an input type system for conformance to the "Type System"
* section of the spec.
*/
function validateSchema(schema, argRules) {
(0, _utilsInvariant2['default'])(schema, 'Must provide schema');
var context = new ValidationContext(schema);
var errors = [];
var rules = argRules || _allRules.allRules;
for (var ii = 0; ii < rules.length; ++ii) {
var newErrors = rules[ii](context);
if (newErrors) {
errors.push.apply(errors, newErrors);
}
}
var isValid = errors.length === 0;
return { isValid: isValid, errors: isValid ? null : errors.map(_error.formatError) };
}
var ValidationContext = (function () {
function ValidationContext(schema) {
_classCallCheck(this, ValidationContext);
this._schema = schema;
}
_createClass(ValidationContext, [{
key: 'getSchema',
value: function getSchema() {
return this._schema;
}
}]);
return ValidationContext;
})();
exports.ValidationContext = ValidationContext;