Is there any way to check, at runtime, if a function parameter is (or not) a Normalizr schema class? Could be any type: entity, array, object, etc.
For example:
function processTMDBRespose(response, schema) {
// if 'schema' param is not a normalizr schema, throw!
// some code
}
You can and can't do what you're looking for.
If you write yourself a lint rule that only allows creating schema from normalizr classes, like new schema.Array()
and prohibits the use of shorthand []
, then you can check using instanceof
:
if (
mySchema instanceof schema.Array ||
mySchema instanceof schema.Entity ||
mySchema instanceof schema.Object ||
mySchema instanceof schema.Union ||
mySchema instanceof schema.Values
) {
// your code
} else {
throw new Error('mySchema is not a schema');
}
However, if you use shorthand, any array []
or plain object {}
is also a valid schema for schema.Array
and schema.Object
, respectively. This is much more difficult to validate, because almost everything is typeof Object
in JavaScript (like null
)