UNPKG

rclnodejs

Version:
99 lines (86 loc) 3.17 kB
// Copyright (c) 2017 Intel Corporation. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. 'use strict'; const rclnodejs = require('./native_loader.js'); const { TypeValidationError, NameValidationError } = require('./errors.js'); /** * An object - Representing a validator in ROS. * @exports validator */ let validator = { _createErrorFromValidation: function (result, nameValue, nameType) { return new NameValidationError(nameValue, nameType, result[0], result[1]); }, /** * Validate a given topic or service name, and throw an error if invalid. * @param {string} topic - The name of topic/service. and it must be fully-qualified and already expanded. * @return {boolean} - True if it is valid. */ validateFullTopicName(topic) { if (typeof topic !== 'string') { throw new TypeValidationError('topic', topic, 'string'); } let result = rclnodejs.validateFullTopicName(topic); if (result === null) { return true; } throw this._createErrorFromValidation(result, topic, 'topic'); }, /** * Validate a given node name, and throw an error if invalid. * @param {string} name - The name of node. * @return {boolean} - True if it is valid. */ validateNodeName(name) { if (typeof name !== 'string') { throw new TypeValidationError('name', name, 'string'); } let result = rclnodejs.validateNodeName(name); if (result === null) { return true; } throw this._createErrorFromValidation(result, name, 'node'); }, /** * Validate a given topic or service name, and throw an error if invalid. * @param {string} topic - The name of topic/service and does not have to be fully-qualified and is not expanded. * @return {boolean} - True if it is valid. */ validateTopicName(topic) { if (typeof topic !== 'string') { throw new TypeValidationError('topic', topic, 'string'); } let result = rclnodejs.validateTopicName(topic); if (result === null) { return true; } throw this._createErrorFromValidation(result, topic, 'topic'); }, /** * Validate a given namespace, and throw an error if invalid. * @param {string} namespace - The namespace to be validated * @return {boolean} - True if it is valid. */ validateNamespace(namespace) { if (typeof namespace !== 'string') { throw new TypeValidationError('namespace', namespace, 'string'); } let result = rclnodejs.validateNamespace(namespace); if (result === null) { return true; } throw this._createErrorFromValidation(result, namespace, 'namespace'); }, }; module.exports = validator;