UNPKG

core-types

Version:

Generic type declarations for e.g. TypeScript, GraphQL and JSON Schema

20 lines (19 loc) 808 B
import { MalformedTypeError } from './error.js'; import { hasConstEnum, isEqual } from './util.js'; export function validate(node) { if (hasConstEnum(node)) validateConstEnum(node); if (node.type === 'and') node.and.forEach(subNode => validate(subNode)); if (node.type === 'or') node.or.forEach(subNode => validate(subNode)); } function validateConstEnum(node) { if (node.enum && node.enum.length === 0) throw new MalformedTypeError("Empty enum is not allowed", node); if (node.enum && node.const !== undefined) { if (!node.enum.some(entry => isEqual(entry, node.const))) throw new MalformedTypeError("Enum and const are both set, but enum doesn't contain const", node); } // TODO: Check data type of enum/const matching type }