IsValidValue() public method

public IsValidValue ( object value ) : bool
value object
return bool
Example #1
0
        private static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts)
        {
            SessionStateScope    scope;
            SessionStateInternal engineSessionState = executionContext.EngineSessionState;
            CommandOrigin        scopeOrigin        = engineSessionState.CurrentScope.ScopeOrigin;

            if (!variablePath.IsVariable)
            {
                engineSessionState.SetVariable(variablePath, value, true, scopeOrigin);
                return(value);
            }
            if (executionContext.PSDebugTraceLevel > 0)
            {
                executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value);
            }
            if (variablePath.IsUnscopedVariable)
            {
                variablePath = variablePath.CloneAndSetLocal();
            }
            PSVariable newValue = engineSessionState.GetVariableItem(variablePath, out scope, scopeOrigin);

            if (newValue == null)
            {
                Collection <Attribute> attributes = (attributeAsts == null) ? new Collection <Attribute>() : GetAttributeCollection(attributeAsts);
                newValue = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);
                engineSessionState.SetVariable(variablePath, newValue, false, scopeOrigin);
                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
                return(value);
            }
            if (attributeAsts != null)
            {
                newValue.Attributes.Clear();
                Collection <Attribute> attributeCollection = GetAttributeCollection(attributeAsts);
                value = PSVariable.TransformValue(attributeCollection, value);
                if (!PSVariable.IsValidValue(attributeCollection, value))
                {
                    ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidValueFailure, new object[] { newValue.Name, (value != null) ? value.ToString() : "" });
                    throw exception;
                }
                newValue.SetValueRaw(value, true);
                newValue.AddParameterAttributesNoChecks(attributeCollection);
                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
                return(value);
            }
            newValue.Value = value;
            return(value);
        }
Example #2
0
        private object VerifyNewAttribute(Attribute item)
        {
            object inputData = this.variable.Value;
            ArgumentTransformationAttribute attribute = item as ArgumentTransformationAttribute;

            if (attribute != null)
            {
                ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                EngineIntrinsics engineIntrinsics        = null;
                if (executionContextFromTLS != null)
                {
                    engineIntrinsics = executionContextFromTLS.EngineIntrinsics;
                }
                inputData = attribute.Transform(engineIntrinsics, inputData);
            }
            if (!PSVariable.IsValidValue(inputData, item))
            {
                ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidMetadataForCurrentValue, new object[] { this.variable.Name, (this.variable.Value != null) ? this.variable.Value.ToString() : "" });
                throw exception;
            }
            return(inputData);
        }
Example #3
0
        /// <summary>
        /// Validates and performs any transformations that the new attribute
        /// implements.
        /// </summary>
        /// <param name="item">
        /// The new attribute to be added to the collection.
        /// </param>
        /// <returns>
        /// The new variable value. This may change from the original value if the
        /// new attribute is an ArgumentTransformationAttribute.
        /// </returns>
        private object VerifyNewAttribute(Attribute item)
        {
            object variableValue = _variable.Value;

            // Perform transformation before validating
            ArgumentTransformationAttribute argumentTransformation = item as ArgumentTransformationAttribute;

            if (argumentTransformation != null)
            {
                // Get an EngineIntrinsics instance using the context of the thread.

                ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS();
                EngineIntrinsics engine  = null;

                if (context != null)
                {
                    engine = context.EngineIntrinsics;
                }

                variableValue = argumentTransformation.TransformInternal(engine, variableValue);
            }

            if (!PSVariable.IsValidValue(variableValue, item))
            {
                ValidationMetadataException e = new ValidationMetadataException(
                    "ValidateSetFailure",
                    null,
                    Metadata.InvalidMetadataForCurrentValue,
                    _variable.Name,
                    ((_variable.Value != null) ? _variable.Value.ToString() : string.Empty));

                throw e;
            }

            return(variableValue);
        }
