private static Func <IEnumerable <SpecificationProperty>, IEnumerable <Error> > EnforceRequired() { return(specProps => { var setsWithTrue = specProps .Where(sp => sp.Specification.IsOption() && sp.Value.IsJust() && sp.Specification.Required) .Select(s => ((OptionSpecification)s.Specification).SetName).ToList(); var requiredButEmpty = specProps .Where(sp => sp.Specification.IsOption()) .Where(sp => sp.Value.IsNothing() && sp.Specification.Required && !setsWithTrue.Contains(((OptionSpecification)sp.Specification).SetName)) .Concat(specProps .Where(sp => sp.Specification.IsValue() && sp.Value.IsNothing() && sp.Specification.Required)).ToList(); if (requiredButEmpty.Any()) { return requiredButEmpty.Select(s => new MissingRequiredOptionError( NameExtensions.FromSpecification(s.Specification))); } return Enumerable.Empty <Error>(); }); }
private static Func <IEnumerable <SpecificationProperty>, IEnumerable <Error> > EnforceMutuallyExclusiveSet() { return(specProps => { var options = specProps .Where(sp => sp.Specification.IsOption()) .Where(sp => ((OptionSpecification)sp.Specification).SetName.Length > 0 && sp.Value.IsJust()); var groups = options.GroupBy(g => ((OptionSpecification)g.Specification).SetName); if (groups.Count() > 1) { return options.Select(s => new MutuallyExclusiveSetError( NameExtensions.FromOptionSpecification((OptionSpecification)s.Specification))); } return Enumerable.Empty <Error>(); }); }
private static Func <IEnumerable <SpecificationProperty>, IEnumerable <Error> > EnforceRange() { return(specProps => { var options = specProps.Where( sp => sp.Specification.TargetType == TargetType.Sequence && sp.Value.IsJust() && ( (sp.Specification.Min.IsJust() && ((Array)sp.Value.FromJust()).Length < sp.Specification.Min.FromJust()) || (sp.Specification.Max.IsJust() && ((Array)sp.Value.FromJust()).Length > sp.Specification.Max.FromJust()) ) ); if (options.Any()) { return options.Select(s => new SequenceOutOfRangeError( NameExtensions.FromSpecification(s.Specification))); } return Enumerable.Empty <Error>(); }); }
MapValues( IEnumerable <SpecificationProperty> propertyTuples, IEnumerable <KeyValuePair <string, IEnumerable <string> > > options, Func <IEnumerable <string>, Type, bool, Maybe <object> > converter, StringComparer comparer) { var sequencesAndErrors = propertyTuples .Select(pt => options.FirstOrDefault( s => s.Key.MatchName(((OptionSpecification)pt.Specification).ShortName, ((OptionSpecification)pt.Specification).LongName, comparer)) .ToMaybe() .Return(sequence => converter(sequence.Value, pt.Property.PropertyType, pt.Specification.TargetType != TargetType.Sequence) .Return(converted => Tuple.Create( pt.WithValue(Maybe.Just(converted)), Maybe.Nothing <Error>()), Tuple.Create <SpecificationProperty, Maybe <Error> >( pt, Maybe.Just <Error>(new BadFormatConversionError(NameExtensions.FromOptionSpecification((OptionSpecification)pt.Specification))))), Tuple.Create(pt, Maybe.Nothing <Error>())) ); return(StatePair.Create( sequencesAndErrors.Select(se => se.Item1), sequencesAndErrors.Select(se => se.Item2).OfType <Just <Error> >().Select(se => se.Value))); }