Skip to content

Commit

Permalink
debug: comming back to main branch
Browse files Browse the repository at this point in the history
Signed-off-by: Tokesh <tokesh789@gmail.com>
  • Loading branch information
Tokesh committed Jan 18, 2025
1 parent 04e55a7 commit 59dd87b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-tools-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
node-version: '20'

- name: Install Dependencies
run: npm install
run: npm ci

- name: Tests
run: |
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"style:prepare": "ts-node tools/src/prepare-for-vale/prepare-for-vale.ts",
"test": "npm run test:unit && npm run test:integ",
"jest": "jest",
"test:unit": "jest tools/tests/tester/StoryValidator.test.ts --testPathIgnorePatterns=/integ/",
"test:unit": "jest --testMatch='**/*.test.ts' --testPathIgnorePatterns=/integ/",
"test:integ": "jest --testMatch='**/integ/*.test.ts' --runInBand",
"test:spec": "ts-node tools/src/tester/test.ts",
"test:spec--insecure": "ts-node tools/src/tester/test.ts --opensearch-insecure"
Expand Down
2 changes: 0 additions & 2 deletions tools/src/_utils/AjvErrorsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ export default class AjvErrorsParser {
}

parse(errors: ErrorObject[] | undefined | null): string {
console.log('Raw errors:', errors); // Логируем все ошибки
const error_groups = this.#group_errors(errors ?? []);
const parsed_errors = [
this.#prohibited_property_error(error_groups.prohibited),
this.#required_property_error(error_groups.required),
this.#enum_error(error_groups.enum),
...error_groups.others
].filter(e => e != null) as ErrorObject[];
console.log('Parsed errors:', parsed_errors); // Логируем после обработки
return this.ajv.errorsText(parsed_errors, this.options);
}

Check failure on line 38 in tools/src/_utils/AjvErrorsParser.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed

