utils/validation.js - Validation Utilities
Location: src/utils/validation.js
This file provides a centralized set of static methods for validating parameters and data structures used throughout the Nagini library.
Class: ValidationUtils
This class contains static methods for various validation tasks.
validateArray(value, paramName, component)
- Description: Checks if a value is an array.
- Throws:
Errorif the value is not an array.
validateString(value, paramName, component, allowEmpty)
- Description: Checks if a value is a string.
allowEmpty(boolean, default:false): Iffalse, the string cannot be empty.- Throws:
Errorif the value is not a string or if it's empty when not allowed.
validateBoolean(value, paramName, component)
- Description: Checks if a value is a boolean.
- Throws:
Errorif the value is not a boolean.
validateFunction(value, paramName, component)
- Description: Checks if a value is a function.
- Throws:
Errorif the value is not a function.
validateObject(value, paramName, component)
- Description: Checks if a value is a plain object (not
nullor an array). - Throws:
Errorif the value is not an object.
validateWorker(worker, component)
- Description: Validates a
Workerinstance by checking for thepostMessagemethod. - Throws:
Errorif the worker is not a valid instance.
validatePyodide(pyodide, component)
- Description: Validates a Pyodide instance by checking for the
FSobject andrunPythonmethod. - Throws:
Errorif the instance is not valid.
validateFilesToLoad(filesToLoad, component)
- Description: Validates an array of file objects, ensuring each has a
urlandpath. - Throws:
Errorif the array or any of its objects are invalid.
validatePackages(packages, component)
- Description: Validates an array of package names, ensuring each is a non-empty string.
- Throws:
Errorif the array or any of its elements are invalid.
validateNamespace(namespace, component)
- Description: Validates an optional
namespaceobject. - Throws:
Errorif the namespace is defined but is not a valid object.
validateExecutionParams(filename, code, namespace, component)
- Description: Validates all parameters for an execution call.
- Throws:
Errorif any parameter is invalid.
checkDangerousPatterns(code)
- Description: Scans Python code for potentially dangerous patterns (e.g.,
import os). - Returns: An array of strings describing any dangerous patterns found.
validateBackend(backend, component)
- Description: Validates the backend name (must be
'pyodide'or'brython'). - Throws:
Errorif the backend name is not valid.