Example #4
0
        internal static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts)
        {
            SessionStateInternal sessionState = executionContext.EngineSessionState;
            CommandOrigin        origin       = sessionState.CurrentScope.ScopeOrigin;

            if (!variablePath.IsVariable)
            {
                sessionState.SetVariable(variablePath, value, true, origin);
                return(value);
            }

            // Variable assignment is traced only if trace level 2 is specified.
            if (executionContext.PSDebugTraceLevel > 1)
            {
                executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value);
            }

            if (variablePath.IsUnscopedVariable)
            {
                variablePath = variablePath.CloneAndSetLocal();
            }

            SessionStateScope scope;
            PSVariable        var = sessionState.GetVariableItem(variablePath, out scope, origin);

            if (var == null)
            {
                var attributes = attributeAsts == null
                                     ? new Collection <Attribute>()
                                     : GetAttributeCollection(attributeAsts);
                var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);

                sessionState.SetVariable(variablePath, var, false, origin);

                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
            }
            else if (attributeAsts != null)
            {
                // Use bytewise operation directly instead of 'var.IsReadOnly || var.IsConstant' on
                // a hot path (setting variable with type constraint) to get better performance.
                if ((var.Options & (ScopedItemOptions.ReadOnly | ScopedItemOptions.Constant)) != ScopedItemOptions.None)
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                            var.Name,
                            SessionStateCategory.Variable,
                            "VariableNotWritable",
                            SessionStateStrings.VariableNotWritable);
                    throw e;
                }

                var attributes = GetAttributeCollection(attributeAsts);
                value = PSVariable.TransformValue(attributes, value);
                if (!PSVariable.IsValidValue(attributes, value))
                {
                    ValidationMetadataException e = new ValidationMetadataException(
                        "ValidateSetFailure",
                        null,
                        Metadata.InvalidValueFailure,
                        var.Name,
                        ((value != null) ? value.ToString() : "$null"));

                    throw e;
                }
                var.SetValueRaw(value, true);
                // Don't update the PSVariable's attributes until we successfully set the value
                var.Attributes.Clear();
                var.AddParameterAttributesNoChecks(attributes);

                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
            }
            else
            {
                // The setter will handle checking for variable writes.
                var.Value = value;
            }

            return(value);
        }
        internal static object SetVariableValue(VariablePath variablePath, object value, ExecutionContext executionContext, AttributeBaseAst[] attributeAsts)
        {
            SessionStateInternal sessionState = executionContext.EngineSessionState;
            CommandOrigin        origin       = sessionState.CurrentScope.ScopeOrigin;

            if (!variablePath.IsVariable)
            {
                sessionState.SetVariable(variablePath, value, true, origin);
                return(value);
            }

            // Variable assignment is traced only if trace level 2 is specified.
            if (executionContext.PSDebugTraceLevel > 1)
            {
                executionContext.Debugger.TraceVariableSet(variablePath.UnqualifiedPath, value);
            }

            if (variablePath.IsUnscopedVariable)
            {
                variablePath = variablePath.CloneAndSetLocal();
            }

            SessionStateScope scope;
            PSVariable        var = sessionState.GetVariableItem(variablePath, out scope, origin);

            if (var == null)
            {
                var attributes = attributeAsts == null
                                     ? new Collection <Attribute>()
                                     : GetAttributeCollection(attributeAsts);
                var = new PSVariable(variablePath.UnqualifiedPath, value, ScopedItemOptions.None, attributes);

                sessionState.SetVariable(variablePath, var, false, origin);

                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
            }
            else if (attributeAsts != null)
            {
                var.Attributes.Clear();
                var attributes = GetAttributeCollection(attributeAsts);
                value = PSVariable.TransformValue(attributes, value);
                if (!PSVariable.IsValidValue(attributes, value))
                {
                    ValidationMetadataException e = new ValidationMetadataException(
                        "ValidateSetFailure",
                        null,
                        Metadata.InvalidValueFailure,
                        var.Name,
                        ((value != null) ? value.ToString() : "$null"));

                    throw e;
                }
                var.SetValueRaw(value, true);
                var.AddParameterAttributesNoChecks(attributes);

                if (executionContext._debuggingMode > 0)
                {
                    executionContext.Debugger.CheckVariableWrite(variablePath.UnqualifiedPath);
                }
            }
            else
            {
                // The setter will handle checking for variable writes.
                var.Value = value;
            }

            return(value);
        }