Expand Down
6 changes: 2 additions & 4 deletions tools/src/_utils/JsonSchemaValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ export default class JsonSchemaValidator {
message: string | undefined

constructor(default_schema?: Record<any, any>, options: JsonSchemaValidatorOpts = {}) {
this.ajv = new AJV({ ...DEFAULT_AJV_OPTS, ...options.ajv_opts, removeAdditional: false, verbose: true, allErrors: true ,strictTypes: false})
this.ajv = new AJV({ ...DEFAULT_AJV_OPTS, ...options.ajv_opts })
addFormats(this.ajv);
if (options.ajv_errors_opts != null) ajv_errors(this.ajv, options.ajv_errors_opts)
for (const keyword of options.additional_keywords ?? []) this.ajv.addKeyword(keyword)
Object.entries(options.reference_schemas ?? {}).forEach(([key, schema]) => {
try {
this.ajv.addSchema(schema, key);
Expand All @@ -50,7 +51,6 @@ export default class JsonSchemaValidator {

validate_data(data: any, schema?: Record<any, any>): string | undefined {
if (schema) return this.#validate(this.ajv.compile(schema), data)
console.log("validate_data func", data);
if (this._validate) return this.#validate(this._validate, data)
throw new Error('No schema provided')
}
Expand All @@ -62,9 +62,7 @@ export default class JsonSchemaValidator {

#validate(validate_func: ValidateFunction, data: any, is_schema: boolean = false): string | undefined {
const valid = validate_func(data) as boolean
console.log("valid", valid);
const errors = is_schema ? this.ajv.errors : validate_func.errors
console.log("errors", errors);
return valid ? undefined : this.errors_parser.parse(errors)
}
}
7 changes: 1 addition & 6 deletions tools/src/tester/StoryValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,24 @@ export default class StoryValidator {

constructor() {
const schema = read_yaml(StoryValidator.SCHEMA_FILE)
this.json_validator = new JsonSchemaValidator(schema)
this.json_validator = new JsonSchemaValidator(schema, { ajv_opts: { strictTypes: false } })
}

validate(story_file: StoryFile): StoryEvaluation | undefined {
const schema_file_error = this.#validate_schema_path(story_file)
if (schema_file_error != null) return schema_file_error
const schema_error = this.#validate_schema(story_file)
console.log("schema_error", schema_error)
if (schema_error != null) return schema_error
}

#validate_schema_path(story_file: StoryFile): StoryEvaluation | undefined {
const actual_path = story_file.story.$schema
console.log("actual_path", actual_path)
const expected_path = path.relative(path.dirname(story_file.full_path), StoryValidator.SCHEMA_FILE)
console.log("expected_path", expected_path)
if (actual_path !== expected_path) return this.#invalid(story_file, `Expected $schema to be ${expected_path}`)
}

#validate_schema(story_file: StoryFile): StoryEvaluation | undefined {
console.log("story_file", story_file)
const message = this.json_validator.validate_data(story_file.story)
console.log("validate_schema message", message);
if (message != null) return this.#invalid(story_file, message)
}

Expand Down
48 changes: 23 additions & 25 deletions tools/tests/tester/StoryValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@ const validator = new StoryValidator()

function validate(path: string): StoryEvaluation | undefined {
const story: ParsedStory = StoryParser.parse(read_yaml(path))
const rr = validator.validate({ story, display_path: path, full_path: path })
console.log(rr);
return rr;
return validator.validate({ story, display_path: path, full_path: path });
}

describe('StoryValidator', () => {
// test('wrong $schema', () => {
// const evaluation = validate('tools/tests/tester/fixtures/wrong_$schema.yaml')
// expect(evaluation?.result).toBe('ERROR')
// expect(evaluation?.message).toBe('Invalid Story: Expected $schema to be ../../../../json_schemas/test_story.schema.yaml')
// })
test('wrong $schema', () => {
const evaluation = validate('tools/tests/tester/fixtures/wrong_$schema.yaml')
expect(evaluation?.result).toBe('ERROR')
expect(evaluation?.message).toBe('Invalid Story: Expected $schema to be ../../../../json_schemas/test_story.schema.yaml')
})

test('invalid story', () => {
const evaluation = validate('tools/tests/tester/fixtures/invalid_story.yaml')
Expand All @@ -38,23 +36,23 @@ describe('StoryValidator', () => {
"data/chapters/1/method must be array --- data/chapters/1/method must match exactly one schema in oneOf")
})

// test('invalid description', () => {
// const evaluation = validate('tools/tests/tester/fixtures/invalid_description.yaml')
// expect(evaluation?.result).toBe('ERROR')
// expect(evaluation?.message).toBe("Invalid Story: " +
// "data/description must match pattern \"^\\p{Lu}[\\s\\S]*\\.$\" --- " +
// "data/chapters/0/synopsis must match pattern \"^\\p{Lu}[\\s\\S]*\\.$|^\\p{Lu}[\\s\\S]*\\. \\[(GET|PUT|POST|DELETE|PATCH|HEAD|OPTIONS)\\]$\"")
// })
test('invalid description', () => {
const evaluation = validate('tools/tests/tester/fixtures/invalid_description.yaml')
expect(evaluation?.result).toBe('ERROR')
expect(evaluation?.message).toBe("Invalid Story: " +
"data/description must match pattern \"^\\p{Lu}[\\s\\S]*\\.$\" --- " +
"data/chapters/0/synopsis must match pattern \"^\\p{Lu}[\\s\\S]*\\.$|^\\p{Lu}[\\s\\S]*\\. \\[(GET|PUT|POST|DELETE|PATCH|HEAD|OPTIONS)\\]$\"")
})

// test('invalid property', () => {
// const evaluation = validate('tools/tests/tester/fixtures/invalid_property.yaml')
// expect(evaluation?.result).toBe('ERROR')
// expect(evaluation?.message).toBe("Invalid Story: " +
// "data/prologues/0/retry contains unsupported properties: until")
// })
test('invalid property', () => {
const evaluation = validate('tools/tests/tester/fixtures/invalid_property.yaml')
expect(evaluation?.result).toBe('ERROR')
expect(evaluation?.message).toBe("Invalid Story: " +
"data/prologues/0/retry contains unsupported properties: until")
})

// test('valid story', () => {
// const evaluation = validate('tools/tests/tester/fixtures/valid_story.yaml')
// expect(evaluation).toBeUndefined()
// })
test('valid story', () => {
const evaluation = validate('tools/tests/tester/fixtures/valid_story.yaml')
expect(evaluation).toBeUndefined()
})
})

0 comments on commit 59dd87b

Please sign in to comment.