Skip to content

Commit

Permalink
Adding pubspec support for survey validator
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasyishak committed Sep 13, 2023
1 parent b5a826b commit 9fd695c
Show file tree
Hide file tree
Showing 8 changed files with 535 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/contextual-json-validator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: surveys/validator
working-directory: surveys/survey-validator
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
- uses: dart-lang/setup-dart@d6a63dab3335f427404425de0fbfed4686d93c4f
Expand Down
4 changes: 4 additions & 0 deletions surveys/contextual-survey-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"description": "Help improve Flutter's release builds with this 3-question survey!",
"snoozeForMinutes": 7200,
"samplingRate": 0.1,
"excludeDashTools": [
"flutter-tool",
"dart-tool"
],
"conditions": [
{
"field": "logFileStats.recordCount",
Expand Down
3 changes: 3 additions & 0 deletions surveys/survey-validator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
30 changes: 30 additions & 0 deletions surveys/survey-validator/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file configures the static analysis results for your project (errors,
# warnings, and lints).
#
# This enables the 'recommended' set of lints from `package:lints`.
# This set helps identify many issues that may lead to problems when running
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
# style and format.
#
# If you want a smaller set of lints you can change this to specify
# 'package:lints/core.yaml'. These are just the most critical lints
# (the recommended set includes the core lints).
# The core lints are also what is used by pub.dev for scoring packages.

include: package:lints/recommended.yaml

# Uncomment the following section to specify additional rules.

# linter:
# rules:
# - camel_case_types

# analyzer:
# exclude:
# - path/to/excluded/files/**

# For more information about the core and recommended set of lints, see
# https://dart.dev/go/core-lints

# For additional information about configuring this file, see
# https://dart.dev/guides/language/analysis-options
10 changes: 10 additions & 0 deletions surveys/survey-validator/bin/survey_validator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dart:io';

import 'package:survey_validator/survey_validator.dart' as survey_validator;

void main(List<String> arguments) {
final contextualSurveyFile =
File('${Directory.current.path}/surveys/contextual-survey-metadata.json');

survey_validator.checkJson(contextualSurveyFile);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
import 'dart:convert';
import 'dart:io';

void main() {
final contextualSurveyFile = File('surveys/contextual-survey-metadata.json');
import 'package:unified_analytics/unified_analytics.dart';

/// The allowed action strings for a given button
const allowedButtonActions = {
'accept',
'dismiss',
'snooze',
};

/// The allowed operators for a given condition item
const allowedConditionOperators = {
'>=',
'<=',
'>',
'<',
'==',
'!=',
};

/// Required keys for the button object
const buttonRequiredKeys = [
'buttonText',
'action',
'url',
'promptRemainsVisible',
];

/// Required keys for the condition object
const conditionRequiredKeys = [
'field',
'operator',
'value',
];

/// The top level keys that must exist for each json object
/// in the array
const requiredKeys = {
'uniqueId',
'startDate',
'endDate',
'description',
'snoozeForMinutes',
'samplingRate',
'conditions',
'buttons',
'excludeDashTools',
};

/// The valid dash tools stored in the [DashTool] enum
List<String> get validDashTools => DashTool.values.map((e) => e.label).toList();

void checkJson(File contextualSurveyFile) {
final jsonContents = jsonDecode(contextualSurveyFile.readAsStringSync());

if (jsonContents is! List) {
Expand Down Expand Up @@ -35,6 +85,7 @@ void main() {
final description = surveyObject['description'] as String;
final snoozeForMinutes = surveyObject['snoozeForMinutes'] as int;
final samplingRate = surveyObject['samplingRate'] as double;
final excludeDashToolsList = surveyObject['excludeDashTools'] as List;
final conditionList = surveyObject['conditions'] as List;
final buttonList = surveyObject['buttons'] as List;

Expand Down Expand Up @@ -62,6 +113,20 @@ void main() {
throw ArgumentError('Sampling rate must be between 0 and 1 inclusive');
}

// Validation on the array containing dash tools to exclude
for (final excludeDashTool in excludeDashToolsList) {
if (excludeDashTool is! String) {
throw ArgumentError(
'Each dash tool in the exclude list must be a string');
}

if (!validDashTools.contains(excludeDashTool)) {
throw ArgumentError(
'The excluded dash tool: "$excludeDashTool" is not valid\n'
'Valid dash tools are: ${validDashTools.join(', ')}');
}
}

// Validation on the condition array
for (final conditionObject in conditionList) {
if (conditionObject is! Map) {
Expand Down Expand Up @@ -136,48 +201,3 @@ void main() {
}
}
}

/// The allowed action strings for a given button
const allowedButtonActions = {
'accept',
'dismiss',
'snooze',
};

/// The allowed operators for a given condition item
const allowedConditionOperators = {
'>=',
'<=',
'>',
'<',
'==',
'!=',
};

/// Required keys for the button object
const buttonRequiredKeys = [
'buttonText',
'action',
'url',
'promptRemainsVisible',
];

/// Required keys for the condition object
const conditionRequiredKeys = [
'field',
'operator',
'value',
];

/// The top level keys that must exist for each json object
/// in the array
const requiredKeys = {
'uniqueId',
'startDate',
'endDate',
'description',
'snoozeForMinutes',
'samplingRate',
'conditions',
'buttons',
};
Loading

0 comments on commit 9fd695c

Please sign in to comment.