data-prism
Advanced tools
Comparing version 0.0.36 to 0.0.37
@@ -38,48 +38,41 @@ const attributeTypes = [ | ||
export function ensureValidSchema(schema) { | ||
try { | ||
if (typeof schema !== "object") { | ||
throw new Error("The schema must be an object."); | ||
if (typeof schema !== "object") { | ||
throw new Error("The schema must be an object."); | ||
} | ||
if (!("resources" in schema) || Array.isArray(schema.resources)) { | ||
throw new Error('The schema must have a "resources" object with valid resources as values.'); | ||
} | ||
Object.entries(schema.resources).forEach(([resKey, resource]) => { | ||
if (!("attributes" in resource) || Array.isArray(resource.attributes)) { | ||
throw new Error(`Each schema resource must have an "attributes" object with valid resource attributes as values. Check the "${resKey}" resource.`); | ||
} | ||
if (!("resources" in schema) || Array.isArray(schema.resources)) { | ||
throw new Error('The schema must have a "resources" object with valid resources as values.'); | ||
const idAttribute = resource.idAttribute ?? "id"; | ||
if (!(idAttribute in resource.attributes)) { | ||
throw new Error(`An id attribute is required. Please ensure "${idAttribute}" attribute is defined on resource type "${resKey}.`); | ||
} | ||
Object.entries(schema.resources).forEach(([resKey, resource]) => { | ||
if (!("attributes" in resource) || Array.isArray(resource.attributes)) { | ||
throw new Error(`Each schema resource must have an "attributes" object with valid resource attributes as values. Check the "${resKey}" resource.`); | ||
Object.entries(resource.attributes).forEach(([attrKey, attribute]) => { | ||
if (!attribute.type) { | ||
throw new Error(`All attributes must have a type. Check the "${attrKey}" attribute on the "${resKey}" resource type.`); | ||
} | ||
const idAttribute = resource.idAttribute ?? "id"; | ||
if (!(idAttribute in resource.attributes)) { | ||
throw new Error(`An id attribute is required. Please ensure "${idAttribute}" attribute is defined.`); | ||
if (!attributeTypes.includes(attribute.type)) { | ||
throw new Error(`"${attribute.type}" is not a valid type. Check the "${attrKey}" attribute on the "${resKey}" resource type. Valid types: ${attributeTypes.join(", ")}.`); | ||
} | ||
Object.entries(resource.attributes).forEach(([attrKey, attribute]) => { | ||
if (!attribute.type) { | ||
throw new Error(`All attributes must have a type. Check the "${attrKey}" attribute on the "${resKey}" resource type.`); | ||
} | ||
if (!attributeTypes.includes(attribute.type)) { | ||
throw new Error(`"${attribute.type}" is not a valid type. Check the "${attrKey}" attribute on the "${resKey}" resource type. Valid types: ${attributeTypes.join(", ")}.`); | ||
} | ||
}); | ||
if (!("relationships" in resource) || | ||
Array.isArray(resource.relationships)) { | ||
throw new Error(`Each schema resource must have an "relationships" object with valid resource relationships as values. Check the "${resKey}" resource.`); | ||
}); | ||
if (!("relationships" in resource) || | ||
Array.isArray(resource.relationships)) { | ||
throw new Error(`Each schema resource must have an "relationships" object with valid resource relationships as values. Check the "${resKey}" resource.`); | ||
} | ||
Object.entries(resource.relationships).forEach(([relKey, relationship]) => { | ||
if (!relationship.cardinality || !relationship.type) { | ||
throw new Error(`All relationships must have a cardinality or a type. Check the "${relKey}" relationship on the "${resKey}" resource type.`); | ||
} | ||
Object.entries(resource.relationships).forEach(([relKey, relationship]) => { | ||
if (!relationship.cardinality || !relationship.type) { | ||
throw new Error(`All relationships must have a cardinality or a type. Check the "${relKey}" relationship on the "${resKey}" resource type.`); | ||
} | ||
if (relationship.cardinality !== "one" && | ||
relationship.cardinality !== "many") { | ||
throw new Error(`Relationship cardinalities must be either "one" or "many". Check the "${relKey}" relationship on the "${resKey}" resource type.`); | ||
} | ||
if (!Object.keys(schema.resources).includes(relationship.type)) { | ||
throw new Error(`"${relationship.type}" is not a valid relationship type. Relationship types must be a type of resource defined in the schema. Check the "${relKey}" relationship on the "${resKey}" resource type. Valid resource type: ${Object.keys(schema.resources).join(", ")}`); | ||
} | ||
}); | ||
if (relationship.cardinality !== "one" && | ||
relationship.cardinality !== "many") { | ||
throw new Error(`Relationship cardinalities must be either "one" or "many". Check the "${relKey}" relationship on the "${resKey}" resource type.`); | ||
} | ||
if (!Object.keys(schema.resources).includes(relationship.type)) { | ||
throw new Error(`"${relationship.type}" is not a valid relationship type. Relationship types must be a type of resource defined in the schema. Check the "${relKey}" relationship on the "${resKey}" resource type. Valid resource type: ${Object.keys(schema.resources).join(", ")}`); | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
throw new Error("Something went wrong validating your schema.", { | ||
cause: err, | ||
}); | ||
} | ||
}); | ||
} |
{ | ||
"name": "data-prism", | ||
"version": "0.0.36", | ||
"version": "0.0.37", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -109,84 +109,76 @@ type SchemaAttribute = { | ||
export function ensureValidSchema(schema: any): void { | ||
try { | ||
if (typeof schema !== "object") { | ||
throw new Error("The schema must be an object."); | ||
if (typeof schema !== "object") { | ||
throw new Error("The schema must be an object."); | ||
} | ||
if (!("resources" in schema) || Array.isArray(schema.resources)) { | ||
throw new Error( | ||
'The schema must have a "resources" object with valid resources as values.', | ||
); | ||
} | ||
Object.entries(schema.resources).forEach(([resKey, resource]: [any, any]) => { | ||
if (!("attributes" in resource) || Array.isArray(resource.attributes)) { | ||
throw new Error( | ||
`Each schema resource must have an "attributes" object with valid resource attributes as values. Check the "${resKey}" resource.`, | ||
); | ||
} | ||
if (!("resources" in schema) || Array.isArray(schema.resources)) { | ||
const idAttribute = resource.idAttribute ?? "id"; | ||
if (!(idAttribute in resource.attributes)) { | ||
throw new Error( | ||
'The schema must have a "resources" object with valid resources as values.', | ||
`An id attribute is required. Please ensure "${idAttribute}" attribute is defined on resource type "${resKey}.`, | ||
); | ||
} | ||
Object.entries(schema.resources).forEach( | ||
([resKey, resource]: [any, any]) => { | ||
if (!("attributes" in resource) || Array.isArray(resource.attributes)) { | ||
Object.entries(resource.attributes).forEach( | ||
([attrKey, attribute]: [any, any]) => { | ||
if (!attribute.type) { | ||
throw new Error( | ||
`Each schema resource must have an "attributes" object with valid resource attributes as values. Check the "${resKey}" resource.`, | ||
`All attributes must have a type. Check the "${attrKey}" attribute on the "${resKey}" resource type.`, | ||
); | ||
} | ||
const idAttribute = resource.idAttribute ?? "id"; | ||
if (!(idAttribute in resource.attributes)) { | ||
if (!attributeTypes.includes(attribute.type)) { | ||
throw new Error( | ||
`An id attribute is required. Please ensure "${idAttribute}" attribute is defined.`, | ||
`"${attribute.type}" is not a valid type. Check the "${attrKey}" attribute on the "${resKey}" resource type. Valid types: ${attributeTypes.join(", ")}.`, | ||
); | ||
} | ||
}, | ||
); | ||
Object.entries(resource.attributes).forEach( | ||
([attrKey, attribute]: [any, any]) => { | ||
if (!attribute.type) { | ||
throw new Error( | ||
`All attributes must have a type. Check the "${attrKey}" attribute on the "${resKey}" resource type.`, | ||
); | ||
} | ||
if ( | ||
!("relationships" in resource) || | ||
Array.isArray(resource.relationships) | ||
) { | ||
throw new Error( | ||
`Each schema resource must have an "relationships" object with valid resource relationships as values. Check the "${resKey}" resource.`, | ||
); | ||
} | ||
if (!attributeTypes.includes(attribute.type)) { | ||
throw new Error( | ||
`"${attribute.type}" is not a valid type. Check the "${attrKey}" attribute on the "${resKey}" resource type. Valid types: ${attributeTypes.join(", ")}.`, | ||
); | ||
} | ||
}, | ||
); | ||
Object.entries(resource.relationships).forEach( | ||
([relKey, relationship]: [any, any]) => { | ||
if (!relationship.cardinality || !relationship.type) { | ||
throw new Error( | ||
`All relationships must have a cardinality or a type. Check the "${relKey}" relationship on the "${resKey}" resource type.`, | ||
); | ||
} | ||
if ( | ||
!("relationships" in resource) || | ||
Array.isArray(resource.relationships) | ||
relationship.cardinality !== "one" && | ||
relationship.cardinality !== "many" | ||
) { | ||
throw new Error( | ||
`Each schema resource must have an "relationships" object with valid resource relationships as values. Check the "${resKey}" resource.`, | ||
`Relationship cardinalities must be either "one" or "many". Check the "${relKey}" relationship on the "${resKey}" resource type.`, | ||
); | ||
} | ||
Object.entries(resource.relationships).forEach( | ||
([relKey, relationship]: [any, any]) => { | ||
if (!relationship.cardinality || !relationship.type) { | ||
throw new Error( | ||
`All relationships must have a cardinality or a type. Check the "${relKey}" relationship on the "${resKey}" resource type.`, | ||
); | ||
} | ||
if ( | ||
relationship.cardinality !== "one" && | ||
relationship.cardinality !== "many" | ||
) { | ||
throw new Error( | ||
`Relationship cardinalities must be either "one" or "many". Check the "${relKey}" relationship on the "${resKey}" resource type.`, | ||
); | ||
} | ||
if (!Object.keys(schema.resources).includes(relationship.type)) { | ||
throw new Error( | ||
`"${relationship.type}" is not a valid relationship type. Relationship types must be a type of resource defined in the schema. Check the "${relKey}" relationship on the "${resKey}" resource type. Valid resource type: ${Object.keys(schema.resources).join(", ")}`, | ||
); | ||
} | ||
}, | ||
); | ||
if (!Object.keys(schema.resources).includes(relationship.type)) { | ||
throw new Error( | ||
`"${relationship.type}" is not a valid relationship type. Relationship types must be a type of resource defined in the schema. Check the "${relKey}" relationship on the "${resKey}" resource type. Valid resource type: ${Object.keys(schema.resources).join(", ")}`, | ||
); | ||
} | ||
}, | ||
); | ||
} catch (err) { | ||
throw new Error("Something went wrong validating your schema.", { | ||
cause: err, | ||
}); | ||
} | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
480154
10620